LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-11-2024, 11:53 AM   #1
Bala451987
Member
 
Registered: Aug 2023
Posts: 55

Rep: Reputation: 0
Error i am facing in wordpress


Hello guys!

I am not sure, whether i can ask the question here about WordPress. I am trying to implement lazy-load function through code. But i am encountering an error. Please find the error and the code i am implementing down below.

## The code i am implementing in function.php

function add_lazyload_to_images($content) {
if (is_admin()) {
return $content;
}

// Add 'loading="lazy"' attribute to images
$content = preg_replace('/<img([^>]*)src=["\']([^"\']*)["\']([^>]*)>/i', '<img$1src="$2" loading="lazy"$3>', $content);

return $content;
}

// Apply the filter to 'the_content'
add_filter('the_content', 'add_lazyload_to_images');


## The error i am encountering:

PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'add_lazyload_to_images' not found or invalid function name in /home/kc8klqrq8k3y/public_html/blog.drugvigil.com/wp-includes/class-wp-hook.php on line 324

Can anyone understand and help me to find the solution. Thanks in advance.
 
Old 05-11-2024, 01:03 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,732

Rep: Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973
Quote:
Originally Posted by Bala451987 View Post
Hello guys!
I am not sure, whether i can ask the question here about WordPress. I am trying to implement lazy-load function through code. But i am encountering an error. Please find the error and the code i am implementing down below. ## The code i am implementing in function.php
Code:
function add_lazyload_to_images($content) {
    if (is_admin()) {
        return $content;
    }

    // Add 'loading="lazy"' attribute to images
    $content = preg_replace('/<img([^>]*)src=["\']([^"\']*)["\']([^>]*)>/i', '<img$1src="$2" loading="lazy"$3>', $content);

    return $content;
}

// Apply the filter to 'the_content'
add_filter('the_content', 'add_lazyload_to_images');
## The error i am encountering:
Code:
PHP Warning:  call_user_func_array() expects parameter 1 to be a valid callback, function 'add_lazyload_to_images' not found or invalid function name in /home/kc8klqrq8k3y/public_html/blog.drugvigil.com/wp-includes/class-wp-hook.php on line 324
Can anyone understand and help me to find the solution. Thanks in advance.
As you've been asked before, use CODE tags when posting such things. And did you actually read what you posted??? Specifically, the *EXACT LINE NUMBER* in the code where things are breaking?? Did you try to look up the error and it's meanings, or do ANY research (as you've been asked to before)???

You haven't properly declared that filter or action. Did you modify functions.php to add that in?? To accept the callback?? Either that or you need to do an add_filter or add_action.
 
Old 05-11-2024, 02:13 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,878
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
There's no obvious error in this (incomplete) code. With some additions it can be tested:
Code:
<?php
function is_admin() {
  return false;
}

function add_lazyload_to_images($content) {
  if (is_admin()) {
    return $content;
  }

  // Add 'loading="lazy"' attribute to images
  $content = preg_replace('/<img([^>]*)src=["\']([^"\']*)["\']([^>]*)>/i', '<img$1src="$2" loading="lazy"$3>',

  return $content;
}

function add_filter($content, $cb) {
    $cb($content);
}

// Apply the filter to 'the_content'
add_filter('the_content', 'add_lazyload_to_images');
?>
 
Old 05-12-2024, 08:10 AM   #4
Bala451987
Member
 
Registered: Aug 2023
Posts: 55

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
As you've been asked before, use CODE tags when posting such things. And did you actually read what you posted??? Specifically, the *EXACT LINE NUMBER* in the code where things are breaking?? Did you try to look up the error and it's meanings, or do ANY research (as you've been asked to before)???

You haven't properly declared that filter or action. Did you modify functions.php to add that in?? To accept the callback?? Either that or you need to do an add_filter or add_action.
I am sorry, i did not aware about code blocks:

Code:
				// Avoid the array_slice() if possible.
				if ( 0 === $the_['accepted_args'] ) {
					$value = call_user_func( $the_['function'] );
				} elseif ( $the_['accepted_args'] >= $num_args ) {
					$value = call_user_func_array( $the_['function'], $args );
				} else {
					$value = call_user_func_array( $the_['function'], array_slice( $args, 0, $the_['accepted_args'] ) );
I highlighted in the above code about the specified line. Could you please now find any errors.

Last edited by Bala451987; 05-12-2024 at 08:13 AM.
 
Old 05-12-2024, 08:12 AM   #5
Bala451987
Member
 
Registered: Aug 2023
Posts: 55

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by NevemTeve View Post
There's no obvious error in this (incomplete) code. With some additions it can be tested:
Code:
<?php
function is_admin() {
  return false;
}

function add_lazyload_to_images($content) {
  if (is_admin()) {
    return $content;
  }

  // Add 'loading="lazy"' attribute to images
  $content = preg_replace('/<img([^>]*)src=["\']([^"\']*)["\']([^>]*)>/i', '<img$1src="$2" loading="lazy"$3>',

  return $content;
}

function add_filter($content, $cb) {
    $cb($content);
}

// Apply the filter to 'the_content'
add_filter('the_content', 'add_lazyload_to_images');
?>
Let me try this.
 
Old 05-12-2024, 08:27 AM   #6
Bala451987
Member
 
Registered: Aug 2023
Posts: 55

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by NevemTeve View Post
There's no obvious error in this (incomplete) code. With some additions it can be tested:
Code:
<?php
function is_admin() {
  return false;
}

function add_lazyload_to_images($content) {
  if (is_admin()) {
    return $content;
  }

  // Add 'loading="lazy"' attribute to images
  $content = preg_replace('/<img([^>]*)src=["\']([^"\']*)["\']([^>]*)>/i', '<img$1src="$2" loading="lazy"$3>',

  return $content;
}

function add_filter($content, $cb) {
    $cb($content);
}

// Apply the filter to 'the_content'
add_filter('the_content', 'add_lazyload_to_images');
?>
Encountering fatal error with this code. Let me check.
 
Old 05-12-2024, 10:07 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,732

Rep: Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973
Quote:
Originally Posted by Bala451987 View Post
I am sorry, i did not aware about code blocks:[
Code:
				// Avoid the array_slice() if possible.
				if ( 0 === $the_['accepted_args'] ) {
					$value = call_user_func( $the_['function'] );
				} elseif ( $the_['accepted_args'] >= $num_args ) {
					$value = call_user_func_array( $the_['function'], $args );
				} else {
					$value = call_user_func_array( $the_['function'], array_slice( $args, 0, $the_['accepted_args'] ) );
Sorry, you've been told about them numerous times in the past
Quote:
I highlighted in the above code about the specified line. Could you please now find any errors.
No, because again you don't post all of the code, just a few lines. And again, you were given some solutions; did you actually TRY THEM???? Did you look up the meaning of the error as you were told to?? Did you modify the file you were told to??? Did you try BOTH of the functions???
 
Old 05-12-2024, 07:51 PM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
I'm sorry, but ... "it is a Fool's quest" to try to post a section of code to a public forum and then ask the participants to "solve it for you." Perhaps because it is actually quite impossible for any actual person to do any such thing. (This is why forums created: "stickies!" Please go read them now.)

This is also why the forum software features these particular emojis: ... ... ... ... ... ... ... ...

In the end: "you wrote the code, and you are going to find and resolve the bugs." We can certainly help you by providing information and advice, born from very-specific experience, which might help you drastically(!) reduce the time spent doing so. And, we are extremely sensitive to the frustration that you now feel. But, in the end, what you ask for is truly impossible. You have to find the bug, and solve it.

But also this: [i]"You are certainly not the first person who encountered just(!) this problem." Whatever(!!) the problem might be. Use the search feature, particularly, "at long-standing forums like this one." Very often, the true power of a website like this one is not: the ability to "ask a new question." It is: the ability to efficiently discover the question that has already been asked.

Last edited by sundialsvcs; 05-12-2024 at 07:55 PM.
 
Old 05-13-2024, 07:57 AM   #9
Bala451987
Member
 
Registered: Aug 2023
Posts: 55

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
Sorry, you've been told about them numerous times in the past

No, because again you don't post all of the code, just a few lines. And again, you were given some solutions; did you actually TRY THEM???? Did you look up the meaning of the error as you were told to?? Did you modify the file you were told to??? Did you try BOTH of the functions???
I tried but have some fatal error, the code i tried having issue.

I am not a tech savvy person. I am just a beginner so i tried with the advices given here.
 
Old 05-13-2024, 08:05 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,732

Rep: Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973
Quote:
Originally Posted by Bala451987 View Post
I tried but have some fatal error, the code i tried having issue. I am not a tech savvy person. I am just a beginner so i tried with the advices given here.
Then you probably need to hire someone. We can try to assist you, but *YOU, PERSONALLY* have to do the work. Again, just putting your error into a search-engine pulls up a LOT of possible solutions. I gave you several, so posting just a few lines of code (which is meaningless) doesn't help, nor does saying something like "Have some fatal error". You claim to be 'not tech savvy', so why are you trying to set up a web server, install WordPress, and edit PHP code???

You were given possible solutions, given a search term to use to get you more, and were even handed a testing module/code snippet. Not sure how much more you need to be able to prcoeed.
 
Old 05-14-2024, 02:28 AM   #11
Bala451987
Member
 
Registered: Aug 2023
Posts: 55

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
Then you probably need to hire someone. We can try to assist you, but *YOU, PERSONALLY* have to do the work. Again, just putting your error into a search-engine pulls up a LOT of possible solutions. I gave you several, so posting just a few lines of code (which is meaningless) doesn't help, nor does saying something like "Have some fatal error". You claim to be 'not tech savvy', so why are you trying to set up a web server, install WordPress, and edit PHP code???

You were given possible solutions, given a search term to use to get you more, and were even handed a testing module/code snippet. Not sure how much more you need to be able to prcoeed.

Of course, I understand your frustration. What do you meant by your response? I am perfectly fine to try and learn new things, its my concern if i encounter difficulties or errors. If you are okay with try to explain me. I'm here for assistance and finding solutions. If you're struggling to explain me the right answer, just leave it, but don't discourage me at least.I am in learning phase. I am trying and any ways thanks for your guidance.
 
Old 05-14-2024, 07:47 AM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,732

Rep: Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973
Quote:
Originally Posted by Bala451987 View Post
Of course, I understand your frustration. What do you meant by your response? I am perfectly fine to try and learn new things, its my concern if i encounter difficulties or errors. If you are okay with try to explain me. I'm here for assistance and finding solutions. If you're struggling to explain me the right answer, just leave it, but don't discourage me at least.I am in learning phase. I am trying and any ways thanks for your guidance.
We're only struggling to get you to to listen.

Again you are not paying attention, since you have been given SEVERAL possible solutions, and don't seem to have read/understood/tried them. Until you actually *DO SOMETHING* with what you're told, there's not much point in posting. You've done this in many of your threads, where you don't seem to do basic research, try to look up an error, or listen to what people are telling you. Again, since you seem to have missed it:
  • Put "PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback" into Google and read some of the MANY solutions
  • Modify function.php in your theme.
  • Use add_filter
  • Use add_function
  • Use the diagnostic/test sample you were given
Done any of that correctly??? You *AGAIN* (as you were told) not calling this correctly. If you did basic research, you'd see how to do it; but you haven't.
 
Old 05-14-2024, 08:47 AM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,878
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
(We might be over-pressing this. The OP either gives more information or not, it is up to him or her.)
 
Old 05-14-2024, 11:08 AM   #14
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,732

Rep: Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973
Quote:
Originally Posted by NevemTeve View Post
(We might be over-pressing this. The OP either gives more information or not, it is up to him or her.)
This is the same pattern the OP has displayed in most of their threads, where they will also play the "I'm very new" card. Hard to imagine someone who is 'very new' getting a job and being tasked with things they don't know how to do, or have little experience doing.
 
Old 05-16-2024, 02:26 AM   #15
Bala451987
Member
 
Registered: Aug 2023
Posts: 55

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
This is the same pattern the OP has displayed in most of their threads, where they will also play the "I'm very new" card. Hard to imagine someone who is 'very new' getting a job and being tasked with things they don't know how to do, or have little experience doing.
"Hey, I gave it a shot and responded, but I encountered a fatal error. I might need your support later on." I am trying to find out. Thanks for your support.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: WordPress Dashboard & Settings Page | WordPress 101 LXer Syndicated Linux News 0 12-07-2020 04:42 PM
Docker with wordpress/mysql/nginx, no styles in wordpress E-Kami Linux - Server 2 01-23-2018 02:41 PM
LXer: WordPress Plugin Tutorial — How To Install WordPress Plugins LXer Syndicated Linux News 0 12-09-2015 09:10 AM
Apache2 Configuration or Wordpress Configuration: Wordpress index.php not loading khun Linux - Newbie 5 05-18-2012 08:26 AM
how to install pre existing wordpress directory on a existing wordpress to run a web ajbardhan Linux - Software 3 04-28-2012 07:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration