Return only short code from message?

Is it possible to filter out only the short code from the message, and then run the short code?

My page looks like this:

[summary img="images/latest.jpg"] This here is the summary [/summary] Lots of text here... 

And I just want to display a shortcode on a specific page.

Tried using regular expressions, but they don't seem to work:

 $the_query = new WP_Query('category_name=projects&showposts=1'); //loop while ( $the_query->have_posts() ) : $the_query->the_post(); echo '<b>'; the_title(); echo '</b>'; echo '<p>'; $string = get_the_content(); if (preg_match("[\\[[a-zA-Z]+\\][a-zA-Z ]*\\[\/[a-zA-Z]+\\]]", $string , $matches)) { echo "Match was found <br />"; echo $matches[0]; } echo '</p>'; endwhile; 

Any idรฉas?

EDIT:

Find a workaround.

 while ( $the_query->have_posts() ) : $the_query->the_post(); $content = str_replace(strip_shortcodes(get_the_content()),"",get_the_content()); echo do_shortcode($content); endwhile; 

I saw that wordpress had a function for marking up short codes, but not for the contents of the strip. So I replaced the line of split content with the whole post to get only a short code. The only bad thing is that shortcodes should be at the beginning of messages.

+4
source share
5 answers

use this regex

preg_match('/\[summary[^\]]*](.*)\[\/summary[^\]]*]/uis', $string , $matches)

$ match [1] should be your text

Edit: ok to match any tag combination use

/\[([^\]]+)\](.*?)\[\/\1\]/uis

but if you have nested tags, you may need to parse the matches again. However, if this is text text in Wordpress, I think you might get too complicated cases where a simple script can handle

0
source

The best answer:

 $pattern = get_shortcode_regex(); preg_match('/'.$pattern.'/s', $post->post_content, $matches); if (is_array($matches) && $matches[2] == 'the_shortcode_name') { $shortcode = $matches[0]; echo do_shortcode($shortcode); } 

It will look for the contents of the short code message called "the_shortcode_name". If he finds it, he will save the short code in the $ matches variable. Easy to run it from there.

+11
source

I had the following problems with these answers:

  • regex pattern that does not contain all registered shortcode tags
  • inability to get all short codes from message

.. and my solution:

 // Return all shortcodes from the post function _get_shortcodes( $the_content ) { $shortcode = ""; $pattern = get_shortcode_regex(); preg_match_all('/'.$pattern.'/uis', $the_content, $matches); for ( $i=0; $i < 40; $i++ ) { if ( isset( $matches[0][$i] ) ) { $shortcode .= $matches[0][$i]; } } return $shortcode; } 

Use it like this:

 <?php echo do_shortcode( _get_shortcodes( get_the_content() ) ) ?> 
+2
source

Search where your shortcode position is in your content

Example:

 [my_shortcode] [my_shortcode_2] 

or

 [my_shortcode_2] [my_shortcode] 

functions.php

 function get_shortcode($code,$content) { $pattern = get_shortcode_regex(); preg_match_all('/'.$pattern.'/s',$content,$matches); if(is_array($matches) && isset($matches[2]) && in_array($code,$matches[2])) { $index = array_search($code,$matches[2]); $shortcode = $matches[0][$index];; return do_shortcode($shortcode); } else { return false; } } 

Usage example:

 $content = $post->post_content; $my_shortcode = get_shortcode('my_shortcode',$content); $my_shortcode_2 = get_shortcode('my_shortcode_2',$content); echo $my_shortcode; if($my_shortcode){ // if shortcode#1 found so echo shortcode#2 after echo $my_shortcode_2; } 
+1
source

You can do this with the get_shortcode_regex() function to get a regular expression for all the registered barcode codes on your blog, apply this to the content to get an array of shortcodes found in this content, and then call the appropriate callback to process one you want.

So, inside the loop somewhere, it will be something like:

 $pattern = get_shortcode_regex(); $matches = array(); preg_match_all("/$pattern/s", get_the_content(), $matches); var_dump($matches); //Nested array of matches //Do the first shortcode echo preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $matches[0][0] ); 

This will execute the first short code found in the message, obviously in practice you want to check that you have one!

-1
source

All Articles