Site logo

Archived topic

Shortcodes in elements?

22 replies · Started by Matthew on January 8, 2019

Viewing posts 1–15 of 23

I have ultimate shortcode plugin installed and I have the following in a GP element:

<?php
$out = 'hello';
echo do_shortcode('[su_spoiler title="Spoiler title" open="no" style="default" icon="plus" anchor="" class=""]'.$out.'[/su_spoiler]');
?>

On output, the shortcode works (a click-expand thing), but the content inside the shortcode literally writes (including single quotes)

‘.$out.’

on the display... why is this happening? I obviously have both shortcode and execute php boxes checked.

Hi there,

Hmm I'm not sure if you can use PHP between the shortcode like that.

Can you check with Ultimate Shortcode's support to see if this is indeed the correct usage?

Hooks don't do anything special to your code - it just displays whatever you have in there.

I would assume there is either a syntax error or you can't use it like that. The plugin support should be able to tell you that.

Okay, I looked into it more and it's weird... if you put just what I pasted into an element, it spits out "hello" like it should. But if you add a function call, it doesn't.

For example...

The following renders the shortcode function but prints '$out' literally.

<?php
$postcat = get_the_category( $query->post->ID);
$out= 'hello';
echo do_shortcode('[su_spoiler title="Spoiler title" open="no" style="default" icon="plus"]'.$out.'[/su_spoiler]');
?>

The following renders the shortcode function but prints '$out' literally.

<?php
$query = new WP_Query(array('tag_id'=>99));
$out= 'hello';
echo do_shortcode('[su_spoiler title="Spoiler title" open="no" style="default" icon="plus"]'.$out.'[/su_spoiler]');
?>

The following renders the shortcode function and prints hello inside.

<?php
$out= 'hello';
echo do_shortcode('[su_spoiler title="Spoiler title" open="no" style="default" icon="plus"]'.$out.'[/su_spoiler]');
?>

Do you have any idea why calling a function inside of an element like this causes do_shortcode to break?

I solved it. I don't understand why it works, but it works.

<?php
$query = new WP_Query(array('tag_id'=>99));
?>
<?php
$out= 'hello';
echo do_shortcode('[su_spoiler title="Spoiler title" open="no" style="default" icon="plus"]'.$out.'[/su_spoiler]');
?>

By separating the two using PHP opening and closing tags, it solves the issue and prints "hello" for $out. Totally weird.

Glad you figured out!

Are shortcode allowed in Elements? I have a shortcode for my real estate search module. It works in normal page sections, but in my home page element it just types out {wp-realty searchpage} as text.

Hi there,

yes the Header Element supports shortcodes.
The hook element does also but you need to check the Execute Shortcodes box for them to work.

Thank you!

Where can I find said "Execute Shortcode" checkbox?

So shortcodes cannot run in Elements... only in Hooks?
Your second answer contradicts your first answer.

Header Elements - they are on by default, no checkbox required.
Hook Elements - require the checkbox to be activated - its an extra layer of security.

That is my problem. The Element is simply displaying the shortcode "{ wp-realty searchpage }" as text.

The shortcode is not being parsed by the Element. How can I troubleshoot this issue?

Check your shortcode they should look like:

[shortcode] instead of {shortcode}

If still an issue test the shortcode in the page editor to eliminate a problem with the shortcode itself.

I tried that. The Element displays "[wp-realty searchpage]" as text.

I am using the shortcode on several pages without issue. It is only in the Element that it does not display correctly.

https://saltlakecityrealty.net/

Thank you for helping me with this issue.

Would you be able to provide us with temporary admin access so i can take a look?

You can send login details, with the URL to this topic for tracking to the Account Issue form:

https://generatepress.com/contact/

This archived topic is closed to new replies.