0
votes

I'm fairly new to WordPress plugins, however I have created a simple one which adds custom JavaScript to my page headers.

When I run the plugin on my WordPress admin page on my localhost, it works perfectly (as in it does what it should), but I have an error with the plugin in my IDE (phpStorm).

The error I get is:

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

I have no idea what could be causing this error but I think it has something to do with the files in my directory.

I also want to upload this plugin to WordPress.org for our family business but when I try to upload the plugin I get the following error:

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

Here is my plugin so far:

    <?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
    }

    ;

    ?>

Any help would be so appreciated :)

1

1 Answers

0
votes

You have to use wordpress coding standards and functions, The hook to include JavaScript should be wp_enqueue_scripts not wp_head. Here's the snippet function to do this. And refer this for more details

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');