0

Have used the same code before and it worked so it's not the code thats the problem. I have only changed the class names in the current code but it doesn't load the jQuery or CSS. This is how I call the scripts.

add_action( 'wp_enqueue_scripts', 'scripts' );

function scripts() {
        wp_enqueue_style( 'style', plugins_url('css/style.css', __FILE__) );
        wp_enqueue_script( 'style' );
        wp_register_script( 'jquery', plugins_url('js/jquery.js',__FILE__ ), array( 'jquery' ));
        wp_enqueue_script('jquery');
}

Like I said, have used the exact same method before but this time it doesn't work. Don't know what to do. Thanks!

2 Answers 2

3
function scripts_func()
{
    //----------------------------------------------
    //  Include css file
    //----------------------------------------------
    wp_register_style( 'style', plugin_dir_url(__FILE__).'css/style.css', '', '1.0', 'all' );
    wp_enqueue_style('style');


    //----------------------------------------------
    //  Include javascript file
    //----------------------------------------------
    wp_enqueue_script( 'my-js',  plugin_dir_url(__FILE__).'assets/myjs.js', array('jquery'), '1.0', false );

}add_action('wp_enqueue_scripts','scripts_func');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, i made the changes you suggested but it still doesn't load the jQuery or CSS.
you need not to add jquery by include jquery.js file. A latest jquery file is included with wordpress. You have to just add wp_enqueue_script('jquery')
1

Final Update: figure out that the plugin dir has space between the words,, removed the space and the plugin works :)

function scripts() {
wp_register_style( 'style', plugins_url('css/style.css', __FILE__) );
wp_enqueue_style( 'style' );
wp_register_script( 'jquery', plugins_url('js/jquery.js',__FILE__ ), array( 'jquery' ));
wp_enqueue_script('jquery');
}

Updated code

V2

function scripts() {
if ( ! defined( 'MyPLUGIN_URL' ) )
    define( 'MyPLUGIN_URL', plugin_dir_url( __FILE__ ) );

wp_register_style( 'style', MyPLUGIN_URL .'css/style.css');
wp_enqueue_style( 'style' );

wp_register_script( 'jquery', MyPLUGIN_URL .'js/jquery.js');
wp_enqueue_script('jquery');
}

9 Comments

Th first 2 lines of the codes are fixed to register and enque the CSS
Thanks for your answer! I have tried that but it still didn't load the jQuery and CSS.
Is your site online? I can see it on my browser?
I'm using localhost. Have had the same problem before. The scripts didn't load and then a day after it suddently worked.
you can try the V2 code, I just updated on my answer, I use the same and it works fine everytime. Also ensure that the css and JS are in the same folder :)
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.