[Resolved] PHP filtering questions (title tag and headings)

Home Forums Support PHP filtering questions (title tag and headings)

  • This topic has 36 replies, 3 voices, and was last updated 4 months ago by David.
Viewing 7 posts - 31 through 37 (of 37 total)
  • Author
    Posts
  • #1523704
    David
    Staff
    Customer Support

    Glad we could be of help!

    #1581927
    Eric

    Hey GP Team, OK so I have another update with this thread… I hope you don’t mind.

    I am now finally trying to trigger these PHP filters using post tags like I mentioned a while back but the solution you guys gave to me is not working like we discussed.

    So I have a global setting from the first code you guys gave that looks something like this:

    function db_change_content_h2( $content ) {
        $content = str_ireplace( '<h2>', '<h3>', $content );
        $content = str_ireplace( '</h2>', '</h3>', $content );
        return $content;
    }
    add_filter( 'the_content', 'db_change_content_h2' );
    
    function db_change_content_h3( $content ) {
        $content = str_ireplace( '<h3>', '<h2>', $content );
        $content = str_ireplace( '</h3>', '</h2>', $content );
        return $content;
    }
    add_filter( 'the_content', 'db_change_content_h3' );
    
    function db_change_content_h4( $content ) {
        $content = str_ireplace( '<h4>', '<h3>', $content );
        $content = str_ireplace( '</h4>', '</h3>', $content );
        return $content;
    }
    add_filter( 'the_content', 'db_change_content_h4' );

    This is affecting the entire site replacing the strings:
    1) h2 to h3
    2) h3 to h2
    3) h4 to h3

    Directly below this global setting function, I have placed the “filter by post tag” code to change the headings differently but only for that specific post tag. This is what I tried to use:

    if (has_tag( 'recipes' ) ) {
    
    function db_change_content_h2( $content ) {
        $content = str_ireplace( '<h2>', '<h2><span>', $content );
        $content = str_ireplace( '</h2>', '</span></h2>', $content );
        return $content;
    }
    add_filter( 'the_content', 'db_change_content_h2' );
    
    function db_change_content_h3( $content ) {
        $content = str_ireplace( '<h3>', '<h2><label>', $content );
        $content = str_ireplace( '</h3>', '</label></h2>', $content );
        return $content;
    }
    add_filter( 'the_content', 'db_change_content_h3' );
    
    function db_change_content_h4( $content ) {
        $content = str_ireplace( '<h4>', '<h2><summary>', $content );
        $content = str_ireplace( '</h4>', '</summary></h2>', $content );
        return $content;
    }
    add_filter( 'the_content', 'db_change_content_h4' );
    
    function db_change_content_p( $content ) {
        $content = str_ireplace( '<p>', '<p><span>', $content );
        $content = str_ireplace( '</p>', '</span></p>', $content );
        return $content;
    }
    add_filter( 'the_content', 'db_change_content_p' );
    
    function db_change_content_li( $content ) {
        $content = str_ireplace( '<li>', '<li><label>', $content );
        $content = str_ireplace( '</li>', '</label></li>', $content );
        return $content;
    }
    add_filter( 'the_content', 'db_change_content_li' ); }

    For this tag I wanted to modify and add different tags to each of the specified elements on the page. I’ve got the following:

    1) h2 to h2span
    2) h3 to h2label
    3) h4 to h2summary
    4) p to pspan
    5) li to lilabel

    So the global settings work no problem but I can’t seem to get the tags to trigger the specific filtering. I’ve also tried your initial suggestion which was to use this:

    if ( is_singular() && has_term('', 'recipes') ) {

    Unfortunately, I’m not really using taxonomy and terms, but only post tags. How I should go about this?

    I would like to set several tag triggered templates this way but I am not too sure what limitations this may create on my site. I intend to just assign a tag to a post and then the elements will swap out as indicated but please let me know if it’s even possible to swap out specific tags like this haha, I hope I’m not breaking anything!

    Also, is there an issue with placing the global setting and the specific tag settings one after the other in the functions.php file? I am just wondering if one is overriding the other and if there is something else that I should be doing to make them all play nice together.

    Thanks so much again for your time and attention with this. I hope you guys have all been staying safe and more power to GP! Hope to hear from you soon!

    #1582188
    David
    Staff
    Customer Support

    You’re IF condition needs to be inside the function not wrapping a group of functions. For example you could combine your Global and has_tag() functions into one:

    function db_change_content_h2( $content ) {
        if (has_tag( 'recipes' ) ) {
            // If i am recipe string replace this
            $content = str_ireplace( '<h2>', '<h2><span>', $content );
            $content = str_ireplace( '</h2>', '</span></h2>', $content );
        } else {
            // else apply the global string replace
            $content = str_ireplace( '<h2>', '<h3>', $content );
            $content = str_ireplace( '</h2>', '</h3>', $content );
        }
        return $content;
    }
    add_filter( 'the_content', 'db_change_content_h2' );
    #1582345
    Eric

    Ok yeah that seems to work! But is there a way to just add them one at a time rather than combining the functions together? Or is it an all or nothing situation? I’m thinking that it’s probably best to keep things as simple as possible just in case I forget how I did it lol! For example, I will have my global template at the top, then I will just add new tag triggered functions one at a time below them. That would then remove the “else” condition, right? Is that possible? This is what I tried but it didn’t work:

    function db_change_content_p( $content ) {
        if (has_tag( 'recipes' ) ) {
            // If i am recipe string replace this
            $content = str_ireplace( '<p>', '<p><span>', $content );
            $content = str_ireplace( '</p>', '</span></p>', $content );
        } 
        return $content;
    }
    add_filter( 'the_content', 'db_change_content_p' );
    #1583065
    David
    Staff
    Customer Support

    You would need to exclude the other other filters from your global filter for that to work.

    function db_change_content_h2( $content ) {
        if ( !has_tag( 'recipes' ) || !has_tag( 'reviews' ) || !has_tag( 'aliens' )  ) {
            // I am global but not recipes, reviews or aliens.....
        } 
        return $content;
    }
    add_filter( 'the_content', 'db_change_content_h2' );

    I would personally stick with the combined filter it will save those extra function callbacks.

    #1583394
    Eric

    Ah good point with the callbacks… OK ill try to get this working! Thanks again!

    #1583627
    David
    Staff
    Customer Support
Viewing 7 posts - 31 through 37 (of 37 total)
  • You must be logged in to reply to this topic.