Wordpress removes shortcode and saves it for use elsewhere

An attempt to remove the gallery label from the contents of the message and save it in a variable for use elsewhere in the template. The new Wordpress gallery tool is great for choosing which images they want and assigns signatures, hoping to use it to create the gallery, but then pull it out of the content on the interface.

So, these small sections are great for deleting the gallery and reusing formatting ... however I want to keep this short gallery code.

$content = strip_shortcodes( get_the_content() ); $content = apply_filters('the_content', $content); echo $content; 

Hoping to keep the short code so that it can be parsed in an array and used to recreate the gallery’s user preferences in the interface. An example of this short code I'm trying to save is ...

[gallery ids="1079,1073,1074,1075,1078"]

Any suggestions are welcome.

+4
source share
3 answers

The function to capture the short code of the first gallery from the message:

 // Return first gallery shortcode function get_shortcode_gallery ( $post = 0 ) { if ( $post = get_post($post) ) { $post_gallery = get_post_gallery($post, false); if ( ! empty($post_gallery) ) { $shortcode = "[gallery"; foreach ( $post_gallery as $att => $val ) { if ( $att !== 'src') { if ( $att === 'size') $val = "full"; // Set custom attribute value $shortcode .= " ". $att .'="'. $val .'"'; // Add attribute name and value ( attribute="value") } } $shortcode .= "]"; return $shortcode; } } } // Example of how to use: echo do_shortcode( get_shortcode_gallery() ); 

The function to remove the barcode of the first gallery from the mail content:

 // Deletes first gallery shortcode and returns content function strip_shortcode_gallery( $content ) { preg_match_all( '/'. get_shortcode_regex() .'/s', $content, $matches, PREG_SET_ORDER ); if ( ! empty( $matches ) ) { foreach ( $matches as $shortcode ) { if ( 'gallery' === $shortcode[2] ) { $pos = strpos( $content, $shortcode[0] ); if ($pos !== false) return substr_replace( $content, '', $pos, strlen($shortcode[0]) ); } } } return $content; } // Example of how to use: $content = strip_shortcode_gallery( get_the_content() ); // Delete first gallery shortcode from post content $content = str_replace( ']]>', ']]>', apply_filters( 'the_content', $content ) ); // Apply filter to achieve the same output that the_content() returns echo $content; 
+6
source

just use get_shortcode_regex ():

 <?php $pattern = get_shortcode_regex(); preg_match_all('/'.$pattern.'/s', $post->post_content, $shortcodes); ?> 

which will return an array of all shortcodes in your content, which you can then print where you feel, for example:

 <?php echo do_shortcode($shortcodes[0][1]); ?> 

Similarly, you can use array entries to check for shortcodes in your content and delete them with str_replace ():

 <?php $content = $post->post_content; $content = str_replace($shortcodes[0][1],'',$content); ?> 
+2
source

Maybe something like $gallery = do_shortcode('[gallery]'); .

0
source

Source: https://habr.com/ru/post/1487373/


All Articles