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

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

Thanks in advance,

Aprillins Fox

share|improve this question
    
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
add comment

3 Answers

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.

share|improve this answer
add comment

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

    <?php 
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 ?>
    ADD YOUR PLAIN HTML CODE HERE
    <?php //Open PHP tags
}
share|improve this answer
add comment

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:

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

then in functions.php:

<?php
my_head_function() {
    // do stuff
    return whatever;
}
?>
share|improve this answer
add comment

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.