Eeek! The load-page-new.php and load-page.php actions have vanished!

So WordPress 3.0 is out, and some of the useful admin area load-* hooks that I use have vanished or changed. To refresh your memory, there are individual and specific hooks which run whenever WordPress loads an admin page. These hooks are named after the PHP file in the URL to the admin page, for example the URL to create a new post is post-new.php so the action you can hook when this page is loaded is “load-post-new.php“. All good. Very handy.

In WordPress 3.0 the URLs to create a post and edit a post are now all post-new.php and post.php, with the post type being passed in a GET parameter like so: post-new.php?post_type=page… i.e. load-page-new.php and load-page.php action hooks have both vanished! Never fear, there are two ways around this problem, read on…

The obvious way to cope with this is to hook the load-post-new.php and load-post.php actions and include some conditional code which looks to the GET parameter in the URL, something like this perhaps:

$get_post_type = @ $_GET[ 'post_type' ];
if ( ! $get_post_type ) {
$get_post_type = 'post';
}

Alternatively you could access a global variable called $typenow, as in this little demo plugin I’ve whipped up:

<?php

/*
Plugin Name: Testing Page/Post Load actions
Plugin URI: http://simonwheatley.co.uk/wordpress/testing-page-post-load-actions
Description: It's a test.
Version: 0.1
Author: Simon Wheatley
Author URI: http://simonwheatley.co.uk/ 
*/
 
/*  Copyright 2010 Simon Wheatley

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

/**
 * Hooks the WP init action to add a custom post type, so we
 * can see what happens when we load the pages to create a new
 * custom post type post, or edit an existing one.
 *
 * @return void
 * @author Simon Wheatley
 **/
function ttpla_init() {
    register_post_type( 'exhibition', array(
        'capability_type' => 'post',
        'hierarchical' => false,
        'labels' => array(
                'add_new' => __( 'Add New' ),
                'add_new_item' => __( 'Add New Exhibition', 'hoi' ),
                'edit_item' => __( 'Edit Exhibition', 'hoi' ),
                'name' => __('Exhibitions', 'hoi' ),
                'new_item' => __( 'New Exhibition', 'hoi' ),
                'not_found' => __( 'No exhibitions found', 'hoi' ),
                'not_found_in_trash' => __( 'No exhibitions found in Trash', 'hoi'),
                'search_items' => __( 'Search Exhibitions', 'hoi' ),
                'singular_name' => __( 'Exhibition', 'hoi' ),
                'view_item' => __('View Exhibition', 'hoi' ),
            ),
        'menu_position' => 20,
        'public' => true,
        'rewrite' => array( 'slug' => 'exhibition' ),
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-thumbnails' ),
    ) );
}

/**
 * Hooks the WP load-post.php and load-post-new.php actions.
 *
 * @return void
 * @author Simon Wheatley
 **/
function ttpla_load() {
    global $typenow;
    error_log( "type now: $typenow" );
}
add_action( 'load-post-new.php', 'ttpla_load' );
add_action( 'load-post.php', 'ttpla_load' );

Happy coding!

Join the Conversation

9 Comments

  1. I was editing some plugins of mine just yesterday to cope with this. I went with the $_GET[‘post_type’] route, but the $typenow variable is a bit more elegant (just).

    Also worth noting that a similar change has happened with the Categories and Tags screens. For example, load-categories.php is now load-edit-tags.php with a ‘taxonomy’ query string.

    To round it off, the names of custom columns for tables in the admin screens are related, so for posts and taxonomies have changed too. If you’re adding a custom column on the Categories screen it’s now “manage_edit-category_columns”, on the Tags screen it’s the good-luck-guessing-the-name-correctly “manage_edit-post_tag_columns”, and on the posts screen it’s now “manage_posts_custom_column” (yay for naming consistencies).

    And there I was thinking that WordPress 3.0 wasn’t going to cause me a lot of work.

    1. Agreed, that is neatly on one line… but I find ternary operators are pretty confusing, so I avoid them (particularly when explaining things). ;)

      [edit: corrected tertiary to ternary]

  2. I’ve put a suggested patch (#14083) in to add backwards compatibility, and to propose some new dynamically constructed hooks based on the content type and taxonomy type (e.g. “load-edit-tags.php-type-post-tax-category”, “load-post.php-type-event”, etc).

    If you guys had time to review it, that would be handy!

  3. Booo – so close, yet so far!

    This works fine when:
    – creating a new post
    – editing a post
    – creating a new page

    HOWEVER, when you edit an existing page, both $typenow and $_GET[‘post_type’] both return ‘post’

    Looks like a bug – boo!

  4. Nope, my mistake – both $typenow and $_GET[‘post_type’] return nothing when editing an existing page (fine when creating a new one though)

  5. I’m new to wordpress, but need to create an admin page plugin, and have learnt a lot from your blog posts. In the example plugin linked to in this page, I get the general idea of what the code is doing, but I’m confused by how ttpla_init() is ever called. Any additional insights would be appreciated.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.