0

I have two sites which I've copied a plugin from the first site and then altered the plugin and changed the name of plugins directoy and class name.

Like from:

old_xplugin

In the root of this plugin I have file called old_xplugin.php and class name equiavivalent: class old_xplugin

TO

new_xplugin (now with modifications of the old xplugin) In the root of this plugin I now a have file called new_xplugin.php and class name equiavivalent: class new_xplugin

Everything works until I change the name of the folder. (I can change name of the plugin, and the class name without problem).

But when I change the name of the folder I get jQuery reference eror: not defined. Why isn't jQuery loaded when change of the plugins folder?

This is the structure of the plugin file(s):

main plugin file (new_xplugin.php). These things are not changed after copying the plugin.

public function __construct() {                        
    //Iniate jquery and css
    add_action( 'wp_enqueue_scripts', array($this, 'js_css' ) );                             
}

public function js_css() {                       
    wp_enqueue_script(
        'wtfjs',
        plugins_url( '/js/wtf.js' , __FILE__) 
    );        
}

wtf.js

jQuery(function ($) {    //Wordpress says: jQuery reference error: undefined.
//code
});

2 Answers 2

1

No idea why the change of folder name makes a difference, but you should add jquery as dependency in your enqueue

wp_enqueue_script(
        'wtfjs',
        plugins_url( '/js/wtf.js' , __FILE__),
        array('jquery')
    );   
2
  • Sorry but it did not work. Same error. Feb 11, 2016 at 13:43
  • 1
    you should check if jquery is being loaded Feb 11, 2016 at 13:44
0

Thanks to @Mark Kaplun that spotted the actual issue. jQuery did not load before wp_enque_script was executed. So I hooked the function js_css() into init like this:

add_action( 'init', array( $this, 'js_css' ) );

and that solved the problem! :-)

(Very strange that this even worked on site1 though)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.