I would like to know is it possible to add script right after head tag? I don't want to use add_action('wp_head','function');

So here is the case:

  • I have custom fields values for example 'this is the custom field value'
  • I want to echo it after head tag
  • I want to echo it using functions.php
  • I don't want to change any code on my Wordpress theme

so it will be

this is custom field value
<?php wp_head(); ?>

Thanks in advance,

Aprillins Fox

  • whats the reasoning behind this? It was my understanding calling the wp_head function started all the WordPress functionality etc, I could be wrong.
    – Alex Older
    Jan 10 '12 at 9:15
  • Not sure that's even possible given the constraints you put, same as Alex here, what's the reason?
    – Noel Tock
    Jan 10 '12 at 9:17

Fairly certain this is not possible without editing the actual theme header.php file. The function wp_head() does in-fact include other scripts, not sure you get a choice to add a script right above everything else and to be honest I don't see why you would need too.

Your constraints seem rather strict, I'm curious why you can't edit the header.php file. It's not like updating Wordpress will overwrite your changes.


This is a late reply but perhaps would be of help to someone. I haven't tested this idea but perhaps you could you use this code to display the custom field outside the loop:

from http://www.wprecipes.com/wordpress-how-to-get-custom-fields-outside-the-loop

global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'customField', true);

and modify this code to insert it in the header from a plugin:

from How to add code to Header.php in a child theme?

    add_action('wp_head', 'wpse_43672_wp_head');
function wpse_43672_wp_head(){
    //Close PHP tags ?>
    <?php //Open PHP tags

Use a child theme - this way you don't have to edit your original (parent) theme, and the only files it will need to contain are a stylesheet (that imports the parent styles as in the example on the Codex page), your functions.php and header.php.

If (for whatever reason) you don't want to use the wp_head hook, then your header.php file will look something like:

    <?php echo my_head_function(); ?>
    rest of header stuff here...

then in functions.php:

my_head_function() {
    // do stuff
    return whatever;

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.