簡體   English   中英

wp_enqueue_scripts不起作用

[英]wp_enqueue_scripts doesn't work

我創建了自己的JavaScript文件,現在想集成到模塊中。 我正在嘗試按照此網站上的指示進行操作https://developer.wordpress.org/reference/functions/wp_enqueue_script/但這不起作用,我不明白為什么

PS:我在模塊的主文件中(exampe.php)

class exemple
{
    public function __construct()
    {
        register_activation_hook(__FILE__, array('exemple_bd', 'install'));

        register_uninstall_hook(__FILE__, array('exemple_bd', 'uninstall'));

        include_once plugin_dir_path( __FILE__ ).'settings.php';
        new Settings();

        add_action('admin_menu', array($this, 'add_admin_menu'));
        add_action('admin_init', array($this, 'load_JS'));         
    }

    public function add_admin_menu()
    {
        add_menu_page('monTitre', 'Monplugin', 'manage_options','xxx', array($this, 'menu_html'),'dashicons-email');
        add_submenu_page('xxx', 'NouveauTitre', 'NouveauTitre', 'manage_options', 'xxx', array($this, 'menu_html'));

    }

    public function load_JS()
    {
        wp_register_script('myfunction',plugins_url( '/js/function.js', __FILE__ ));
        wp_enqueue_script('myfunction');
    }

您需要使用動作鈎子“ wp_enqueue_scripts”注冊它們。 使用“ admin_enqueue_scripts”作為管理腳本。

如果您想在站點的前端調用該函數,則應使用wp_enqueue_scripts操作掛鈎來調用該函數,如上面的示例中所示...

https://developer.wordpress.org/reference/functions/wp_enqueue_script/#notes

如果所有路徑都正確,則您有一個自定義主題,請確保您的主主題文件引用了wp_head()

它應該像這樣在結束的“ head”標簽之前:

<?php ...

wp_head();
 ?></head>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM