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