programing

admin_init 후크콜백 내부의 커스텀 포스트 타입을 검출하는 방법

closeapi 2023. 3. 17. 21:30
반응형

admin_init 후크콜백 내부의 커스텀 포스트 타입을 검출하는 방법

아니면 다른 훅으로 할까요?주로 admin에 있을 때만 몇 가지 작업을 수행하려고 합니다.또, 커스텀 투고에 대해서도 몇 가지 작업을 실시합니다.

이 방법은 간단합니다.

/**
 * Based on http://wordpress.stackexchange.com/a/59999/12615
 */

add_action( 'admin_head', 'wpse_59652_list_terms_exclusions' );

function wpse_59652_list_terms_exclusions() {
    global $current_screen;

    if( 'post' != $current_screen->post_type )
        return;

    // Do your stuff
}

기타:

/**
 * Based on http://wordpress.stackexchange.com/a/54279/12615
 */

add_action( 'admin_notices', 'wpse_54258_display_error_message' );

function wpse_54258_display_error_message() { 
    global $post;
    if( !isset( $post ) || 'page' != $post->post_type )
        return;

    ?>
        <div class="error fade">This is a "page" post type</div>
    <?php
}

그리고 복잡한 문제:

/***
  * Conditional enqueue of scripts according to Admin page
 * Based on http://wordpress.stackexchange.com/a/9095/12615
 */

add_action('admin_init', 'wpse_9080_admin_init');

function wpse_9080_admin_init() {
    global $pagenow;
    global $firephp; // Using FirePHP for debugging - Remove if library not included
    if ( 'edit.php' == $pagenow) {
        if ( !isset($_GET['post_type']) ) {
            $firephp->log('I am the Posts listings page');  
        }
        elseif ( isset($_GET['post_type']) && 'page' == $_GET['post_type'] ) {
            // Will occur only in this screen: /wp-admin/edit.php?post_type=page
            $firephp->log('I am the Pages listings page');  
        }
    }
    if ('post.php' == $pagenow && isset($_GET['post']) ) {
        // Will occur only in this kind of screen: /wp-admin/post.php?post=285&action=edit
        // and it can be a Post, a Page or a CPT
        $post_type = get_post_type($_GET['post']);
        $firephp->log($post_type);
        if ( 'post' == $post_type ) {
            $firephp->log('I am editing a post');   
        }
        elseif ( 'page' == $post_type) {
            $firephp->log('I am editing a page');   
        }
        elseif ( 'movie' == $post_type) {
            $firephp->log('I am editing a custom post type');   
        }
    }

    if ('post-new.php' == $pagenow ) {
        // Will occur only in this kind of screen: /wp-admin/post-new.php
        // or: /wp-admin/post-new.php?post_type=page
        if ( !isset($_GET['post_type']) ) {
            $firephp->log('I am creating a new post');  
        }
        elseif ( isset($_GET['post_type']) && 'page' == $_GET['post_type'] ) {
            $firephp->log('I am creating a new page');  
        }
        elseif ( isset($_GET['post_type']) && 'movie' == $_GET['post_type'] ) {
            $firephp->log('I am creating a new custom post type');  
        }
    }   
}

여기에 문제없이 사용할 수 있는 기능이 하나 있습니다.

function admin_post_type () {
    global $post, $parent_file, $typenow, $current_screen, $pagenow;

    $post_type = NULL;

    if($post && (property_exists($post, 'post_type') || method_exists($post, 'post_type')))
        $post_type = $post->post_type;

    if(empty($post_type) && !empty($current_screen) && (property_exists($current_screen, 'post_type') || method_exists($current_screen, 'post_type')) && !empty($current_screen->post_type))
        $post_type = $current_screen->post_type;

    if(empty($post_type) && !empty($typenow))
        $post_type = $typenow;

    if(empty($post_type) && function_exists('get_current_screen'))
        $post_type = get_current_screen();

    if(empty($post_type) && isset($_REQUEST['post']) && !empty($_REQUEST['post']) && function_exists('get_post_type') && $get_post_type = get_post_type((int)$_REQUEST['post']))
        $post_type = $get_post_type;

    if(empty($post_type) && isset($_REQUEST['post_type']) && !empty($_REQUEST['post_type']))
        $post_type = sanitize_key($_REQUEST['post_type']);

    if(empty($post_type) && 'edit.php' == $pagenow)
        $post_type = 'post';

    return $post_type;
}

유연하고, 빠르고, 정확하도록 만들었습니다.필요에 따라 확장할 수 있습니다.

언급URL : https://stackoverflow.com/questions/11124525/how-to-detect-custom-post-type-inside-admin-init-hook-callback

반응형