Title: Moving Function to Header..
Last modified: August 19, 2016

---

# Moving Function to Header..

 *  Resolved [Lou Sparx](https://wordpress.org/support/users/roulettered/)
 * (@roulettered)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/)
 * Hi guys,
 * I’m trying to move some PHP code from my Functions.php to my Header.php.
 *     ```
       function hello() {
       global $post;
       if (!is_single()) {
           return;
       }
   
       //print_r($post);
       $pony = strip_tags($post->post_content);
       $pony = str_replace(array('"',"'", "\n", "\r", "\t"), ''', $pony);
   
       echo "$pony";
       }
   
       add_action('wp_head', 'hello');
       ```
   
 * I’ve tried:
 *     ```
       <?PHP {
       global $post;
       if (!is_single()) {
           return;
       }
   
       //print_r($post);
       $pony = strip_tags($post->post_content);
       $pony = str_replace(array('"',"'", "\n", "\r", "\t"), ''', $pony);
   
       echo "$pony";
       }
   
       ;?>
       ```
   
 * **etc..** But this doesn’t work.
 * Any help would be much appreciated.
 * Thanks,
    Lou

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/moving-function-to-header/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/moving-function-to-header/page/2/?output_format=md)

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642545)
 * Can I ask why you’re trying to move this code?.
 *  Thread Starter [Lou Sparx](https://wordpress.org/support/users/roulettered/)
 * (@roulettered)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642555)
 * I want the output in the header at the **top**.
 * Any suggestions?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642559)
 * At the top of the `<head></head>` section of each page or within your theme’s
   header area?
 *  Thread Starter [Lou Sparx](https://wordpress.org/support/users/roulettered/)
 * (@roulettered)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642576)
 * **I want the 1st code listed to work within my header.php** instead of the functions.
   php but not sure how to convert it from a function to something that would work
   placed in my header.php.
 *  Thread Starter [Lou Sparx](https://wordpress.org/support/users/roulettered/)
 * (@roulettered)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642577)
 * If I place:
 * > **`function hello() {
   >  global $post; if (!is_single()) { return; }
   > //print_r($post);
   >  $pony = strip_tags($post->post_content); $pony = str_replace(
   > array(‘”‘,”‘”, “\n”, “\r”, “\t”), ”’, $pony);
   > echo “$pony”;
   >  }
   > add_action(‘wp_head’, ‘hello’);`
 * In my header.php it doesn’t work.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642589)
 * It won’t. `add_action('wp_head', 'hello');` means that this function is being
   called when wp_head() runs in header.php. It will still echo its output within
   the post content of single post pages. You would need to completely re-write 
   the function to get it to output something into your pages’ header.
 *  Thread Starter [Lou Sparx](https://wordpress.org/support/users/roulettered/)
 * (@roulettered)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642593)
 * Exactly my original post.. If you check my second code example I’m trying to 
   convert that to work within my header.php.
 * If anyone can help I’d much appreciate it.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642639)
 * The function is altering the single post **content**. Why would you have the 
   post content in the head of a page? If that’s what you want, then you’d need 
   to run a Loop (perhaps using [get_posts](http://codex.wordpress.org/Function_Reference/get_posts))
   in header.php.
 *  Thread Starter [Lou Sparx](https://wordpress.org/support/users/roulettered/)
 * (@roulettered)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642656)
 * With all due respect, esmi, I’ve had my question answered by several other questions
   followed by _“needs to be re-written”_ which I was originally asking for help
   with in the first place lol.
 * Thanks anyway
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642675)
 * That’s not how functions work, they don’t run based on the file they reside in..
 * Functions are called, either by another function or by a direct function call,
   ie. `my_function();` ..
 * Functions should be ideally placed into the functions.php file (although there
   are other use cases where they might be suited directly in the template file,
   this isn’t one of them).
 * Can you explain what it is that you want to place into your header so someone
   is able to better advise you on how to proceed.
 * That particular function is not suited for placing content into the header area(
   fine, no problem), but if you want help adapting the function to do something
   different then in order to advise you we need to be able to understand what is
   it you’re aiming to do..
 * My best guess right now is that you want to print the content from a post into
   the header, would that be correct? And if so, is the content intended to be read
   from a particular post or are you aiming for the post content to be pulled dynamically?
 * Please clarify what you want to do and i’ll do what i can to help .. 🙂
 *  Thread Starter [Lou Sparx](https://wordpress.org/support/users/roulettered/)
 * (@roulettered)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642696)
 * Hi Mark,
 * OK I’ll start a new query which will lead to the same result.. I have this code
   that calls the excerpt into a meta tag:
 * > <meta name=”description” content=”<?php $words = explode(‘ ‘, get_the_excerpt());
   > 
   > echo implode(‘ ‘, array_slice($words, 0, 24)).’ …’; ?>” />
 *  The only problem is the excerpt that **is** called has quotes inside it that
   I don’t want (they break the meta tag).
 * How can I str_replace **” (quotes)** within the excerpt, with say a **Z**?
 * Maybe:
 * > <meta name=”description” content=”<?php **str_replace ‘”‘, ‘Z’;** $words = 
   > explode(‘ ‘, get_the_excerpt());
   >  echo implode(‘ ‘, array_slice($words, 0, 
   > 24)).’ …’; ?>” />
 * The excerpt that is being called contains quotes (“) that I don’t want. Please
   help me **str_replace** them 🙂
 * Lou
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642732)
 * Replace both single and double quotes…
 *     ```
       $something = str_replace( array( '"', "'" ), '', $somevarwithquotes );
       ```
   
 * Hope that helps.. 🙂
 *  Thread Starter [Lou Sparx](https://wordpress.org/support/users/roulettered/)
 * (@roulettered)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642756)
 * So I’m assuming my variable is **get_the_excerpt()**.
 * Becomes..
 * > <meta name=”description” content=”<?php **$something = str_replace( array( ‘”‘,“‘”),”,
   > get_the_excerpt()** ); $words = explode(‘ ‘, get_the_excerpt());
   >  echo implode(‘‘,
   > array_slice($words, 0, 24)).’ …’; ?>” />
 * What would replace the **$something**?
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642787)
 * Last piece of code was only intended as an example..
 * Here’s a better version..
 *     ```
       <?php
       $excpt = str_replace( array( '"', "'" ), '', get_the_excerpt() );
       $words = explode( ' ', $excpt );
       $excpt = implode( ' ', array_slice( $words, 0, 24 ) );
       ?>
       <meta name="description" content="<?php echo $excpt; ?>" />
       ```
   
 * 🙂
 *  Thread Starter [Lou Sparx](https://wordpress.org/support/users/roulettered/)
 * (@roulettered)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/#post-1642797)
 * Thank you so much, Mark. Really appreciate you taking the time to write that 
   code for me.
 * Lou

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/moving-function-to-header/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/moving-function-to-header/page/2/?output_format=md)

The topic ‘Moving Function to Header..’ is closed to new replies.

## Tags

 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [header](https://wordpress.org/support/topic-tag/header/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 16 replies
 * 3 participants
 * Last reply from: [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * Last activity: [15 years, 8 months ago](https://wordpress.org/support/topic/moving-function-to-header/page/2/#post-1642895)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
