Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using a WordPress plugin for including PHP code within posts and pages, and it's working great - however, I need to insert some styles and scripts, and in order to do that, I need to add a hook to wp_head. I tried it, creating a function and using add_action('wp_head','my_func'); but it didn't work - my stuff is not being included.

I think its because wp_head is being called before my script it being included.

I would like to do this without changing any of my themes files.

Is there any way to do this? I am using the latest WordPress version.

share|improve this question
    
Are you trying to add the hook within a post? That won't work. Hooks must be placed inside your theme's functions.php file. –  Joseph Aug 31 '12 at 12:26
    
Yes, that is what I was trying. WP hooks only work inside the Functions file? –  Jeff Aug 31 '12 at 12:45
    
How would one go about adding scripts and styles to the head tag from a specific page then? –  Jeff Aug 31 '12 at 12:46

1 Answer 1

up vote 1 down vote accepted

To enqueue styles and scripts properly, use the wp_enqueue_scripts action like so:

function wpa_63708_enqueue_scripts() {
    wp_register_script( 'my-script','/path/to/script' );
    wp_enqueue_script( 'my-script' );
}    

add_action( 'wp_enqueue_scripts', 'wpa_63708_enqueue_scripts' );
share|improve this answer
1  
Use wp_enqueue_style for stylesheets, but on the same wp_enqueue_scripts action hook. –  Otto Aug 31 '12 at 17:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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