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.

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

1 Answer 1

up vote 2 down vote accepted

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
    }
}
share|improve this answer
    
bloody hell...I knew that, "admin_enqueue_scripts" grrr it's always the little things...but I didn't know about passing the hook...awesome! Working now! Thanks Stephen. –  Brian Thornton Apr 22 '12 at 20:32

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.