0

I am trying to create an editor meta box and I have been following tips found in other similar questions without luck in getting the meta box to save and display its content. The meta box shows but when I write anything it doesn't get saved. Please help! I've been at this for hours without any luck.

Here is my code at the moment:

add_action( 'add_meta_boxes', 'add_educadme_related_resources_metabox' );   
// Add the Custom Fields Meta Boxe
function add_educadme_related_resources_metabox() {

    add_meta_box('educadme_related_resources', 'معلومات إضافية', 'display_educadme_related_resources', 'lesson', 'normal', 'high');

}


// Display The Custom Fields Meta Metabox
function display_educadme_related_resources( $post ) {

    // Get the resources data if its already been entered
    $educadme_resources = get_post_meta($post->ID, 'related_resources', true);
    wp_editor( $educadme_resources, 'related_resources');

}

add_action( 'save_post', 'save_educadme_related_resources' );
// Save Function    
function save_educadme_related_resources( $post_id ){
    // Is the user allowed to edit the post or page?
    if ( !current_user_can( 'edit_post', $post_id ))
        return;
    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // If empty don't do anything (don't save)
    $related_resources = get_post_meta($post_id, 'related_resources', true);
    if(!$related_resources OR $related_resources == '') return;

    // OK, authenticated: we need to find and save the data
    if ( isset ( $_POST['related_resources'] ) ) {
        update_post_meta( $post_id, 'related_resources', $_POST['related_resources'] );
    }

}
0

The problem was with this line:

 if(!$related_resources OR $related_resources == '') return;

I changed it to:

 if($related_resources == '') return;

And it started working properly. I hope others can make use of the code too. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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