WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Edit head (via wp_head) after filtering content (via the_content) (11 posts)

  1. Jan-Philip Gehrcke
    Member
    Posted 3 years ago #

    Hello guys!

    I'm quite unexperienced with the WordPress internals and ask myself how to change the html page head in dependence on the post content.

    [The motivation: A filter on the_content accomplishes syntax highlighting and generates CSS code. To achieve a valid document, this CSS code has to end up between <head> and </head>.]

    Therefore, I have to use:

    (a) a filter on the_content
    =============================
    http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
    In general, I want to build a string S which depends on the content.

    (b) the wp_head action hook:
    ==============================
    http://codex.wordpress.org/Plugin_API/Action_Reference/wp_head
    The string S built in dependence on the content should end up in the header between <head> and </head>.

    Is this possible in theory? Or will it be impossible due to some chronological order things are called/evaluated?

    My first idea was to just call

    add_filter('the_content', 'some_func_building_S', 99);
    add_action('wp_head', 'some_func_echoing_S');

    but it seems like it does not work using this simple approach.

    Thank you for any suggestions!

    Jan-Philip Gehrcke
    --
    http://gehrcke.de

  2. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

    I think you misunderstand how wp_head works. Perhaps a very simple function from one of my themes will help:

    // Output designer meta tags
    if( !function_exists( 'purplepastels_designer') ) :
    function purplepastels_designer() {
    ?>
    <meta name="designer" content="Mel P." />
    <meta name="designer_email" content="esmi at quirm dot net" />
    <?php
    }
    endif;
    add_action( 'wp_head', 'purplepastels_designer' );
  3. Jan-Philip Gehrcke
    Member
    Posted 3 years ago #

    esmi, thanks for your answer. But: the way the wp_head add_action hook works is clear to me. Look at my example given above. I'll make it "bigger", to show my actual problem:

    function some_func_echoing_S(){
        global $S;
        echo $S;
    }
    
    function some_func_building_S($content){
        global $S;
        $S .= foobar($content);
        return barfoo($content);
    }
    
    $S = "rofl";
    add_filter('the_content', 'some_func_building_S', 99);
    add_action('wp_head', 'some_func_echoing_S');

    In this case, "rofl" ends up in the head. Fine. I understood how wp_head works. But $S .= foobar($content); does not end up in the head. This is my problem. And my question is, how to do something like this (change head in dependence of content) :-) I hope this is clearer now.

  4. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

    Could be something to do with using $content. Are you really trying to place post content in the <head\></head> section?

  5. Jan-Philip Gehrcke
    Member
    Posted 3 years ago #

    Could be something to do with using $content. Are you really trying to place post content in the <head\></head> section?

    Let me cite the motivation from my initial posting:

    The motivation: A filter on the_content accomplishes syntax highlighting and generates CSS code. To achieve a valid document, this CSS code has to end up between <head> and </head>

    @esmi: to provide you a more vivid description, I'll go more into detail. A filter (add_filter on the_content) parses the post content. It accomplishes syntax highlighting by generating and adding <div>'s and <span>'s and <pre>'s with class and id attributes to specific parts of the post content (the code to be highlighted). Hence, it changes the post content and that's why I need this filter. At the same time, it also generates CSS code dynamically (only the required minimum of what's needed for the piece of code that has to be highlighted in the post content). This CSS code must be applied to do the actual coloring/styling. It must be placed in the <head> section of an html document to keep standard conformity. So, as you can see, the post content gets filtered. During this process, a string $S (the CSS code) is generated and depends on the content. $S then has to be added to the head. Hopefully the problem is clear now.

    So, no, I don't plan to place post content in the head section. I plan to put a string $S into the head section, which depends on but is not the post content. This is the general description of my problem and my question is: is this possible? how is it possible?

  6. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

    the_content filter doesn't add any CSS to the head section of a page. All user-applied styling is added as inline CSS. Plus the filter only works on the post content. I don't think it can be applied to arbitrary text.

  7. Jan-Philip Gehrcke
    Member
    Posted 3 years ago #

    Sorry, no offense, but you did not understand the problem so far :-) I'll try it again :-)

    the_content filter doesn't add any CSS to the head section of a page

    "the_content filter" does not exist. But a filter on the_content can change the content string in a way whatever the developer likes. It can change it and it can analyze it. And as it is a simple PHP function it can calculate prime numbers and send funny words via email to a random address alongside within the same function that changes and analyzes the post content.

    Again, look at:
    add_filter('the_content', 'some_func_building_S', 99);

    For me, some_func_building_S() does three things:
    1) analyze the content: it finds pieces of code to highlight
    2) change the content: it adds the pre's and span's and so on to the content.
    3) generate CSS code: this CSS code (let's call it string $S as before) actually styles the new filtered post content.

    Up to here, everything works as intended: I've got the modified/changed/filtered post content. I've got the CSS code, the string $S. I can print it right before the post content. This works. This styles the post content. But this is invalid HTML. Now, I would like to place this CSS code (the string $S) in the head section of the website to make it valid HTML. And I don't know how to do it. Got it now? :-)

  8. Tim Moore
    Moderator
    Posted 3 years ago #

    You can't do it that way. By the time WordPress has gotten to the point of allowing you to generate the CSS from the_content, it has already processed triggers for wp_head. You can't go back in time.

    In order to do what you want, you'd have to create your own function that pulls the content for the current page, does the parsing/analyzing, then outputs the CSS. Attach that function to wp_head. You can then save this code and replace your the_content() call with an echo of your variable.

  9. Jan-Philip Gehrcke
    Member
    Posted 3 years ago #

    Thank you, Tim. I'll see if I can do this in an efficient way. I think we can mark this as resolved, since my general question is anwered :)

  10. Mark / t31os
    Moderator
    Posted 3 years ago #

    Run a filter/action on the_posts, iterate over the posts, then enqueue the scripts if you find what you're looking for...

    This is a little different to what you're aiming for but the approach being used shows you how you can enqueue scripts/styles based on the posts that are due to be displayed...

    http://wordpress.stackexchange.com/questions/2302/loading-scripts-only-if-a-particular-shortcode-or-widget-is-present

    Alot of what's posted there won't apply to what you're doing, but the action posted gives a good example of scanning through the posts prior to wp_head..

    So basically what you need to do here is..

    1. Hook onto the_posts and iterate over the array of posts.
    2. Determine if any of these posts need your script, using strpos, or whatever method is suited to your needs.
    3. Enqueue what needs enqueuing if you found a post in the iteration that needs your script.

    Quite simple really ... ;)

  11. Jan-Philip Gehrcke
    Member
    Posted 3 years ago #

    I just wanted to say thank you for the last post.

    Here, the_posts is marked as deprecated in 3.0:
    http://adambrown.info/p/wp_hooks/version/3.0?old=1

    So, now, I did not use it, but I created "my own" posts filter using $wp_query->posts. Modifiying posts by overwriting $post->post_content at this point works very well.

    But, until now, I did not manage to modify comments using this approach :/

Topic Closed

This topic has been closed to new replies.

About this Topic