Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This seems like simple thing but I need way to determine whether the current screen is for Add New or Edit ( a sort of wordpress admin conditional tag). Is there a built in function for this already, or ... any idea how to accomplish?

share|improve this question
add comment

1 Answer

up vote 9 down vote accepted

here is a function that i have:

/**
 * is_edit_page 
 * function to check if the current page is a post edit page
 * 
 * @author Ohad Raz <admin@bainternet.info>
 * 
 * @param  string  $new_edit what page to check for accepts new - new post page ,edit - edit post page, null for either
 * @return boolean
 */
function is_edit_page($new_edit = null){
    global $pagenow;
    //make sure we are on the backend
    if (!is_admin()) return false;


    if($new_edit == "edit")
        return in_array( $pagenow, array( 'post.php',  ) );
    elseif($new_edit == "new") //check for new post page
        return in_array( $pagenow, array( 'post-new.php' ) );
    else //check for either new or edit
        return in_array( $pagenow, array( 'post.php', 'post-new.php' ) );
}

Usage the usage is simple just like any other conditional tag , few examples:

check for new or edit page:

if (is_edit_page()){
   //yes its an edit/new post page
}

check for new post page:

if (is_edit_page('new')){
   //yes its an new post page
}

check for edit post page:

if (is_edit_page('edit')){
   //yes its an new post page
}

combine this with a $typenow global to check for a specific post type edit page:

global $typenow;
if (is_edit_page('edit') && "Post_Type_Name" == $typenow){
   //yes its an edit page  of a custom post type named Post_Type_Name
}
share|improve this answer
1  
Thanks Alternatively I found another global variable $current_screen. global $current_screen; if( $current_screen->post_type =='CPT' && $current_screen->action == 'add'){ //task } –  Dipesh Kc Apr 24 '12 at 8:05
    
+1 Thought the only admin conditional is is_admin ;). Are there more? –  kaiser Apr 24 '12 at 9:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.