显示但不保存的编辑器元框

时间:2015-01-01 作者:Dr.Hariri

我正在尝试创建一个编辑器元框,我一直在遵循其他类似问题中找到的技巧,但没有幸运地让元框保存并显示其内容。元框显示,但当我写入任何内容时,它不会被保存。请帮忙!我已经干了几个小时了,一点运气都没有。

这是我目前的代码:

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\'] );
    }

}

1 个回复
SO网友:Dr.Hariri

这条线路存在问题:

 if(!$related_resources OR $related_resources == \'\') return;
我将其更改为:

 if($related_resources == \'\') return;
它开始正常工作。我希望其他人也能使用该代码。谢谢

结束