2017-12-27 187 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'); 
相关问题