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:
- write this line
echo $screen->base . 'debug-xyz';
beforeif ( in_array( $screen->base, $this->pages ) )
line. - now go to your page
http://wp.local/wp-admin/options-general.php?page=xapp
- 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
- Search for
debug-xyz
in your page source using Find tool(CTRL F) then before thedebug-xyz
you get your page base name. - copy that base name and add that base name into your
public $pages
array. - remove that echo line that we added for getting base value.
- Now check if script is loading on your setting page or not.