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 ?
- Crete custom Metabox inside a Class and Instantiate it by creating an object.
- 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.
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.
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.
Leave a Reply