- This topic has 14 replies, 2 voices, and was last updated 2 years, 1 month ago by Leo.
-
AuthorPosts
-
March 3, 2022 at 1:33 pm #2141432Ray 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. π
March 3, 2022 at 1:42 pm #2141435LeoStaffCustomer SupportHi 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-informationLet me know π
March 3, 2022 at 1:49 pm #2141439Ray TolerHi 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.
March 3, 2022 at 2:02 pm #2141446LeoStaffCustomer SupportHmm 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_PageAdding PHP: https://docs.generatepress.com/article/adding-php/
March 3, 2022 at 2:33 pm #2141463Ray TolerThank 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!
March 3, 2022 at 2:35 pm #2141465LeoStaffCustomer SupportHow are you adding my code?
March 3, 2022 at 2:42 pm #2141471Ray TolerI 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.
March 3, 2022 at 2:49 pm #2141477LeoStaffCustomer Supportstrange. 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 ); } );
March 3, 2022 at 2:53 pm #2141479Ray TolerThat gives the correct/expected behavior on both the excluded page and the rest of the site. Perhaps the Randomizer plugin is doing something unseemly. π
March 3, 2022 at 2:55 pm #2141482Ray TolerIn 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.
March 3, 2022 at 2:55 pm #2141485LeoStaffCustomer SupportYeah unfortunately I believe that’s the case here.
March 3, 2022 at 3:00 pm #2141489Ray TolerThank 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.March 3, 2022 at 3:04 pm #2141494LeoStaffCustomer SupportSounds good π
March 3, 2022 at 4:03 pm #2141524Ray TolerOk – 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.
March 4, 2022 at 10:25 am #2142536LeoStaffCustomer SupportThanks for sharing!
-
AuthorPosts
- You must be logged in to reply to this topic.