how to limit edit_form_after_title hook to page and post edit only?

Personally I’d use a different approach, because @Shazzad‘s solution seems too much global dependent, and @s_ha_dum‘s needs 2 hooks instead of one.

I’d use get_current_screen function to get a WP_Screen object, then I’d look at its property to run (or not) something after the title:

function do_something_after_title() {
    $scr = get_current_screen();
    if ( ( $scr->base !== 'post' && $scr->base !== 'page' ) || $scr->action === 'add' )
        return;
    echo '<h2>After title only for post or page edit screen</h2>';
}

add_action( 'edit_form_after_title', 'do_something_after_title' );

Leave a Comment