I am trying to configure a custom shortcode that I can use in a conditional block in a Content Template. I want the condition to be:
IF [wpv-post-url] {contains a substring}
I was trying to modify the snippet shared by Nigel in the following support ticket from 2018:
https://toolset.com/forums/topic/comparing-a-portion-of-a-string-in-conditional-output/#post-1085588
I created the same 'contains' custom function on my site to add the custom shortcode.
I registered the 'contains' custom function as well as the 'wpv-post-url' shortcode.
I then tried to add a conditional block in my Content Template as displayed in the attached screenshot.
Note: I'm able to save the condition in Visual mode, but if I try saving it in Advanced mode, I get an error message that reads: "Please, complete all the conditions or remove them."
If the condition is true (i.e., if the current URL contains '/gab/discussion/' the template is configured to display a "Hello, world!" message. The template is placed in a theme footer widget so that it should appear below the BuddyPress activity feed (outside of the BP content wrapper), just above the bottom-most green footer, on the following page:
hidden link
As you can see in the other attached screen shot, no "Hello, world" message is displaying above the green footer.
I suspect the reference in the older snippet to field may be the issue. Is there a simple way to modify the add shortcode function to accomplish my goal?
Hi,
Thank you for contacting us and I'd be happy to assist.
Nigel's shortcode in his ticket is for a slightly different requirement. It is for a case when you need to check for some text within the output coming from a custom field value.
For a more generic use, where you can check for a contained text from any string (e.g. URL coming from 'wpv-post-url' shortcode ), you'll need to adjust the shortcode like this:
add_shortcode( 'contains', function( $atts ){
$return = -1;
$atts = shortcode_atts( [ 'string' => '', 'find' => '' ], $atts );
if ( (isset($atts['string'])) && (isset($atts['find'])) ) {
$return = strpos( $atts['string'], $atts['find'] );
if ( $return === false ) {
$return = -1;
}
}
return $return;
});
Next, please add "contains" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.
This shortcode accepts two attributes:
- string: source text
- find: text that needs to be found in the source text
Example:
[contains string='some text some text' find='some' ]
If the text to find is found, it will return the number of times it exists in the source; otherwise, it will return nothing/false.
In the conditional block, you can use it to evaluate if the output is greater than or equal to '1', as shown in the attached screenshot.
I hope this helps and please let me know if you need further assistance.
regards,
Waqar
Thank you, Waqar. I have made the changes. I'm still having some difficulty getting it to work. Please see the attached screen shots for a detailed view of what I've done. These include:
1) Image of revised 'contains' function taken from Toolset's Custom Code screen.
2) Confirmation that 'contains' is registered as both a function and as a shortcode.
3) Image of a test content template that tries to apply the shortcode.
4) Image of the front-end content showing the results on the URL that is referenced in the content template, which is: hidden link
Notice that the content template includes a block outside of the conditional to confirm that it's working. But the test copy configured inside the conditional is still not displaying.
Can you see any errors in my implementation?
Thanks!
Thanks for the update.
Because the content template is being loaded in the footer, I suspect that the current post's scope is not available, and hence shortcode like [wpv-post-url] is not returning the current URL.
Can you test the shortcodes like [wpv-post-url], [wpv-post-id], and [wpv-post-title] in this content template to see if the current post's URL, ID, and title are returned, correctly?
Waqar: I did some more testing (this time on my staging site) and confirmed: a) that those shortcodes do work in Content Templates; and b) that the 'contains' function snippet you provided *is* working outside of BuddyPress pages.
Attached is a screenshot of the revised conditional block placed in the template. If you take a look at this blog post...
hidden link
...you'll see that the 'contains' shortcode is working. But when I browse to a BuddyPress page that contains the same substring:
hidden link
...it does not work.
I assume this means that there's a compatibility issue with BuddyPress, and that I might need to devise some other method -- not based on [wpv-post-url] to identify when the user is on a BP page.
Can you think of any other ways to do that using a Conditional block?
Thank you for sharing these findings.
Can you please share temporary admin login details in reply to this message? I'll be able to perform some tests and suggest an alternative, accordingly.
Note: Your next reply will be private and it is recommended to make a complete backup copy before sharing the access details.
Hi Waqar,
I devised a solution that seems to be working. Basically, I'm using a Conditional block to determine if [wpv-post-type] is empty. If so, I'm assuming that the content in question is coming from BuddyPress, because post-types are returned for all other content on my site.
If you see any major flaws in my logic, please let me know. Otherwise, since you provided a solution to my original request, I'm closing out this ticket.
Thanks so much for your help!
Best,
Brian