Title: add_filter inside a function
Last modified: August 19, 2016

---

# add_filter inside a function

 *  [darkpaladin](https://wordpress.org/support/users/darkpaladin/)
 * (@darkpaladin)
 * [18 years ago](https://wordpress.org/support/topic/add_filter-inside-a-function/)
 * i’m creating a plugin, and i want to use add_filter for wp_head for every filter
   in the_content
    this is my code:
 *     ```
       <?php
       function content_function($content)
       {
       //Analize the_content
       add_filter('wp_head', 'head_function',1);
       }
   
       function head_function()
       {
       echo 'something to put in the head';
       }
   
       add_filter('the_content', 'content_function', 1);
       ```
   
 * i want to first analize the_content and if some conition is true then add something
   to the head, but it doesn’t works
    i triend to put the add_filter(‘wp_head’, ‘
   head_function’,1); outside the function and it works, but inside it doesn’t work
 * what can i do?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Roger Theriault](https://wordpress.org/support/users/rogertheriault/)
 * (@rogertheriault)
 * [18 years ago](https://wordpress.org/support/topic/add_filter-inside-a-function/#post-772181)
 * If the function is never called, the filter is never activated. Keep it outside–
   for what you’re attempting. You usually want WP to set up your filters and actions
   when it reads in all the PHP, before it has started doing any real output.
 * Only write one filter (for the head). If you’re only “analyzing” the content 
   you don’t need to filter it. In the head function, you can access anything you
   need in WordPress, so you can analyze the content from there… and then make changes
   to the head. Make sense?
 * OK, here’s an example of something similar… except it is an action not a filter,
   but same concept.
 *     ```
       function my_header_recipe() {
       	global $post;
       	# only if the Page has a parent slugged "recipes"
       	if (($parent = $post->post_parent) &&
       	    (the_slug($parent) == 'recipes')) {
       		// DO SOMETHING HERE
       	}
       }
       add_action('wp_head', 'my_header_recipe', 1);
       ```
   
 * Of course if you got all that but were trying to do something really strange 
   like look at the content AFTER it had been filtered, you might have a problem
   with timing…
 *  Thread Starter [darkpaladin](https://wordpress.org/support/users/darkpaladin/)
 * (@darkpaladin)
 * [18 years ago](https://wordpress.org/support/topic/add_filter-inside-a-function/#post-772186)
 * thanks, but what i want to do is to add an alternate stylesheet for an specific
   post,
    for example:
 * i write in a post [CSS = style2.css] then with the filter the_content i read 
   that code and then with the filter wp_head add it to the head tag. that’s why
   i need to read the content first and then call the wp_head filter.
 * also if i am in the index, i want to load all the stylesheets that is specified
   in the posts
 * i hope you undertand me =P, i’m not very good at english
 *  [Roger Theriault](https://wordpress.org/support/users/rogertheriault/)
 * (@rogertheriault)
 * [18 years ago](https://wordpress.org/support/topic/add_filter-inside-a-function/#post-772245)
 * I think I understand… you actually type the shortcode or insert it via the editor
   in some way, so it is stored in the database as part of the post?
 * What I suggested above will work in that case, although you’ll need to make the
   shortcode disappear on output.
 * But if I might suggest an alternative?
 * Instead of adding a shortcode, which is usually used for _replacing_ content 
   via a filter, why not either directly add a new editor selection to the Write
   Post / Write Page forms, which sets a custom field, or (try this first) open 
   the Custom Field dialog, and add something with key CSS and value style2.css.
 * Then in your head action, simply check for a custom field that matches the key
   and use the value to include the desired style sheet.
 * For some additional information: [custom field by default](http://wordpress.org/support/topic/145968)
 * That way you don’t need to mess with the content at all, and you can even make
   a dropdown listing only the valid css files to choose from (with descriptive 
   choices rather than filenames, too).
 * (Note that you can also use the custom Page Template selector, and check this
   in your head action, if you need different output logic to go with the special
   stylesheet, and in that case you won’t need a custom field)

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘add_filter inside a function’ is closed to new replies.

## Tags

 * [add_filter](https://wordpress.org/support/topic-tag/add_filter/)
 * [the_content](https://wordpress.org/support/topic-tag/the_content/)
 * [wp_head](https://wordpress.org/support/topic-tag/wp_head/)

 * 3 replies
 * 2 participants
 * Last reply from: [Roger Theriault](https://wordpress.org/support/users/rogertheriault/)
 * Last activity: [18 years ago](https://wordpress.org/support/topic/add_filter-inside-a-function/#post-772245)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
