How to determine whether we are in add New page/post/CPT or in edit page/post/CPT in wordpress admin?

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 <[email protected]>
 * 
 * @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 existing 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
}

Leave a Comment