Skip Navigation

[Resolved] WYSIWYG field does not render shortcodes?

This thread is resolved. Here is a description of the problem and solution.

Problem: I have inserted a shortcode in a WYSIWYG field's contents, but when I try to output the WYSIWYG field using PHP, the shortcode is simply written to the page. I would like to echo the WYSIWYG field and render the shortcodes in its content.

Solution: Use the do_shortcode function to parse the WYSIWYG field contents and render the interior shortcodes:

$field = get_post_meta(12345, 'wpcf-wysiwyg', true);
echo do_shortcode($field);

Relevant Documentation:
https://developer.wordpress.org/reference/functions/do_shortcode/
https://codex.wordpress.org/Function_Reference/get_post_meta

This support ticket is created 6 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 11 replies, has 3 voices.

Last updated by Valeriia 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#914155

Hi,

It looks like the WYSIWYG field does not render shortcodes. Is that so or am I doing something wrong? Is there a way to make it render shortcodes?

Thanks.

#914215

This is so strange, I get this report like very 6 months or so.

And never I can even closely replicate this, but always the one who reports clearly can show me how it fails open their site.

So here it is again - my findings from the current tests:
- Toolset (and any other properly coded ShortCode)... work fine when inserted in a WYSIWYG if that WYSIWYG is rendered again inside for example a post body, thru a ShortCode.

I start to believe, that this reports are about the ShortCodes you insert in the WYSIWYG as seen in the backend.
There, logically these will never expand, as they will be content, just as if you would insert them in a Post body and view the Post body in the backend - the ShortCodes would logically not expand there.

Can you help me to completely understand this issue once and for all so I can then help you to either adjust it on your end, or eventually change the setup?

This is what I tested - maybe I did it wrong:
- Create a WYSIWYG Field (field-one)
- Create any other kind of field (field-two)
- Edit a Post and add values to (field-two)
- Add the ShortCode to display (field-two) in the WYSIWYG (field-one) using "Fields and Views" GUI
- Add the ShortCode to display the WYSIWYG (field-one) in the Post Body
- View the post, and see the content you added to (field-two).

This works fine on my tests, and always did, but some users did not agree, although I never had proper steps listed.

Maybe this changes now!

Thanks

#914247

Hi Beda,

Thanks for the reply.

I tried your flow and it worked on Post. If I try to add any shortcode to the WYSIWYG of a CPT, it's not rendered.

First I was just dispaying the field via php, then I added a content template (everything via Toolset). Whatever works for Post, does not work for CPT.

#914248

Sorry, got totally confused in my templates, here is the correct result: it works only if I display it via Content Template.

Is there a way to make it render when added via php?

I tried apply_filters('the_content', $wysiwyg_field), but it didn't work.

Thanks.

#914249

I need to display some custom fields if some other custom post exists, and display the_content() if not. It's easier and faster to do in php. That's why I need to render those fields via php.

Thanks.

#914460

Hi, here's one approach you could try:

$field = get_post_meta(12345, 'wpcf-wysiwyg', true);
echo do_shortcode($field);

Replace 12345 with the actual post ID, and replace 'wpcf-wysiwyg' with the slug of the custom field, including the wpcf- prefix. If that doesn't work, I need to take a closer look.

#914634

Hi Christian,

No, it didn't work.

Thanks for helping.

#914879

Please provide login credentials here and let me know where I can see the problem on the front-end of the site.

#916284

I didn't see the code anywhere in this template file, so I added the modified code and it appears to be working:

...
$page2field = get_post_meta($lesson_id, 'wpcf-lesson-page-2', true);
...
echo do_shortcode($page2field);
...

Please take a look. You can see "HERE IS THE QUIZ: No items found", indicating the View in this WYSIWYG field is being output as expected. Are you trying to echo the shortcode somewhere else? What am I missing?

#916297

Hi Christian,

I see what you added, but I can't find where you added it... It's not in single-lesson.php, is it? You somehow managed to put it right in the middle of the post meta between taxonomies and posted_on, but I don't see it in the code...

I was trying to add it to single-lesson.php. Starting from the line 56:

$lesson_meta = get_post_meta($lesson_id, '', false); 
// lesson navigation
for ($i=1; $i <= 5; $i++) {
$page_content = (isset($lesson_meta['wpcf-lesson-page-' . $i][0])) ? $lesson_meta['wpcf-lesson-page-' . $i][0] : '';
if ($page_content)) {
echo '<div id="page_'.$i.'">' .do_shortcode($page_content). '<div class="lesson_nav flex even">';
... 
echo '</div></div>';
}
}

Thanks.

#916334

It's in single-lesson.php, I edited the file directly through wp-admin. Check lines 12 and 54. Did you pull down from the site to your local?

#916342

I got it. My bad. I bookmarked a folder with another theme in FileZilla and was making the changes in a completely different file. That's why you didn't find it there and it didn't work for me. Stupid. 🙂

Thank you for helping. 🙂