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.

I've got some jQuery that interacts with items on the post and page editor screens. However, the script is also loading on other admin screens that are driven by post.php

Is there a way to target the post/page editor screen exclusively?

I'm currently using:

global $pagenow;
if( 'post.php' == $pagenow )
{
  //jQuery blocks go here
}

However, since the jQuery has refences to objects that only appear on the post/page editor screens, I'm getting console errors on other pages that are driven by post.php

For example, this code throws a console error of "Uncaught TypeError: Cannot read property 'length' of undefined":

var charactersMeta = jQuery("#excerpt").val().length;
share|improve this question
    
Do you mean not to load on custom post types? –  Max Yudin Feb 1 '13 at 21:12
    
I only want to load the code when viewing normal post edits and page edits. –  N2Mystic Feb 1 '13 at 21:21
add comment

3 Answers

up vote 3 down vote accepted

Use the WP_Screen object to tell where you are at in the admin instead. Much more convenient.

$screen = get_current_screen();
if ( $screen->id == 'edit-post' ) {
   // you're on the posts screen
}

Note that you have to wait until at least the admin_head hook to run for the current screen to have been determined and the WP_Screen object to therefore have been initialized. The current screen is not yet set at plugin load, or even at init.

You can use this to see the various properties that the screen object will have on various pages in the admin:

add_action('admin_head', 'show_screen_info_in_help_tab');
function show_screen_info_in_help_tab() {
    $screen = get_current_screen();
    $screen->add_help_tab( array(
        'id'      => 'screen',
        'title'   => 'Screen Info',
        'content' => '<pre>' . var_export($screen, true) . '</pre>',
    ) );
}

This will add a new section to the Help dropdown in WordPress, dumping the screen object into it as a "Screen Info" tab. Handy, and makes it easy to figure out the unique properties for each admin screen.

share|improve this answer
    
+1 especially for the help dropdown tip. –  N2Mystic Feb 7 '13 at 5:23
add comment

There are a number of possibilities:

1 - Add checks to your javascript to verify that the element you're trying to interact with exists, for example:

if(jQuery('#excerpt').length > 0) {
    // this won't throw a console error
}

2 - You could change your if-statement to $pagenow == 'post.php' && $_GET['action'] == 'edit' to only include your script on the post edit screen. Never mind, this also targets the edit media screen. You could however use this in combination with option 1 to further narrow down the amount of pages where the script is (needlessly) loaded.

share|improve this answer
    
About option 2, the media edit screen has action=edit too. Don't want to load there. –  N2Mystic Feb 1 '13 at 21:25
    
I see. After some quick testing I would recommend using option 1. The reason you're getting a console error is because you can't use length on jQuery("#excerpt").val() (since it doesn't exist; you can't use val() on 'nothing'). $('#excerpt').length checks how much '#excerpt' nodes there are on the page, which will return 0 if none. –  Sven Feb 1 '13 at 21:33
add comment

You have to check post for post_type:

$post = get_post($_GET['post']);
$post_type = $post->post_type;

if( !empty($_GET['post']) && ('post' == $post_type || 'page' == $post_type) ) {
    //jQuery blocks go here
}

But the right way to use your jQuery is to enqueue admin script:

function wpse_84138_add_script_2_post_edit() {

    global $pagenow
    // don't run on other pages
    if('post.php' != $pagenow)
        return;

    // get current post type
    $post = get_post($_GET['post']);
    $post_type = $post->post_type;

    // Edited: post types array if somebody wants to add custom post types in future
    $post_types = array{'post', 'page'};

    // if is page or post
    if( !empty($_GET['post']) && in_array($post_type, $post_types) ) {
        // check second parameter for right script URL
        wp_enqueue_script(
            'my_jQuery_script',
            get_template_directory_uri().'/admin/js/my_jQuery_script.js',
            array('jquery')
        );
    }

}

add_action('admin_enqueue_scripts', 'wpse_84138_add_script_2_post_edit');

There is one more global $typenow, but seems it isn't useful on post.php.

Edited: post_types array added

share|improve this answer
add comment

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.