WordPress

特定の画面でのスクリーンIDの取得(表示)と条件分岐

'25.02.03

取得と確認(表示)

add_action('admin_notices', function () {
    $screen = get_current_screen();
    if ($screen) {
        echo '<div class="class">';
        echo '<p>Current Screen ID: <strong>' . esc_html($screen->id) . '</strong></p>';
        echo '</div>';
    }
});

条件分岐

add_action('current_screen', function ($screen) {
    // スクリーン ID を取得
    $screen_id = $screen->id;
    if ($screen_id === 'post') {
        // 投稿編集画面でのみ動作させる
    }
    if ($screen_id === 'page') {
        // 固定ページ編集画面でのみ動作させる
    }
});

例)https://1os.work/wp-admin/admin.php?page=slide_setting

 =>toplevel_page_slide_setting

サブメニューの場合

 {parent_slug}_page_{slug}

例)[slide_setting]がtools.phpに追加されたサブメニューの場合

 =>tools_page_slide_setting

(View:16)