WordPress插件样式和javascript无法加载(管理部分)

时间:2018-07-13 10:31:54

标签: javascript php jquery wordpress

所以我不知道为什么我的样式表和脚本不起作用。我在样式表中有样式。和我的脚本文件中的警报。请帮忙。 (我的路径是正确的。如果包含它们,它将回显它们的内容。)

<?php
    /*
    Plugin Name: Stock Manager
    */
    function my_admin_menu() {
        $page_title = 'Stock Manager';
        $menu_title = 'Stock Manager';
        $capability = 'manage_options';
        $menu_slug = 'stock-manager';
        $function = 'index';
        $icon_url = plugins_url('recycle.png',__FILE__);
        $position = 56;

        add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position);
    }
    add_action( 'admin_menu', 'my_admin_menu' );
    // Styles
    add_action('wsm_styles','reg_styles');
    function reg_styles(){
        wp_enqueue_style('wsm_stylesheet',plugins_url('css/styles.css',__FILE__));
    }
    // Content
    function index(){
      include ("includes/content.php"); 
    }   
    // Scripts
    add_action('wsm_scripts','reg_scripts');
    function reg_scripts(){
        wp_enqueue_script('wsm_scripts',plugins_url('js/scripts.js',__FILE__));
    }
?>

2 个答案:

答案 0 :(得分:1)

答案

需要专门为管理部分添加文件,如下所示。

我还从原始帖子中进行了一些结构更改。

<?php
    /*
    Plugin Name: Stock Manager
    */
    function my_admin_menu() {
        $page_title = 'Stock Manager';
        $menu_title = 'Stock Manager';
        $capability = 'manage_options';
        $menu_slug = 'stock-manager';
        $function = 'content';
        $icon_url = plugins_url('recycle.png',__FILE__);
        $position = 56;

        add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position);
    }
    add_action( 'admin_menu', 'my_admin_menu' );

    // Content
    function content(){
        include ("includes/content.php");
    }

 //This is where the styles and scripts is been included
 add_action('admin_head', 'my_custom_fonts');
 function my_custom_fonts() {
   echo '
     <script src="/wp-content/plugins/stock-manager/assets/js/scripts.js"></script>
     <link href="/wp-content/plugins/stock-manager/assets/css/styles.css" rel="stylesheet">
   ';
 }
 ?>

答案 1 :(得分:0)

尝试此代码

<?php
    /*
    Plugin Name: WebFootprint Stock Manager
    */
    function my_admin_menu() {
        $page_title = 'Stock Manager';
        $menu_title = 'Stock Manager';
        $capability = 'manage_options';
        $menu_slug = 'stock-manager';
        $function = 'index';
        $icon_url = plugins_url('recycle.png',__FILE__);
        $position = 56;

        add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position);
    }
    add_action( 'admin_menu', 'my_admin_menu' );
    // Styles
    add_action('wp_enqueue_scripts','reg_styles');

    function reg_styles(){
        wp_enqueue_script('wsm_scripts',plugins_url('js/scripts.js',__FILE__));
        wp_enqueue_style('wsm_stylesheet',plugins_url('/css/stylestest.css',__FILE__));
    }
    // Content
    function index(){
     include ("includes/content.php"); 
    }   

?>

将此add_action('wsm_styles'替换为add_action('wp_enqueue_scripts'