If you recently changed your theme and used shortcodes from it or disabled plugins you maybe want to find all leftovers of them because they are not translated any more. You will find things like [shortcode attribute=”bla”] all over website. Obviously you don’t want to review each post on your website to find them manually. So there comes a little snippet into the game:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | /** * Finds broken shortcodes. * @author No3x */ function find_broken_shortcodes() { //TODO: support pages - $results accessible for pages later on $results = array(); foreach( get_object_vars ( $count_posts = wp_count_posts() ) as $post_status => $count ) { for( $offset = 0; $offset < $count; $offset+= 160 ) { foreach( get_posts( array( 'posts_per_page' => '150', 'post_status' => $post_status, 'offset' => $offset ) ) as $post ) { $content = do_shortcode( $post->post_content ); if ( ( stripos( $content, '[' ) !== FALSE ) ) { $results[] = $post; } } } } if( count($results) ) { echo "<ul>"; foreach( $results as $key => $post ) { echo '<li>Found untranslated shortcode at <a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></li>'; } echo "</ul>"; } } add_action('wp_footer', 'find_broken_shortcodes'); |
This will hook into your footer and print all posts where a broken shortcode was found. Just copy it (for example) into your active theme functions.php. You will find the output as mentioned in the footer. Example output:
Now you can find and replace this shortcodes. The snippet just can find the [ so maybe you get wrong results but it’s still better than manually searching. Keep in mind that you can’t search for broken shortcodes through full text search or mysql because you don’t know if the shortcode is translated ( because shortcodes are translated (for example from [shortcode] to actual <span …>…</span>) by php at runtime). Pages are currently not supported but let me know if you need this.
This worked great. Thanks for the code.