Reputation: 3489
I inherited a Wordpress plugin that makes custom fields available when editing pages and/or posts. It adds slightly different content for pages than it does for posts. The plugin determines page or post with the following:
if(preg_match('|post.php|i', $_SERVER['SCRIPT_NAME']) || preg_match('|post-new.php|i', $_SERVER['SCRIPT_NAME'])) {
add_meta_box('qaws_' . $cg ,$group, 'qaws_admin_meta', 'post', 'advanced', 'core');
} elseif(preg_match('|page.php|i', $_SERVER['SCRIPT_NAME']) || preg_match('|page-new.php|i', $_SERVER['SCRIPT_NAME'])) {
add_meta_box('qaws_' . $cg ,$group, 'qaws_admin_meta', 'page', 'advanced', 'core');
}
This worked fine in older versions of Wordpress because they used post.php in the URL when editing posts and page.php when editing pages. Now, however, Wordpress does not do this. Both posts and pages are edited with a URL that looks like: post.php?post=4&action=edit
so the only possible identifier in the URL is the post number.
So, how would I identify whether or not I am editing a page or a post? Is there a Wordpress function that can readily tell me this? Is the only option to grab the post number and somehow check it to see what it is?
How would you approach this?
EDIT:
Here is what I did...
rather than doing the preg_match I retrieved the global $id, used get_all_page_ids()
to get a list of all of the pages and matched the $id to the ids in the get_all array. If there is a match, do the page add_meta_box, if not do the post one.
I also moved the checking out of a loop it was in which saved a bunch on time to render the page. Now I set a flag right at the start and in the loop the renders these meta_boxes just check to see if that flag isset.
Upvotes: 0
Views: 17692
Reputation: 21
in file /wp-admin/includes/screen.php line 530 @see set_current_screen()
global $typenow;
$typenow = $this->post_type;
Here is what I use:
if ($typenow == 'post') {
//do something for post screens only.
}
or
if ($typenow == 'page') {
//do something for page screens only.
}
and if need to expect some types
if (!$typenow == 'page' AND !$typenow == 'post') {
//do something for page screens only.
}
Upvotes: 2
Reputation: 193
you can try using the get_current_screen() function, to check if you are editing from post or page, you just have to set a condition
$screen = get_current_screen();
if($screen->post_type == 'page'){
//do something for page screens only
}elseif($screen->post_type == 'post'){
//do something for post screens only.
}
Let me know if it works for you.
Upvotes: 2
Reputation: 1735
As of WP 3.1 you can use get_current_screen()
to get complete information about current screen shown at back-end : http://codex.wordpress.org/Function_Reference/get_current_screen
Upvotes: 13
Reputation: 1420
Here is what I use:
if( strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php') || strstr($_SERVER['REQUEST_URI'], 'wp-admin/post.php') ) {
//I'm editing a post, page or custom post type
}else{
//I'm doing none of the above.
}
Good luck!
Upvotes: 1
Reputation: 4010
It would be better to use get_post_type($id)
http://codex.wordpress.org/Function_Reference/get_post_type
Upvotes: 2
Reputation: 464
Is the function is_page() available to your plugin?
See here: http://codex.wordpress.org/Conditional_Tags
Upvotes: 3