0

I'm teaching myself from a plugin tutorial dvd, everything was going fine. I've setup custom_post_types and the metaboxes with no issues. The issue i have is a fatal error when i update/publish the post

Fatal error: Uncaught ArgumentCountError: Too few arguments to function mdb_save_metaboxes(), 1 passed in C:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php on line 310 and exactly 3 expected in C:\xampp\htdocs\wordpress\wp-content\plugins\maxi_metaboxes\maxi_metabox_types.php:45 Stack trace: #0

To check i was not adapting the code incorrectly, I back tracked and used the tutors metaboxes and code that worked for him, but it still doesn't.

I have a good idea it is to do with the verify_nonce check in the mdb_save_metaboxes function. Any help will be greatly accepted.

<?php

/*
 * Plugin Name:      Driver Bible metaboxes
 * Plugin URI:        
 * Description:      Adds metaboxes to Driver Bible
 * Version:           1.0
 * Author:            Fuzzy
 * Author URI:        
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
  */

  function mdb_add_metaboxes() {
    add_meta_box('mdb-metaboxes', 'Dealer Info', 'mdb_metaboxes_container', 'dealers', 'normal', 'high', 'null');
  }
add_action('add_meta_boxes', 'mdb_add_metaboxes');

//Add metaboxes
function mdb_metaboxes_container($post) {

    wp_nonce_field(basename(__FILE__), "meta-box-nonce");
    ?> 
    <div>
        <label for="input-metaboxes">Cal:</label>
        <input type="text" name="input-metabox" value="<?php echo get_post_meta($post->ID, "input-metabox", true); ?>">
        <br/>
        <label for="textarea-metaboxes">text area:</label>
        <textarea type="text" name="textarea-metabox"><?php echo get_post_meta($post->ID, "textarea-metabox", true); ?></textarea>
        <br/>

       
    </div>
<?php
}

//Saving metabox updates
//================================================================================

//permissions check
add_action('save_post', 'mdb_save_metaboxes');
function mdb_save_metaboxes($post_id, $post, $update){
    if (!isset($_POST["meta-box-nonce"]) || !wp_verify_nonce($_POST["meta-box-nonce"], basename(__FILE__)))
        return $post_id;

    if(!current_user_can("edit_post", $post_id))
        return $post_id;

    if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
        return $post_id;

// save

$input_metabox ='';
$textarea_metabox ='';

if(isset($_POST["input-metabox"])) {
    $input_metabox = $_POST["input-metabox"];
}
update_post_meta($post_id, "input-metabox", $input_metabox );

if(isset($_POST["textarea-metabox"])) {
    $textarea_metabox = $_POST["textarea-metabox"];
}
update_post_meta($post_id, "textarea-metabox", $textarea_metabox );

}

1 Answer 1

0

You are getting that fatal error because your custom mdb_save_metaboxes function is expecting to receive 3 parameters (function mdb_save_metaboxes($post_id, $post, $update)), but WordPress only passed 1 parameter, because you did not tell WordPress that it needs to pass 3 parameters.

So to fix the issue, set the fourth parameter for add_action() like so:

// If you do this, only 1 parameter will be passed to mdb_save_metaboxes:
add_action('save_post', 'mdb_save_metaboxes');

// So do this instead, to ensure that 3 parameters passed to the function.
add_action('save_post', 'mdb_save_metaboxes', 10, 3);
2
  • That makes total sense.Thank you so much Sally works well now, just got to put all my boxes back in :-}
    – fuzzy2023
    Commented Mar 29, 2023 at 14:22
  • Glad it helped! But just a friendly note, remember to sanitize and validate the inputs, and remember also, that if you want to group similar things together, taxonomy is the proper approach for that (e.g. a category named comedy, as opposed to a custom field or meta named comedy, for posts about comedy) :)
    – Sally CJ
    Commented Mar 29, 2023 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.