Is there an “Add Page” hook?

I’m looking for the action hook corresponding to the click on the “Add page” link. Any idea?

Thanks !

Related posts

Leave a Reply

2 comments

  1. If you want to target the admin page that displays the post editor, you’ll likely need to hook to two places whether it’s for a script or style.

    You’d use the top two for scripts and the bottom two for styles.

    // Script action for the post new page    
    add_action( 'admin_print_scripts-post-new.php', 'example_callback' );
    
    // Script action for the post editting page    
    add_action( 'admin_print_scripts-post.php', 'example_callback' );
    
    // Style action for the post new page    
    add_action( 'admin_print_styles-post-new.php', 'example_callback' );
    
    // Style action for the post editting page 
    add_action( 'admin_print_styles-post.php', 'example_callback' );
    

    If you wanted to target a particular post type, simply global $post_type inside your callback function, like so..

    function example_callback() {
        global $post_type;
        // If not the desired post type bail here.
        if( 'your-type' != $post_type )
            return;
    
        // Else we reach here and do the enqueue / or whatever
    }
    

    If you’re enqueuing scripts(not styles) specifically there is a hook that runs earlier called admin_enqueue_scripts which passes on the hook as the first arg, so you could also do it like this for scripts..(if you were hooking onto admin_enqueue_scripts instead of the two admin_print_scripts actions above).

    function example_callback( $hook ) {
        global $post_type;
    
        // If not one of the desired pages bail here.
        if( !in_array( $hook, array( 'post-new.php', 'post.php' ) ) )
            return;
    
        // If not the desired post type bail here.
        if( 'your-type' != $post_type )
            return;
    
        // Else we reach here and do the enqueue / or whatever
    }
    

    These hooks exist exactly for this type of thing, you shouldn’t need to fire things as early as admin_init unless your specific use case dictates a requirement to. If you’re unsure, chances are you don’t need to fire your code that early.

  2. you can use the admin_init hook and add you conditionals to page something like

    add_action('admin_init','load_my_code');
    function load_my_code() {
      global $typenow;
      if (empty($typenow) && !empty($_GET['post'])) {
        $post = get_post($_GET['post']);
        $typenow = $post->post_type;
      }
      if (is_admin() && $typenow=='page') {
        //do your stuff here
      }
    }