Display Content of Shortcode in WYSIWYG Field on Frontend
- This topic has 9 replies, 2 voices, and was last updated 9 months, 3 weeks ago by
Peter.
-
AuthorPosts
-
June 25, 2024 at 8:42 AM #45700
saint
ParticipantHi,
I have been trying to get a shortcode in a WYSIWYG editor displayed on the frontend of Bricks Builder. I have read https://support.metabox.io/topic/output-shortcode-from-a-custom-field/, and have tried the following:
1.) Attempted to add a filter function to try and render the field. See:
https://share.zight.com/Kouk9wgW
2.) Tried various different methods to see which one would output the correct values, and so far the only values I can see displaying are basic texts in the WYSIWYG field - not the shortcode.
https://share.zight.com/NQuAW1yz
Can you provide a step-by-step example of how you would output a real-life example shortcode? In my case, it is a form.
Thank you.
June 25, 2024 at 8:46 AM #45703saint
ParticipantOther attempts tried:
1) [rwmb_meta id="mb_resource_resource_offer_cta_box" render_shortcode="true"]
2) {echo:resource_form_sc}<?php add_filter( 'rwmb_get_value', function( $value, $field, $args, $object_id ) { if ( 'wysiwyg' === $field['type'] ) { $value = do_shortcode( $value ); } return $value; }, 10, 4 ); ?>
<?php function offer() { $value = rwmb_meta( 'mb_resource_resource_offer_cta_box' ); echo do_shortcode( wpautop( $value ) ); } ?>
<?php $value = rwmb_meta( 'mb_resource_resource_offer_cta_box' ); echo do_shortcode( wpautop( $value ) ); ?>
<?php echo do_shortcode( 'mb_resource_resource_offer_cta_box' ); ?>
June 25, 2024 at 10:34 PM #45715Peter
ModeratorHello,
Please let me know if it works when you:
- add the helper shortcode[rwmb_meta id="mb_resource_resource_offer_cta_box"]
directly to the page content without using Bricks or any third-party plugin.- use the PHP code and add it to the template of a WordPress theme.
$value = rwmb_meta( 'mb_resource_resource_offer_cta_box' ); echo do_shortcode( wpautop( $value ) );
June 26, 2024 at 2:12 AM #45721saint
ParticipantHi Peter,
Thanks for your reply. I've tried the above suggestion, but may be implementing it incorrectly? Here are the steps I took:
1. Activated and switched over to the Twenty-Twenty Two theme, then added the code into the functions.php file:
https://share.zight.com/BluvbvAq2. De-activated all plugins except Metabox, Classic Editor, and a form plugin for testing purposes
3. Added the helper shortcode into the Post Content area of the test record in the backend:
https://share.zight.com/8LuNqNAX4. This is what I saw on the frontend:
https://share.zight.com/xQuoRJlj5. Tested with a form plugin shortcode for a side-by-side comparison:
https://share.zight.com/Kouk98d96. Frontend with MB and form plugin shortcode:
https://share.zight.com/qGubqn5ZCould there be a step I missed during the implementation?
June 26, 2024 at 10:23 PM #45729Peter
ModeratorHello,
1. You have to add the code to the template of the theme, not the file functions.php. For example: Twenty-Twenty One theme, file wp-content/themes/twentytwentyone/template-parts/content/content-single.php
3. Is the field "mb_resource_resource_offer_cta_box" a custom field of the current post to which you add the shortcode?
If you don't add the "object_id" attribute to the helper shortcode, the current post ID will be used.Following the documentation
https://docs.metabox.io/shortcode/
https://docs.metabox.io/displaying-fields-with-code/June 28, 2024 at 5:43 AM #45755saint
ParticipantHi Peter,
Sure, I've updated the site to reflect your feedback.
Re: #1 -
$value = rwmb_meta( 'mb_resource_resource_offer_cta_box' ); echo do_shortcode( wpautop( $value ) );
Is now added to
content-single.php
template for TwentyTwentyOne.https://share.zight.com/8LuNYO9D
When I added:
[rwmb_meta id="resource_offer_cta_box"] Post Content Here.
to Post Content, I see:Frontend: https://share.zight.com/v1u9WpLY
Backend: https://share.zight.com/04u8vXLWRe: #3 - Yes, it is a custom field of the current post.
June 28, 2024 at 10:06 PM #45762Peter
ModeratorHello,
Please share your site credentials by submitting this contact form https://metabox.io/contact/
I will take a look.June 29, 2024 at 1:43 AM #45767saint
ParticipantHi Peter,
Thank you for your help! A quick update for you and for others that may stumble upon this when running into the same situation.
From what I understand, in Bricks Builder, the issue is that all dynamic tags will be sanitized before using on an element.
The workaround to have a form shortcode render from a Metabox WYSIWYG field on the frontend when using Bricks Builder is:
add_filter( 'wp_kses_allowed_html', function( $tags, $context ) { if ( ! isset( $tags['form'] ) ) { $tags['form'] = [ 'data-id' => true, 'id' => true, 'class' => true, ]; } return $tags; }, 100, 2 );
Can add this filter as a code snippet and it should render the output correctly. I've tested this in production and can confirm the solution works.
June 29, 2024 at 1:43 AM #45768saint
ParticipantAs an addendum: They are aware of this issue and will likely address it in one of the upcoming releases.
June 30, 2024 at 2:49 PM #45779Peter
ModeratorSo the issue arises from the Bricks builder, not from Meta Box itself. Thanks for sharing the solution.
-
AuthorPosts
- You must be logged in to reply to this topic.