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

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

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

Viewing 15 posts - 1 through 15 (of 37 total)
  • Author
    Posts
  • #1443536
    Eric

    Hey GP Team! I hope you guys are all staying safe and well out there…

    I had a few questions about filtering across my GPRESS site. display:none solutions will not work unfortunately so I am hoping there is a PHP solution instead? I would like to:

    1. Filter out the <title> tag from all pages

    I have already been able to stop Yoast from generating one, but even with it disabled I can still see a <title> tag in the code. It seems auto-generated and includes the site title along with it. Is there a way for me to filter it out completely site-wide?

    2. Change all sub headings (h2, h3, h4, h5, h6) across the site into <p> tags

    I want to be able to change these all out across the entire site and style them instead with a class. The only way I know how to do this is to search and replace and I’m concerned that I will have to revert back to headings again at some point! I’m hoping to use something like in place of the traditional h2, h3, h4, etc.:

    <p class="headings3">

    with style:

    .headings3 {
    font-size:35px;  
    font-weight:900;
    }

    The tricky part of all of this is that I might have to retain some headings, and change out others. So if there’s a solution that will specifically replace each heading as needed that would be fantastic.

    Anyways, I hope to hear from you soon and thank you guys for making the BEST WP theme in the world! #GPRESS4LYF

    #1443726
    David
    Staff
    Customer Support

    Hi there,

    1. Try this PHP Snippet to remove the <title> tag from the WP <head>:

    remove_action( 'wp_head', '_wp_render_title_tag', 1 );

    2. That sounds really complicated – you would have to filter the_content for example:

    function db_change_content_h2( $content ) {
        $content = str_ireplace( '<h2>', '<p class="heading-2">', $content );
        $content = str_ireplace( '</h2>', '</p>', $content );
        return $content;
    }
    add_filter( 'the_content', 'db_change_content_h2' );

    You can string as many $content = str_ireplace within that function.

    Selectively choosing which is affected would be difficult. The way to do that would to make sure the <h2> element is different. eg. the above code would NOT affect <h2 class="my-custom-class"> as the tag is different, however it will rely on WP to correct the closing </h2> – which it may do perfectly well but can’t guarantee that it won’t mess up somewhere.

    #1444045
    Eric

    Hey David, thank you for the response… unfortunately, this tag:

    remove_action( ‘wp_head’, ‘_wp_render_title_tag’, 1 );

    …it has no effect and the <title> tag is still in the code. I actually tried this out first before messaging you guys but for some reason it won’t work.

    Noted on the headings solution, I’ll give it a go and let you know.

    #1444070
    David
    Staff
    Customer Support

    I tested that snippet on a clean install and it works.
    Do you have an SEO plugin – that may be overriding the core function – let us know.

    #1444110
    Eric

    Hmmm just Yoast but I’ve already filtered out the seotitle from there as well. I need to keep Yoast active on the site for it’s other features. I’ve tried this below:

    add_filter( 'wpseo_title', '__return_false' );
    
    remove_action( 'wp_head', '_wp_render_title_tag', 1 );

    It filtered out the SEO title that you type in the post editor but it then still displays a <title> tag but then adds the sitename at the end as well. What do you suggest?

    #1444140
    Eric

    BTW the headings fix worked like a charm! Thank you!

    #1444357
    David
    Staff
    Customer Support

    I found this:

    https://wordpress.org/support/topic/_wp_render_title_tag-not-working/

    Unfortunately they’re not very helpful with how to disable there new feature….
    Maybe you have premium support with Yoast where they may be a little more co-operative ?

    #1444523
    Eric

    Wow not very helpful is an understatement lol

    OK thank you, I’ll try to figure it out over there. At the end of the day, maybe it would be best to just shut it off. Thanks again for the headings fix!

    #1444548
    David
    Staff
    Customer Support

    You’re welcome – let us know if Yoast decide offer up any assistance.

    #1476370
    Eric

    Hey there David, sorry to unearth this ticket again… Just wanted to update that yeah no luck on the Yoast front unfortunately. But I did have a question about the headings swap code you gave me last time. I am currently trying to use it on h1 and it doesn’t seem to work. I would like to swap out all my h1 tags to h2 site wide (so that none of the pages has an h1 in the code). Unfortunately, when I try to use the php you gave me last time on h1s, it doesn’t work like on all the other headings. Here is the adjusted code I tried to use from what you gave me above:

    function db_change_content_h1( $content ) {
        $content = str_ireplace( '<h1>', '<h2>', $content );
        $content = str_ireplace( '</h1>', '</h2>', $content );
        return $content;
    }
    add_filter( 'the_content', 'db_change_content_h1' );

    What have I done wrong here exactly? Shouldn’t this swap them all out site wide like the others (h2, h3, h4, etc.)?

    Anyways, thanks again for your time and attention and I hope you guys are all staying safe and healthy. Hope to hear from you soon!

    #1476806
    David
    Staff
    Customer Support

    Try this PHP Snippet:

    add_filter( 'generate_get_the_title_parameters', function( $params ) {
        if ( is_singular() ) {
            $params = array(
                'before' => sprintf(
                    '<h2 class="entry-title"%s>',
                    'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : ''
                ),
                'after' => '</h2>',
            );
        }
        return $params;
    } );
    #1476998
    Eric

    Hmmmm ok I’ve tried it out but doesn’t seem to have any effect. The <h1> is still in the page code, and doesn’t change into an <h2>. Any other ideas?

    #1477147
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    That code should turn H1 elements to H2 elements on single posts and pages.

    Are you using GP 3.0.0?

    #1477642
    Eric

    Oh sorry about that! yes it was still on version 2.4.2… this code does work on version 3, thank you! Congrats on the latest version release by the way, the changelog is BANANAS! Thanks again guys… please stay safe and keep up the great work!

    #1478809
    Tom
    Lead Developer
    Lead Developer

    Awesome, I’m glad we could help! Thanks, you as well 🙂

Viewing 15 posts - 1 through 15 (of 37 total)
  • You must be logged in to reply to this topic.