I have a shortcode: [content_block]

If that shortcode exists in content and has the attribute position=top, I would like to remove it from the content and display it in other place. For example, in sidebar.

In summary:

  • Detect if [content_block] exists
  • If exists, detect if has the attibute position=top
  • If has the attribute, then store the complete shortcode to display it in a different place (for example in the sidebar).
  • And remove it from the content.

How can I do it?



You can look to the code WordPress uses for parsing Shortcodes to get some ideas about how to do this. There are a couple of helpful core functions to simplify things.

// our callback function to check for the shortcode.
// this is where you'd put code to set a flag for rendering the shortcode elsewhere
// the complete tag is in $tag[0], which can be passed to do_shortcode later
function wpd_check_shortcode( $tag ){
    if( 'content_block' == $tag[2] ){
        // convert string of attributes to array of key=>val pairs
        $attributes = shortcode_parse_atts( $tag[3] );
        if( array_key_exists( 'position', $attributes )
            && 'top' == $attributes['position'] ){
                // move shortcode to sidebar here
                // return empty to strip shortcode
                return '';
    // return any non-matching shortcodes
    return $tag[0];

// source text with shortcode
$text = 'lorem ipsum [content_block]';

// this generates a regex pattern for all registered shortcodes
$pattern = get_shortcode_regex();

// process all shortcodes through our callback function
$text = preg_replace_callback( "/$pattern/s", 'wpd_check_shortcode', $text );

// modified $text with shortcode removed
echo $text;
  • Thank you very much for your answer. But how can I display the shortcode in the sidebar? – user1443216 Nov 18 '14 at 19:06
  • do_shortcode() @user1443216 – Nicolai Nov 18 '14 at 19:09
  • put the shortcode in a var, it will be in $tag[0], or better, wrap the whole thing in a class and set a class var, then output that when in your sidebar by passing it to do_shortcode. – Milo Nov 18 '14 at 19:52
  • Sorry, but I don't know how to display it using $tag[0] in my sidebar. – user1443216 Nov 19 '14 at 21:49
add_filter( 'the_content', 'wpse168789_shortcode_checker' );
function wpse168789_shortcode_checker( $content ) {
    // 1. Does the shortcode exist?
    if( has_shortcode( $content, 'content_block' ) ) {
        // 2. Is it using position='top'?
        $regex = '/\[content_block[^\]]*position=[\'"]?top[\'"]?[^\]]*\]/';
        if( preg_match( $regex, $content ) ) {
                // 3. Filter the shortcode out of the_content
                $content = preg_replace( $regex, '', $content );
    return $content;

You will probably have to massage the regular expression $regex -- I haven't tested this.

As for displaying the shortcode's content in another location -- this question might guide you.


  • Thanks for your answer, but the regular expression fails. The error is: "Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING" – user1443216 Nov 18 '14 at 18:34
  • 1
    Whoops. I think that should now be fixed. – Pat J Nov 18 '14 at 19:23
  • Ok. There is no errors now. But if I have [content_block position="top" colour="blue" size="medium"] your function remove the shortcode from the content. That is correct, but now I would like to save that shortcode to display it for example in the sidebar. How can I do it? – user1443216 Nov 18 '14 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.