Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created February 21, 2019 20:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/e1f9ea8567020a836776b62fcba9ef0b to your computer and use it in GitHub Desktop.
Save billerickson/e1f9ea8567020a836776b62fcba9ef0b to your computer and use it in GitHub Desktop.
<?php
/**
* Find Shortcode
*
*/
function be_find_shortcode( $atts = array() ) {
$atts = shortcode_atts( array(
'shortcode' => '',
), $atts, 'find_shortcode' );
$loop = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => array( 'post', 'page' ),
'orderby' => 'title',
'order' => 'ASC'
));
$found = array();
if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post();
global $post;
if( has_shortcode( $post->post_content, $atts['shortcode'] ) )
$found[] = '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
endwhile; endif; wp_reset_postdata();
if( !empty( $found ) ) {
$output = '<h3>Pages with the [' . $atts['shortcode'] . '] shortcode</h3><ul>' . join( PHP_EOL, $found ) . '</ul>';
} else {
$output = '<h3>No pages found with [' . $atts['shortcode'] . '] shortcode</h3>';
}
return $output;
}
add_shortcode( 'find_shortcode', 'be_find_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment