0

I have a plugin that requires scripts to load for an admin panel in the page-attributes template drop-down on page post type.

However this script causes a Uncaught TypeError: on post post types which is a correct response and I understand that is not where my focus should turn.

Since this script is only necessary for the page post type, I believe the best solution is to conditionally format my script so it works as such.

Due diligence was taken to find and work this out, but without much luck. In class-wp-screen.php, 95, I find $parent_base, but I do not know how to format this to meet the conditions.

My code to be modified

add_action('admin_enqueue_scripts', 'gp_front_page_script');

function gp_front_page_script() 
{
    if ( isset( $_GET['post']) && isset( $_GET['action']) && $_GET['action'] == 'edit' ) 
    {
        wp_enqueue_script('gp_fp', 
        plugins_url() . '/network-plugins/includes/globals/genpages/front-page.js',
       array('jquery'));
    }
}

How do I format a condition to load a script on edit.php?post_type=page and not any other screen i.e. edit.php?

0

You can use get_current_screen() to get details about the current screen. We want to hook on or after load-edit.php because get_current_screen() is defined just prior to this hook.

By hooking into load-edit.php, we guarantee that this code will only fire on the edit.php page. Then we check the current screen to see if the post type is a page. If it is, then we enqueue the script.

add_action( 'load-edit.php', 'wpse_259909_load_edit' );
function wpse_259909_load_edit() {
  if( 'page' === get_current_screen()->post_type ) {
    add_action( 'admin_enqueue_scripts', 'gp_front_page_script' );
  }
}

function gp_front_page_script() {
  wp_enqueue_script(
    'gp_fp', 
    plugins_url() . '/network-plugins/includes/globals/genpages/front-page.js',
    array( 'jquery' )
  );
}

If you need the script on the individual edit pages, you can add this hook too.

add_action( 'load-page.php', 'wpse_259909_load_edit' );

If you need the script on the new edit pages, you can add this hook too.

add_action( 'load-page-new.php', 'wpse_259909_load_edit' );

I'm not sure why the above code isn't working for you. I added the above code to a blank plugin on a fresh install of WP 4.7.3 and it enqueues the javascript in the head after jQuery.

The load-edit.php fires pretty early in the request, so maybe you're adding the hook too late.

You can use get_current_screen() in the admin_enqueue_scripts hook as well, but you need to make sure you're on an edit screen. So this should work too.

add_action( 'admin_enqueue_scripts', 'gp_front_page_script' );    
function gp_front_page_script() {
  $current_screen = get_current_screen();
  if( 'edit' === $current_screen->parent_base && 'page' === $current_screen->post_type ) {
    wp_enqueue_script(
      'gp_fp', 
      plugins_url() . '/network-plugins/includes/globals/genpages/front-page.js',
      array( 'jquery' )
    );
  }
}
  • I follow the logic in your example. Unless I am overlooking something (very likely), I cannot get it to work. The example code I was working with has been commented out. – Jarmerson Mar 13 '17 at 18:39
  • I appreciate your input, but still no luck. I failed to mention this in my original question and it might have an impact (unsure); I'm running multisite. – Jarmerson Mar 13 '17 at 21:29
  • I am on the correct page per your statement. If I omit if( 'edit' === $current_screen->base && 'page' === $current_screen->post_type ) the code fires. The script loads with the other header scripts such as `jquery-core.js'. It fired off a few from the last to fire. I also changed the priority though that wouldn't have anything to do with it. – Jarmerson Mar 13 '17 at 21:45
  • What WP version are you using? What page are you trying to load the js on? /wp-admin/edit.php?post_type=page - correct? – Nathan Johnson Mar 13 '17 at 21:47
  • 4.7.3 - pasteboard.co/J4onLSDn8.jpg – Jarmerson Mar 13 '17 at 21:49

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.