TransWikia.com

Conditions for admin get_current_screen action parent_file edit.php?post_type=page

WordPress Development Asked by Jarod Thornton on December 4, 2021

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?

One Answer

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' )
    );
  }
}

Answered by Nathan Johnson on December 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP