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:

1
2
3
4
$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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
 
/*
Plugin Name: Testing Page/Post Load actions
Description: It's a test.
Version: 0.1
Author: Simon Wheatley
*/
  
/*  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!


Comments

9 responses to “Eeek! The load-page-new.php and load-page.php actions have vanished!”

  1. Simon – great work fella, this is just what I needed! Thanks so much for putting this info up, it couldn’t have been timed better!

  2. 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.

  3. […] yet due to other more pressing projects, but just following on form this I have just noticed that Simon Wheatley has just posted a very timely blog post about this – may be of use if someone else has a similar issue! Reply With Quote   […]

  4. A shorter way would be


    $post_type = isset($_GET['post_type']) ? $_GET['post_type'] : 'post';

    1. Simon Wheatley Avatar
      Simon Wheatley

      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]

  5. Simon Wheatley Avatar
    Simon Wheatley

    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!

  6. 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!

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

  8. Greg WOods Avatar
    Greg WOods

    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 Reply to Jonny Cancel reply

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.