2017-12-27 175 views
0

我對WordPress插件相當陌生,但是我創建了一個簡單的向我的頁面頁眉添加自定義JavaScript的插件。調用未定義的函數add_action()wp_head

當我在我的本地主機上的WordPress管理頁面上運行插件時,它完美地工作(就像它應該做的那樣),但是我的IDE(phpStorm)插件有錯誤。

我得到的錯誤是:

PHP Fatal error: Uncaught Error: Call to undefined function add_action() in C:\xampp\htdocs\website1\wp-content\plugins\jade-plugin\jade-plugin.php:12 Stack trace: thrown in C:\xampp\htdocs\website1\wp-content\plugins\jade-plugin\jade-plugin.php on line 12

Fatal error: Uncaught Error: Call to undefined function add_action() in C:\xampp\htdocs\website1\wp-content\plugins\jade-plugin\jade-plugin.php:12 Stack trace: thrown in C:\xampp\htdocs\website1\wp-content\plugins\jade-plugin\jade-plugin.php on line 12

Process finished with exit code 255

我不知道是什麼導致這個錯誤,但我認爲這是與我的目錄中的文件。

我也想上傳這個插件與WordPress.org我們的家族企業,但是當我嘗試上傳插件我收到以下錯誤:

The plugin has no name. Add a Plugin Name: line to your main plugin file and upload the plugin again.

這裏是我的插件至今:

<?php 
    /* 
    * Plugin Name: InSite 
    * Plugin URI: http://localhost 
    * Description: Adds javascript 
    * Version: 1.0 
    * Author: Jade 
    * License: GPL2 
    */ 

    /* This function adds two lines of javascript to the page header */ 

    add_action('wp_head', 'addHeader'); 

    function addHeader() 
    { 
     ?> 
     <script 
    src="http://app.insitesoftware.co.za:8080/socket.io/socket.io.js"> 
    </script> 
     <script src="http://app.insitesoftware.co.za:8080/client.js"> 
    </script> 

<?php 
    } 

    ; 

    ?> 

任何幫助會如此感激:)

回答

0

你必須使用WordPress的編碼標準和功能,掛鉤包括JavaScript的應該是wp_enqueue_scripts不能wp_head。這是執行此操作的代碼片段函數。並且參考this以獲得更多細節

function slug_cssinvolve() { 

     //for custom script 
     wp_register_script('custom-script', plugins_url('/js/custom-script.js', __FILE__)); 
     //for library 

     wp_register_script('custom-script', plugins_url('/js/custom-script.js', __FILE__), array('jquery')); 

} 
add_action('wp_enqueue_scripts', 'slug_cssinvolve'); 
相關問題