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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
05-11-2024, 11:53 AM
|
#1
|
Member
Registered: Aug 2023
Location: India
Distribution: Right now with LMDE
Posts: 61
Rep:
|
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.
|
|
|
05-11-2024, 01:03 PM
|
#2
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,146
|
Quote:
Originally Posted by Bala451987
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.
|
|
|
05-11-2024, 02:13 PM
|
#3
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,924
|
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');
?>
|
|
|
05-12-2024, 08:10 AM
|
#4
|
Member
Registered: Aug 2023
Location: India
Distribution: Right now with LMDE
Posts: 61
Original Poster
Rep:
|
Quote:
Originally Posted by TB0ne
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.
|
|
|
05-12-2024, 08:12 AM
|
#5
|
Member
Registered: Aug 2023
Location: India
Distribution: Right now with LMDE
Posts: 61
Original Poster
Rep:
|
Quote:
Originally Posted by NevemTeve
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.
|
|
|
05-12-2024, 08:27 AM
|
#6
|
Member
Registered: Aug 2023
Location: India
Distribution: Right now with LMDE
Posts: 61
Original Poster
Rep:
|
Quote:
Originally Posted by NevemTeve
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.
|
|
|
05-12-2024, 10:07 AM
|
#7
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,146
|
Quote:
Originally Posted by Bala451987
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???
|
|
|
05-12-2024, 07:51 PM
|
#8
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,829
|
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.
|
|
|
05-13-2024, 07:57 AM
|
#9
|
Member
Registered: Aug 2023
Location: India
Distribution: Right now with LMDE
Posts: 61
Original Poster
Rep:
|
Quote:
Originally Posted by TB0ne
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.
|
|
|
05-13-2024, 08:05 AM
|
#10
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,146
|
Quote:
Originally Posted by Bala451987
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.
|
|
|
05-14-2024, 02:28 AM
|
#11
|
Member
Registered: Aug 2023
Location: India
Distribution: Right now with LMDE
Posts: 61
Original Poster
Rep:
|
Quote:
Originally Posted by TB0ne
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.
|
|
|
05-14-2024, 07:47 AM
|
#12
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,146
|
Quote:
Originally Posted by Bala451987
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.
|
|
|
05-14-2024, 08:47 AM
|
#13
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,924
|
(We might be over-pressing this. The OP either gives more information or not, it is up to him or her.)
|
|
|
05-14-2024, 11:08 AM
|
#14
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,146
|
Quote:
Originally Posted by NevemTeve
(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.
|
|
|
05-16-2024, 02:26 AM
|
#15
|
Member
Registered: Aug 2023
Location: India
Distribution: Right now with LMDE
Posts: 61
Original Poster
Rep:
|
Quote:
Originally Posted by TB0ne
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.
|
|
|
All times are GMT -5. The time now is 12:11 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|