balbzr
balbzr

Reputation: 179

Wordpress - how to add a code snippet to <head> of each page

I want to add some code to the head of each page that loads using a plugin that I build. What is the proper hook for this and the proper way of doing it? Thanks a lot

Upvotes: 4

Views: 2707

Answers (2)

Marco Panichi
Marco Panichi

Reputation: 1185

For those looking for a sample code, here it is:

add_action( 'wp_head', 'YOUR_SCRIPT_IN_HEAD_FUNCTION' );     
function YOUR_SCRIPT_IN_HEAD_FUNCTION() {
    echo '<script>
        // some code
    </script>';
}

Upvotes: 5

richplane
richplane

Reputation: 547

To add JS code, use the wp_enqueue_script() function - put your call in a function which gets added to the filter 'wp_enqueue_scripts'.

To add HTML, write a function to render whatever code you need and put it onto the wp_head filter. As in add_action('wp_head', 'your_function');

Does that help?

Upvotes: 0

Related Questions