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.

  • 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 Leedy 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

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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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