在函数中。我的主题php我有代码在管理区添加页面并向其添加脚本。但未加载脚本。下面是代码。注释掉的add\\u操作行是我检查过的。
// functions.php
// Append user style and scripts to Add New Wallpaper menu
function pb_admin_scripts() {
wp_enqueue_script(\'media-upload\');
wp_enqueue_script(\'thickbox\');
wp_register_script(\'pb-upload\', get_template_directory_uri() . \'/pb-upload.js\');
wp_enqueue_script(\'pb-upload\');
}
function pb_admin_style() {
wp_enqueue_style(\'thickbox\');
}
function pb_wallpaper_adminmenu() {
$page = add_menu_page(\'Dodaj Tapetę\', \'Dodaj Tapetę\', \'edit_posts\', \'add-wallpaper\', \'pb_add_wallpaper\', \'\', 6);
}
// add_action( \'admin_head-add-wallpaper\', \'pb_admin_scripts\');
// add_action( \'admin_print_scripts-add-wallpaper\', \'pb_admin_scripts\');
// add_action( \'admin_print_styles-add-wallpaper\', \'pb_admin_style\');
// add_action( \'load-add-wallpaper\', \'pb_admin_scripts\');
// add_action( \'load-add-wallpaper\', \'pb_admin_style\');
// add new wallpaper
function pb_add_wallpaper() {
?>
<div class="wallpaper-add">
<h3>Dodaj Nową Tapetę</h3>
<form method="post" action="">
<input type="hidden" name="action" value="pb_add_wallpaper" />
<input id="upload_image_button" type="button" value="Upload Image" />
<input type="hidden" name="upload_image" id="upload_image" />
<?php wp_nonce_field(\'pb_add_wallpaper\', \'_nonce\'); ?>
</form>
</div>
<?php
}
add_action(\'admin_menu\', \'pb_wallpaper_adminmenu\');
最合适的回答,由SO网友:Miha Rekar 整理而成
使用add_action(\'admin_enqueue_scripts\', \'pb_admin_style\');
作为manual 各州admin_enqueue_scripts
也可用于针对特定的管理页面。使用此选项仅选择所需的管理页面。
function pb_admin_style($hook) {
if( \'edit.php\' != $hook )
return;
wp_enqueue_script( \'my_custom_script\', plugins_url(\'/myscript.js\', __FILE__) );
}
add_action( \'admin_enqueue_scripts\', \'pb_admin_style\' );
SO网友:Adrian
这是我的加载脚本方法,因为我在特定的post类型中加载脚本。此示例适用于post\\u type=\'resources\'
if(!function_exists(\'load_pdf_uploader_scripts\'))
{
function load_pdf_uploader_scripts($hook)
{
//Check if is in edit post
if(isset($_GET[\'post\']) && isset($_GET[\'action\']))
{
//Get post type
$post_type = get_post_type( $_GET[\'post\'] );
//Check if we are in resources post type to load specific scripts
if($post_type == \'resources\' && $_GET[\'action\']==\'edit\')
{
//Load the script
wp_enqueue_script(\'pdfupload\', get_stylesheet_directory_uri().\'/assets/js/pdfupload.js\', array(\'jquery\')); // in footer
}
}
//Check if is new post item
elseif(isset($_GET[\'post_type\']))
{
//Check if we are in resources post type to load specific scripts
if($_GET[\'post_type\'] == \'resources\')
{
//Load the script
wp_enqueue_script(\'pdfupload\', get_stylesheet_directory_uri().\'/assets/js/pdfupload.js\', array(\'jquery\')); // in footer
}
}
}
//We load this script only in admin area
add_action(\'admin_enqueue_scripts\', \'load_pdf_uploader_scripts\');
}