可以在wordpress后端的某些页面模板上禁用内容编辑器吗?

时间:2012-08-31 16:05:03

标签: wordpress

我正在使用wordpress的高级自定义字段插件,这意味着我的一些页面中已经不存在内容编辑器,所以我想在某些页面上禁用它们。我确实在某些网站上找到了以下代码,但这会在后端Undefined index: postUndefined index: post_ID

中产生错误
add_action( 'admin_init', 'hide_editor' );

function hide_editor() {
    // Get the Post ID.
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    if( !isset( $post_id ) ) return;

    // Get the name of the Page Template file.
    $template_file = get_post_meta($post_id, '_wp_page_template', true);

    if($template_file == 'contact.php'){ // edit the template name
        remove_post_type_support('page', 'editor');
    }
}

1 个答案:

答案 0 :(得分:7)

使用PHP函数isset()来修复此错误:

add_action( 'admin_init', 'hide_editor' );

function hide_editor() {

        // Get the Post ID.
        if ( isset ( $_GET['post'] ) )
        $post_id = $_GET['post'];
        else if ( isset ( $_POST['post_ID'] ) )
        $post_id = $_POST['post_ID'];

    if( !isset ( $post_id ) || empty ( $post_id ) )
        return;

    // Get the name of the Page Template file.
    $template_file = get_post_meta($post_id, '_wp_page_template', true);

    if($template_file == 'contact.php'){ // edit the template name
        remove_post_type_support('page', 'editor');
    }

}