[Resolved] Random (via plugin/shortcode) tagline / site description

Home Forums Support [Resolved] Random (via plugin/shortcode) tagline / site description

Home Forums Support Random (via plugin/shortcode) tagline / site description

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #2141432
    Ray Toler

    I’d like to have a random tagline and have selected the Randomizer plugin as it has some other uses for me. I have successfully gotten it to display as a widget, both in the site header/ID area as well as the normal body.

    However, putting a shortcode in the site description field (either in settings or Customizer) simply results in the actual shortcode being displayed, which I suppose is expected. Next, I tried looking for the appropriate place to do the embed via a php wrapper in a hook, but couldn’t figure out exactly where it should go.

    I’m sure it’s pretty straightforward and simple, but I’m having a hard time finding that starting point. So far, I’ve researched child themes, elements, snippets, hooks etc., but have never really had much need to use them, so there’s a lot of drinking from the firehose going on.

    I’m more than happy to do the reading and experimentation, just looking for a pointer on where/how to place the shortcode (or the php wrapper for it) so that it provides the site description value.

    The code I’d need to add if PHP is required is:

    <?php randomize('EXAMPLE_CATEGORY'); ?>

    Thanks! I know this is pretty trivial, but I’m trying to be more self-sufficient. πŸ™‚

    #2141435
    Leo
    Staff
    Customer Support

    Hi there,

    Any chance you can link us to the page in question?

    You can use the private information field:
    https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information

    Let me know πŸ™‚

    #2141439
    Ray Toler

    Hi Leo, Sorry – yes, that probably would have been helpful. It’s https://www.raytoler.com and I’d like this to happen on any page except the “Experience” page if possible (that’s why I was initially looking at hooks).

    I’m just not sure what file needs a child or where the hook should live to override the default site description.

    #2141446
    Leo
    Staff
    Customer Support

    Hmm give this snippet a shot:

    add_filter( 'generate_site_description_output', function( $output ) {
        $tagline = html_entity_decode( get_bloginfo( 'description', 'display' ) );
    
        if ( ! is_page( xxxx ) ) {
            $tagline = randomize('EXAMPLE_CATEGORY');
        }
    
        return sprintf(
            '<p class="site-description" itemprop="description">
                %1$s
             </p>',
            $tagline
        );
    } );

    Replace xxxx with the actual ID of the Experience page:
    https://codex.wordpress.org/Conditional_Tags#A_PAGE_Page

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    #2141463
    Ray Toler

    Thank you for the amazingly speedy response! It’s almost there… the exclusion of the Experience page is working correctly, and the randomization is replacing the text of the tagline, but it’s not showing up in the correct place in the HTML, landing too early in the site header:

      <header class="site-header" id="masthead" aria-label="Site" itemtype="https://schema.org/WPHeader" itemscope>
            <div class="inside-header">
    
                				HR Googlers, get your red pens ready!
                <div class="site-branding">
                    <p class="main-title" itemprop="headline">
                        <a href="https://www.raytoler.com/" rel="home">
                        						Ray Toler
                        					</a>
                    </p>
                    <p class="site-description" itemprop="description">
                    </p>
                </div>
                <div class="site-logo">
    

    On this particular load, the tagline is “HR Googlers, get your red pens ready!” but you can see that it’s not in the site-description paragraph.

    I tried moving the $tagline in the snippet to be just after the %1$s, but it didn’t fix it. Sadly, I don’t know enough PHP to make an intelligent guess at what might be missing or need relocating. Also, I was pretty successful understanding the snippet you provided me, but what is the %1$s doing?

    Thank you again!

    #2141465
    Leo
    Staff
    Customer Support

    How are you adding my code?

    #2141471
    Ray Toler

    I installed the Code Snippets plugin per the “Adding PHP” link you provided, then added a new snippet named “Randomize Tagline”. It is set to “Run snippet everywhere” and is the only active snippet.

    #2141477
    Leo
    Staff
    Customer Support

    strange. My code should’ve simply replaced the output of the site description.

    What happens if you add this?

    add_filter( 'generate_site_description_output', function( $output ) {
        $tagline = html_entity_decode( get_bloginfo( 'description', 'display' ) );
    
        if ( ! is_page( xxxx ) ) {
            $tagline = 'A custom tagline';
        }
    
        return sprintf(
            '<p class="site-description" itemprop="description">
                %1$s
             </p>',
            $tagline
        );
    } );
    #2141479
    Ray Toler

    That gives the correct/expected behavior on both the excluded page and the rest of the site. Perhaps the Randomizer plugin is doing something unseemly. πŸ™‚

    #2141482
    Ray Toler

    In fact, Leo, I’d suspect it first since your code works fine without it and it’s a tad old (last updated a year ago). Rather than waste your time, let me experiment with some other plugs and see if they work better.

    #2141485
    Leo
    Staff
    Customer Support

    Yeah unfortunately I believe that’s the case here.

    #2141489
    Ray Toler

    Thank you for the very quick responses and the code! I’m trying to learn. πŸ™‚ I’ll leave this open for a bit longer so I can post an update if I find something that works, otherwise will close out the ticket later today.

    Best regards!
    r.

    #2141494
    Leo
    Staff
    Customer Support
    #2141524
    Ray Toler

    Ok – leaving this additional info in case future searchers are helped.

    The most direct way I found of doing this is to use the XV Random Quotes plugin. It’s a bit older and not specifically tested with WP 5.9, but still generally in development and seems to work without any issues.

    My settings in the XV Random Quotes plugin were to enable the “shortcodes in bloginfo” option, and to use the following shortcode in the site’s Settings > General tagline field: [stray-random categories=tagline noajax=true]

    That solves the primary issue of getting a random tagline. I’ll be modifying Leo’s code snippet to force my “serious” tagline for certain pages.

    Some additional info, just in case it points someone toward another solution. When I first enabled the XV plugin and used its PHP call in Leo’s code snippet, I got the same behavior I did with the other randomization plugin – the tagline was changing, but appeared immediately before the site title, and didn’t pick up any style info.

    Next, I tried globally disabling AJAX in the XV plugin. This moved the randomized tagline to the line above the site name, but again, no style information. Clearly, something in what these plugins are generating is stomping on something else, but it’s interesting that they both end up putting the content in the same place.

    Thank you again, Leo, for the code snippet. I hope this helps someone in the future!

    r.

    #2142536
    Leo
    Staff
    Customer Support
Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.