Shortcodes in Pagebuilder

I’m using shortcodes inside classic text elements. The pagebuilder layout often gets broken when trying to render the shortcode’s markup/output. I would like them not to be ‘executed’.
Shortcodes come from a custom plugin, I tried to initialize it only if

! is_admin()

but this doesn’t seem to work. Doesn’t this function return true when using the cornerstone page builder?

Thanks in advance

Hello @philipp_halla,

is_admin() condition which refers to the WordPress dashboard or when you are logged in. When editing in Cornerstone, you are also logged in so the condition will return true as well.

Hope this helps.

is_admin() doesn’t check whether you are logged in or not, as explained in official documentation:

is_admin() is not intended to be used for security checks. It will return true whenever the current URL is for a page on the admin side of WordPress. It does not check if the user is logged in, nor if the user even has access to the page being requested. It is a convenience function for plugins and themes to use for various purposes, but it is not suitable for validating secured requests.

Hey Philipp,

Sorry for the confusion. is_admin does not return true in Cornerstone. Please use: did_action('cs_element_rendering') instead.

Cornerstone:

Frontend:

Hope that helps.

Hi @philipp_halla,

You can achieve that something like these

global $no_preview_for_this_shortcode = false;

add_action('cs_before_preview_frame', 'no_preview_for_this_shortcode', -9999);

function no_preview_for_this_shortcode () {

global $no_preview_for_this_shortcode;

$no_preview_for_this_shortcode = true;

}


//Your shortcode here

add_shortcode('this_is_my_shortcode', 'this_is_my_shortcode');

function this_is_my_shortcode () {

global $no_preview_for_this_shortcode;

if( $no_preview_for_this_shortcode ) return false; //Simply exit the function, no rendering.

//And do your shortcode's internal here

}

This is just an idea and not a complete solution, you’re free to enhance it but we couldn’t provide custom development related to this. Another solution would be changing your !is_admin() condition similar to this

    if ( ! isset( $_POST['cs_preview_state'] ) || ! $_POST['cs_preview_state'] || 'off' === $_POST['cs_preview_state'] ) {

      //do your shortcode's internal here

    }

Admin or logged in, it may not work within the preview since it’s mixed. The only way to make it happen is by checking if it’s a preview request from the builder.

Thanks!

Thank you. did_action( ‘cs_template_rendering’) did the job.

You are most welcome. :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.