admin管理员组

文章数量:1295696

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?

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 asked Apr 24, 2012 at 5:38 Dipesh KCDipesh KC 9241 gold badge8 silver badges20 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 40

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
}

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)

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
}

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...

本文标签: