-
-
Save billerickson/e1f9ea8567020a836776b62fcba9ef0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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