最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

admin - get_current_screen and the WP_Screen class

programmeradmin5浏览0评论

So I'm trying to figure out how to properly use the get_current_screen. For learning sake, I'm just popping an alert for a specific page. I've looked at .html and the codex on WP_Screen and get_current_screen (hit my max links cause my rep isn't 10 yet)

I thought I was doing this right...but not getting it to work. What I'm hoping for is that when I go to /wp-admin/post-new.php?post_type=product I can do stuff (will be enqueueing a script). Did pretty much a straight copy and changed the post type...but, what am I missing here?

add_action( 'wp_enqueue_scripts', 'rw_enqueue_scripts' );
/**
* Enqueue scripts for 'post' page of 'product' post type only
*
* @return void
*/

function rw_enqueue_scripts()

{

$screen = get_current_screen();

// Check screen base and current post type
if ( 'post' === $screen->base && 'product' === $screen->post_type )
{
    echo "<script type='text/javascript'>alert('worked');</script>

    ";
}
}

Thanx in advance,

Brian

So I'm trying to figure out how to properly use the get_current_screen. For learning sake, I'm just popping an alert for a specific page. I've looked at http://www.deluxeblogtips/2012/01/get-admin-screen-information.html and the codex on WP_Screen and get_current_screen (hit my max links cause my rep isn't 10 yet)

I thought I was doing this right...but not getting it to work. What I'm hoping for is that when I go to /wp-admin/post-new.php?post_type=product I can do stuff (will be enqueueing a script). Did pretty much a straight copy and changed the post type...but, what am I missing here?

add_action( 'wp_enqueue_scripts', 'rw_enqueue_scripts' );
/**
* Enqueue scripts for 'post' page of 'product' post type only
*
* @return void
*/

function rw_enqueue_scripts()

{

$screen = get_current_screen();

// Check screen base and current post type
if ( 'post' === $screen->base && 'product' === $screen->post_type )
{
    echo "<script type='text/javascript'>alert('worked');</script>

    ";
}
}

Thanx in advance,

Brian

Share Improve this question edited Apr 23, 2012 at 17:27 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Apr 22, 2012 at 19:36 Brian ThorntonBrian Thornton 421 gold badge2 silver badges7 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 3

wp_enqueue_scripts is only fired on the front end, and not on admin screens. For loading scripts admin side, you'll want the hook admin_enqueue_scripts.

Passed as an optional argument is the page hook. (Examples include edit.php, for the admin edit page (which lists pages/posts/ cpt posts), post.php when editing a post/ custom post type and post-new.php when creating a new one).

$screen = get_current_screen(); is also available to you, so you can restrict by post type. E.g.:

add_action( 'admin_enqueue_scripts', 'rw_enqueue_scripts' ,10,1);

function rw_enqueue_scripts($hook){

    $screen = get_current_screen();

    // Check screen hook and current post type
    if ( 'post.php' == $hook && 'product' == $screen->post_type ){
        //Load scripts
    }
}

I used this code to make one of the fields on the product page required. But it includes the condition to check the current page is product page before insert the script in the header.

add_action( 'admin_head', 'dcwd_require_weight_field' );
function dcwd_require_weight_field() {
    $screen         = get_current_screen();
    $screen_id      = $screen ? $screen->id : ''; 
    if ( $screen_id == 'product' ) {
?>
<script>
  //Load scripts
</script>
<?php
    }
}
发布评论

评论列表(0)

  1. 暂无评论