4

私はワードプレス用の高度なカスタムフィールドプラグインを使用しています。つまり、一部のページに無効なコンテンツエディターが含まれているため、特定のページでそれらを無効にします。いくつかのウェブサイトで以下のコードを見つけましたが、これによりバックエンドでエラーが発生し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');
    }
}
4

1 に答える 1

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

}
于 2012-08-31T16:45:44.360 に答える