Using has_shortcode with cs content

I’m using a raw content element with one custom shortcode. Withing the plugin which provides this shortcode I want to use wp’s has_shortcode to enqueue js/css.

But unfortunately $post->post_content doesn’t contain the shortcode, so the function ist not working properly (of course). Is it possible to get the ‘final’ cs content (which contains the sc)?

Thanks in advance

Hello @philipp_halla,

Thanks for writing in!

The most common use of has_shortcode should always be this:

function custom_shortcode_scripts() {
	global $post;
	if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'custom-shortcode') ) {
		wp_enqueue_script( 'custom-script');
	}
}
add_action( 'wp_enqueue_scripts', 'custom_shortcode_scripts');

This should work when adding any shortcode inside a raw content element or with a classic text element. Just do not place your shortcode inside the v2 text element because it’s content will be inserted in a post meta and not in the post content. I would recommend that you output the whole $post->post_content like print_r($post->post_content) to check whether your shortcode is present or not.

If you would be kind enough, please also provide your codes in your next reply so that we can check it.

Regards.

Thank you for your response. Yes, I’m using the function this way (as expained in official WP Dev Documentation).
print_r shows the following:

[cs_content][cs_element_section _id=“1” ][cs_element_row _id=“2” ][cs_element_column _id=“3” ][cs_element_headline _id=“4” ][cs_element_text _id=“5” ][cs_element_content_area _id=“6” ][/cs_element_column][/cs_element_row][/cs_element_section][/cs_content]

The shortcode should be inside the content area.

Thanks in advance

Hi @philipp_halla,

Please add your shortcode within the classic text element as Nel recommended. The content area element is a v2 element where data are inaccessible outside the page where it’s created.

Thanks!

Thank you very much. Works like a charm!

You are most welcome. :slight_smile:

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