0

i was following atutorial on how to make a custom metabox to wordpress post but its giving errors

here is my full code

//Add Metabox
function diwp_custom_metabox(){
    //add_meta_box(metabox_id,metabox name,callback function,metabox location(page,post,custom post etc),context-(normal,side,advanced),piority())
    add_meta_box('diwp-metabox','My Custom Metabox','diwp_post_metabox_callback','post','normal');
}

//add action
add_action('add_meta_boxes','diwp_custom_metabox');
//metabox callback function

function diwp_post_metabox_callback(){
    // echo 'hi i am diwp metabox';
    ?>
    <div class="row">
      <div class="label">Post Reading Time</div>
      <div class="fields">
        <input type="text" name="_diwp_reading_time" value="<?php get_post_meta($post->ID,'post_reading_time',true); ?>">
      </div>
    </div>
    <?php
}

function diwp_save_custom_metabox(){
    // update_post_meta($post_id,$meta_key,$meta_value,$prev_value);
    global $post;
    if(isset($_POST['_diwp_reading_time'])){
        update_post_meta($post->ID,'post_reading_time',$_POST['_diwp_reading_time']);
    }
}

add_action('save_post','diwp_save_custom_metabox');

error screen shot enter image description here

any help will be appreciated ,thanks

1 Answer 1

0

The error there clearly says that there's an undefined variable (post) in your metabox callback function.

So you need to define $post in your function head:

// The $post variable is passed by WordPress.
function diwp_post_metabox_callback( $post )

And you should also do the same to diwp_save_custom_metabox() function (which is hooked to save_post):

// Here, $post is the second parameter.
function diwp_save_custom_metabox( $post_ID, $post ) {
    // ...
}

// Don't forget to set the 4th parameter to 2:
add_action( 'save_post', 'diwp_save_custom_metabox', 10, 2 );

Or alternatively, use get_post() and not the global call:

// Here, $post is the second parameter.
function diwp_save_custom_metabox( $post_ID ) {
    $post = get_post( $post_ID ); // like this
//  global $post;                 // not this

    // ...
}

add_action( 'save_post', 'diwp_save_custom_metabox' );
4
  • hello thanks for reply and code works but can you please explain what 10 and 2 means in this line add_action( 'save_post', 'diwp_save_custom_metabox', 10, 2 ); ..i have seen that im few tutorials but didnt understand what that means? and to output the meta field data on front end i am using this code echo get_post_meta( $post->ID, 'post_reading_time', true ); is it ok?
    – Sash_007
    Commented Oct 10, 2020 at 3:54
  • Yes, that's fine. And check out the docs for add_action() here, but the 10 means the callback's priority - if there were 4 callbacks hooked to save_post, that priority determines the position of your own callback, e.g. is it the first or last one that executes. And the 2 is the number of args your callback accepts, but you need to check the hook docs (e.g. this for save_post) to know all the args for that specific hook.
    – Sally CJ
    Commented Oct 11, 2020 at 6:13
  • You should also validate, sanitize and escape user data, e.g. use esc_attr() with the <input>'s value and esc_html() when displaying the value, e.g. echo esc_html( get_post_meta( ... ) ).
    – Sally CJ
    Commented Oct 11, 2020 at 10:35
  • ok i have used wp nonce along with esc_html let me know if its ok ..check here ... pastebin.com/d3Y4vS3x
    – Sash_007
    Commented Oct 16, 2020 at 4:08

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.