I'm using has_shortcode()
to detect a shortcode, it works but not at all. If I put this specified shortcode inside another one the has_shortcode()
function stops working.
has_shortcode( $post->post_content, 'slider' )
For example:
[2col] //left column
[slider]
[2col_next] //right column
[slider]
[/2col]
The has_shortcode()
function won't work in that case but if I use [slider]
shortcode without [2col]
it works perfect. This refers to every shortcode. I'm pretty sure that there's nothing wrong with my shortcodes.
Was looking for a solution to this just now. If anyone else is still looking for it then it has been answered here:
https://wordpress.stackexchange.com/questions/126563/has-shortcode-how-to-detect-nested-shortcode
Reworked translation of above link in case it becomes broken:
function is_shortcode_active() {
global $post;
if ( strpos( $post->post_content, '[shortcode' ) ) {
return true;
}
return false;
}
I changed it a bit from the original linked answer. I just used the $post
object instead of other functions and I also removed the /
after the opening bracket and left off the closing bracket completely. The reason for leaving off the ending bracket is in case you are tyring to find a shortcode in which users may have added attributes to - if the ]
is there then it won't match in those instances.