Skip to content

Instantly share code, notes, and snippets.

@Jany-M
Last active December 7, 2016 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jany-M/00d72d9a8a8b075b6b09c689d069b88b to your computer and use it in GitHub Desktop.
Save Jany-M/00d72d9a8a8b075b6b09c689d069b88b to your computer and use it in GitHub Desktop.
[WP] Parse content for shortcode, extract & show it somewhere else, delete it from content
<?php
/* Example with Visual Composer and an add-on for Slides
|
| We want to remove the slider from the post content,
| while keeping it in the backend post editor VC content,
| then place it somewhere else, like the header.
|
| In our case, the slider always sits at the top of the
| post content editor, as first item, in its own row.
|_____________________________________________________________*/
// in header.php
global $post;
setup_postdata( $post );
$fullstring = get_the_content($post->ID);
$arr = array_map('trim',array_filter(explode('[/vc_row]',$fullstring),'strlen'));
if(strpos( $arr[0], 's_slider' ) !== false ) {
echo '<div class="container slider_extracted">';
echo do_shortcode('[vc_row]'.$arr[0].'[/vc_row]');
echo '</div>';
}
// in single.php
$fullstring = get_the_content($post->ID);
$arr = array_map('trim',array_filter(explode('[/vc_row]',$fullstring),'strlen'));
if(strpos( $arr[0], 's_slider' ) !== false ) {
$remove_slider = $arr[0].'[/vc_row]';
$new_content = str_ireplace($remove_slider, '', $fullstring);
}
if(strpos( $arr[0], 's_slider' ) !== false ) {
echo wpautop(do_shortcode($new_content));
} else {
the_content();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment