2

I have the following code:

function register_scripts(){
    wp_register_style( 'new_style', plugins_url('/css/style.css', __FILE__));
    wp_register_script( 'google-maps-api', 'http://maps.google.com/maps/api/js?sensor=false' );
}

add_action('wp_enqueue_scripts', 'register_scripts'); 

But it is not working, can anyone see what I've done wrong?

3
  • are you sure, add_action is called? Commented Nov 16, 2013 at 9:00
  • Philipp, thanks for the quick reply - I am basing it on codex.wordpress.org/Function_Reference/wp_enqueue_script so yes, add_action should be being called. The code snippet is from a plugin I am building. Commented Nov 16, 2013 at 9:12
  • 1
    You have registered them , but not enqueued .. Commented Nov 16, 2013 at 9:26

1 Answer 1

5

Like in comment - You have registered them, but not enqueued...

function regiqueue_scripts(){
    wp_register_style( 'new_style', plugins_url('/css/style.css', __FILE__));
    wp_register_script( 'google-maps-api', 'http://maps.google.com/maps/api/js?sensor=false' );
    wp_enqueue_style( 'new_style' ); // or use just enqueue without register .. but not the other way around 
    wp_enqueue_script( 'google-maps-api' ); 
}

add_action('wp_enqueue_scripts', 'regiqueue_scripts'); 

You see - registering the scripts just makes them available for use, but it will not enqueue until you tell it to do so. the function wp_enqueue_xx() - when all parameters filled CAN work without wp_register_xx() - but not the other way around.

Always use both as it allows more control over where and when to use the script.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Obmerk and thank you everyone else for the comments. First time using stackoverflow and have to say very impressed indeed!!
@Ian You are both welcome and welcomed. This is the best way to learn and progress - by solving other persons issues ..

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.