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

heelo,
I want to use this great hook edit_form_after_title

it was announced on December 1, 2012:
http://make.wordpress.org/core/2012/12/01/more-hooks-on-the-edit-screen/

Read More

it curreclty hook for :
post-new, post, page-new, page.

how do I make it to work only in edit page/post (only post, page)

thanks all

Related posts

3 comments

  1. 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' );
    
  2. Don’t know if there was any change during the time that passed from the original question but as of 4.0 you get the post as a parameter to the hook and all that is needed to do is to check the post type. If you wonder what happens when creating a new post the answer is that WordPress generates a dummy post if the post type set to the correct type.

    function do_something_after_title( $post ) {
        if( 'post' !== $post->post_type && 'page' !== $post->post_type ) {
            return;
        }
    
        echo '<h2>After title only for post or page edit screen</h2>';
    }
    add_action( 'edit_form_after_title', 'do_something_after_title' );
    
  3. Check post type using $typenow and $pagenow global variables.

    add_action( 'edit_form_after_title', 'myprefix_edit_form_after_title' );
    function myprefix_edit_form_after_title()
    {
        global $typenow, $pagenow;
        if( in_array($typenow, array('post', 'page') ) && $pagenow == 'post.php' )
        {
            echo '<h2>This is edit_form_after_title!</h2>';
        }
    }
    

Comments are closed.