Home > Mobile >  wp_enqueue_style for specific pages
wp_enqueue_style for specific pages

Time:09-22

I try to load the css and js files in specific pages. and use this code:

public $pages = array(
    'post', 'post-new', 'profile', 'user-edit',
    'options-general',
);
 public function admin_scripts() {
    $screen = get_current_screen();
    $css_path = 'lib/css/app-admin.css';
    $js_path = 'lib/js/app-admin.js';
    wp_register_style( 'app-admin', APP_URL . $css_path, array(), _ver( $css_path ) );
    wp_register_script( 'app-admin', APP_URL . $js_path, array(), _ver( $js_path ), true );
    
    if ( in_array( $screen->base, $this->pages ) ) {
        wp_enqueue_style( 'app-admin' );
        wp_enqueue_script( 'app-admin' );
        wp_localize_script( 'app-admin', 'XFAPJS', array(
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
            'nonce' => wp_create_nonce( 'app_nonce', 'app_nonce' ),
            'base' => $screen->base,
            'blocksEditor' => method_exists( $screen, 'is_block_editor' ) &&      $screen->is_block_editor() ? true : false
        ) );
        wp_add_inline_script( 'app-admin', 'APP.init();' );
    }
}

So the scripts are loaded in 'post', 'post-new', 'profile', 'user-edit', but not in settings page. I want to load in my plugin settings page also http://wp.local/wp-admin/options-general.php?page=xapp

where is the problem?

CodePudding user response:

Follow these steps:

  1. write this line echo $screen->base . 'debug-xyz'; before if ( in_array( $screen->base, $this->pages ) ) line.
  2. now go to your page http://wp.local/wp-admin/options-general.php?page=xapp
  3. View page source(Right-click on the page and click on the View source page or open like this view-source:http://wp.local/wp-admin/options-general.php?page=xapp
  4. Search for debug-xyz in your page source using Find tool(CTRL F) then before the debug-xyz you get your page base name.
  5. copy that base name and add that base name into your public $pages array.
  6. remove that echo line that we added for getting base value.
  7. Now check if script is loading on your setting page or not.
  • Related