0
votes

I have created a CPT called 'encyclopedia' in WordPress. I am then creating a couple of Meta Fields inside that, just some simple Text Fields, but they are not saving at the moment and I cannot figure out why. Can someone help?

/*
=========================================================================
Custom Meta Fields - English version
=========================================================================
*/

function custom_meta_box_markup()
{
    wp_nonce_field(basename(__FILE__), "meta-box-nonce");

    ?>
        <div>
            <label for="english_version">Description</label>
            <input name="english_version" type="text" value="<?php echo get_post_meta($object->ID, "english_version", true); ?>">
        </div>


<?php }

function add_custom_meta_box()
{
    add_meta_box("english_version", "English Version", "custom_meta_box_markup", "encyclopedia", "advanced", "high", null);
    //add_meta_box("german-version", "German Version", "custom_meta_box_markup", "encyclopedia", "advanced", "high", null);
}

add_action("add_meta_boxes", "add_custom_meta_box");

function save_custom_meta_box($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;
    }

    $slug = "post";
    if($slug != $post->post_type){
        return $post_id;
    }

    $meta_box_text_value = "";

    if(isset($_POST["english_version"]))
    {
        $meta_box_text_value = $_POST["english_version"];
    }   
    update_post_meta($post_id, "english_version", $meta_box_text_value);

}

add_action("save_post_encyclopedia", "save_custom_meta_box", 10, 3);

The relevant code is above and I currently have it stored in the functions.php file of my child theme.

Thanks

1
value='<?php echo get_post_meta($object->ID, "english_version", true); ?>'> - Stender
I think your concatanation is wrong there - Stender
You have added, $slug = 'post'; inside save_custom_meta_box function. This should be $slug = 'encyclopedia';. - тнє Sufi
Thanks @тнєSufi, I changed this like you suggested but no change at all - damienoneill2001
Thanks @Stender I'm not sure exactly what you mean by this. Could you explain a little further please? - damienoneill2001

1 Answers

1
votes

As per my comment on your question, after fixing the post slug, you still have something wrong on the code.

On your meta box output function custom_meta_box_markup, you are using get_post_meta($object->ID, "english_version", true) without defining $object.

I have tested your code and your data is being saved in DB. But as $object->ID is returning nothing, it's not showing anything on the input textfield. custom_meta_box_markup receives a $post object, which you have missed. Update your code like this:

function custom_meta_box_markup($post) {
    wp_nonce_field(basename(__FILE__), "meta-box-nonce");

    ?>
    <div>
        <label for="english_version">Description</label>
        <input name="english_version" type="text" value="<?php echo get_post_meta($post->ID, "english_version", true); ?>">
    </div>


<?php }