Display Content of Shortcode in WYSIWYG Field on Frontend

Support General Display Content of Shortcode in WYSIWYG Field on FrontendResolved

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #45700
    saintsaint
    Participant

    Hi,

    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.

    #45703
    saintsaint
    Participant

    Other 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' );
    
    ?>
    #45715
    PeterPeter
    Moderator

    Hello,

    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 ) );
    #45721
    saintsaint
    Participant

    Hi 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/BluvbvAq

    2. 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/8LuNqNAX

    4. This is what I saw on the frontend:
    https://share.zight.com/xQuoRJlj

    5. Tested with a form plugin shortcode for a side-by-side comparison:
    https://share.zight.com/Kouk98d9

    6. Frontend with MB and form plugin shortcode:
    https://share.zight.com/qGubqn5Z

    Could there be a step I missed during the implementation?

    #45729
    PeterPeter
    Moderator

    Hello,

    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/

    #45755
    saintsaint
    Participant

    Hi 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/04u8vXLW

    Re: #3 - Yes, it is a custom field of the current post.

    #45762
    PeterPeter
    Moderator

    Hello,

    Please share your site credentials by submitting this contact form https://metabox.io/contact/
    I will take a look.

    #45767
    saintsaint
    Participant

    Hi 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.

    #45768
    saintsaint
    Participant

    As an addendum: They are aware of this issue and will likely address it in one of the upcoming releases.

    #45779
    PeterPeter
    Moderator

    So the issue arises from the Bricks builder, not from Meta Box itself. Thanks for sharing the solution.

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.