18

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?

29

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
}
  • 12
    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
    +1 Thought the only admin conditional is is_admin ;). Are there more? – kaiser Apr 24 '12 at 9:25
0

I prefer get_current_screen(), as it is much simpler:

$screen = get_current_screen();
  if ($screen->base == "post") {
}

This function returns an object that includes the screen’s ID, base, post type, and taxonomy, among other data points.

(Codex)

0

I wanted to enqueue script and styles only on specific new/edit post type screens. Created a function to check if i am on edit/new-post screen of the given CPT.

/**
 * Check if 'edit' or 'new-post' screen of a 
 * given post type is opened
 * 
 * @param null $post_type name of post type to compare
 *
 * @return bool true or false
 */
function is_edit_or_new_cpt( $post_type = null ) {
    global $pagenow;

    /**
     * return false if not on admin page or
     * post type to compare is null
     */
    if ( ! is_admin() || $post_type === null ) {
        return FALSE;
    }

    /**
     * if edit screen of a post type is active
     */
    if ( $pagenow === 'post.php' ) {
        // get post id, in case of view all cpt post id will be -1
        $post_id = isset( $_GET[ 'post' ] ) ? $_GET[ 'post' ] : - 1;

        // if no post id then return false
        if ( $post_id === - 1 ) {
            return FALSE;
        }

        // get post type from post id
        $get_post_type = get_post_type( $post_id );

        // if post type is compared return true else false
        if ( $post_type === $get_post_type ) {
            return TRUE;
        } else {
            return FALSE;
        }
    } elseif ( $pagenow === 'post-new.php' ) { // is new-post screen of a post type is active
        // get post type from $_GET array
        $get_post_type = isset( $_GET[ 'post_type' ] ) ? $_GET[ 'post_type' ] : '';
        // if post type matches return true else false.
        if ( $post_type === $get_post_type ) {
            return TRUE;
        } else {
            return FALSE;
        }
    } else {
        // return false if on any other page.
        return FALSE;
    }
}

to use the function, pass the post type name.

/**
 * return true if 'edit' or 'new-post' screen of 'cpt_name' is opened
 */
if ( is_edit_or_new_cpt('cpt_name') ) {
    // do something
}
0

i have this function

if(!function_exists('is_post_edit_page')){
    function is_post_edit_page() {
        static $result = null;
        if ( $result === null ) {
            $result = false;
            if ( is_admin() ) {
                if (
                    empty( $_POST )
                    &&
                    isset( $_GET['action'] )
                    &&
                    $_GET['action'] === 'edit'
                    &&
                    isset( $_GET['post'] )
                ) {
                    // Display Edit Post page
                    $result = true;
                } elseif (
                    isset( $_POST['action'] )
                    &&
                    $_POST['action'] === 'editpost'
                    &&
                    isset( $_POST['post_type'] )
                    &&
                    isset( $_POST['post_ID'] )
                    &&
                    strpos( wp_get_referer(), 'action=edit' ) !== false
                ) {
                    // Submit Edit Post page
                    $result = true;
                }
            }
        }

        return $result;
    }
}

which returns true in the edit page and false on the other pages...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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