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

    (@darkpaladin)

    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

    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

    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.