CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   Others   [Solved] Warning: call_user_func_array() Expects Parameter 1 ...

[Solved] Warning: call_user_func_array() Expects Parameter 1 to be a Valid Callback, Function Not found or Invalid Function Name

August 26, 2018 by SNK

warning call_user_func_array() expects parameter 1 to be a valid callback wordpress,

In this short tutorial we are going see how can we fix,

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘metabox_name’ not found or invalid function name in \wp-includes\class-wp-hook.php on line 288

warning message in WordPress.

Lets get started,

What are we going to do ?

  1. Crete custom Metabox inside a Class and Instantiate it by creating an object.
  2. Solving the issue.

Step 1 : – Creating Metabox Inside a Class and Instantiate it By Creating an Object

As the part of this tutorial i am going to use recent post that was to add meta box and handle custom meta box multiple fields in WordPress.

I am just going to create new class called “Personal Details” and copy paste that code inside it and instantiate by creating an object of the class.

Creating Custom Meta Box Inside a Class
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
 
Class PersonalDetails {
 
 
    public function __construct() {
 
    add_action('add_meta_boxes','pdetails_meta_box');
    add_action('save_post','pdetails_save');
    }
 
function pdetails_meta_box() {
add_meta_box('pdetails','Personal Details','pdetails_callback','post');
}
 
 
function pdetails_callback( $post ) {
 
wp_nonce_field('pdetails_save','pdetails_meta_box_nonce');
$pdetails =  get_post_meta($post->ID,'_pdetails_key',false);
 
?>
 
        <label for="">Enter First Name</label>
        <input type="text" name="first_name_field" placeholder="Enter First Name" value="<?php echo $pdetails[0]['first_name']; ?>">
        <br>
        <label for="">Enter Last Name</label>
        <input type="text" name="last_name_field" placeholder="Enter last Name" value="<?php echo $pdetails[0]['last_name']; ?>">
        <br>
        <label for="">Enter Email</label>
        <input type="text" name="email_field" placeholder="Enter Email" value="<?php echo $pdetails[0]['email']; ?>">
<?php
}
 
function pdetails_save( $post_id ) {
 
if( ! isset($_POST['pdetails_meta_box_nonce'])) {
return;
}
 
if( ! wp_verify_nonce( $_POST['pdetails_meta_box_nonce'], 'pdetails_save') ) {
return;
}
 
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
 
if( ! current_user_can('edit_post', $post_id)) {
return;
}
 
$personal_details = [
'first_name' => $_POST['first_name_field'],
'last_name' => $_POST['last_name_field'],
'email' =>   $_POST['email_field']
];
 
 
update_post_meta( $post_id,'_pdetails_key', $personal_details );
}
 
}
$personal_info = new PersonalDetails();

If you hit refresh and check inside “Add New Post” section in WordPress. Warning displays like the image below.

Step 2 :- How to Solve the Issue

While creating Meta-Box inside the class, All the function inside the class needs to get referenced from the class.

Taking OOP concept, we cannot reference the function directly inside a class that’s why we need to use “$this” pointer to represent the function inside the class.

If you take a look at the function above, we have not referenced the function with this pointer inside a class, so its not working and giving us the warning message.

To fix it just reference it with $this pointer.  Look at the code below where you need to make the changes.

Reference function with $this pointer
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* Reference with $this pointer in constructor
*/
public function __construct() {
add_action('add_meta_boxes',array($this,'pdetails_meta_box'));
add_action('save_post',array($this,'pdetails_save'));
}
 
/**
* Reference with $this pointer for callback function
*/
 
function pdetails_meta_box() {
add_meta_box('pdetails','Personal Details',array($this,'pdetails_callback'),'post');
}

If it solved your problem. Please feel free to share, else you can drop in comments or contact us via contact page.

SHARE ON
Buffer

Enjoyed this article?

Like us on

Others call_user_func_array () expects parameter 1 to be a valid callback class wp hook php call_user_func_array() expects parameter 1 to be a valid callback function not found function not found or invalid function name public_html/wp-includes/class-wp-hook.php on line 286 public_html/wp-includes/class-wp-hook.php on line 298 warning call_user_func_array() expects parameter 1 to be a valid callback wordpress wordpress call_user_func_array >() expects parameter 1 to be a valid callback wp-includes/class-wp-hook.php on line 286

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.