Skip to content

Instantly share code, notes, and snippets.

@keesiemeijer
Last active January 17, 2018 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keesiemeijer/2bcf218b9a7b2d138688 to your computer and use it in GitHub Desktop.
Save keesiemeijer/2bcf218b9a7b2d138688 to your computer and use it in GitHub Desktop.
Shortcode Regex Finder
<?php
/**
* Plugin Name: Shortcode Regex Finder
* Plugin URI:
* Description: This plugin creates the regex WordPress would use to find a specific shortcode in content.
* Author: keesiemijer
* License: GPL v2
* Version: 1.0
* Text Domain: shortcode-regex-finder
*/
// Load the textdomain
add_action( 'init', 'pluginception_load_textdomain' );
function pluginception_load_textdomain() {
load_plugin_textdomain( 'shortcode-regex-finder', false, dirname( plugin_basename( __FILE__ ) ) );
}
add_action( 'admin_menu', 'shortcode_regex_finder_admin_menu' );
function shortcode_regex_finder_admin_menu() {
add_options_page(
__( 'Shortcode Regex Finder', 'shortcode-regex-finder' ),
__( 'Shortcode Regex Finder', 'shortcode-regex-finder' ),
'manage_options',
'shortcode-regex-finder.php',
'shortcode_regex_finder_admin_page' );
}
function shortcode_regex_finder_admin_page() {
$results = shortcode_regex_finder_get_regex();
$value = isset( $results['shortcode'] ) ? $results['shortcode'] : '';
echo '<div class="wrap">';
echo '<h2>' . __( 'Shortcode Regex Finder', 'pluginception' ) . '</h2>';
echo '<p>' . __( 'This plugin creates the same regular expression WordPress would use to find a specific shortcode.', 'pluginception' ) . '</p>';
settings_errors();
echo '<form method="post" action="">';
wp_nonce_field( 'shortcode_regex_finder_nonce' );
echo "<table class='form-table'><tr valign='top'><th scope='row'>Shortcode Name</th>";
echo "<td><input class='regular-text' type='text' name='shortcode_search' value='{$value}'>";
echo "<p class='description'>Shortcode name <strong>without</strong> the brackets.</p></td></tr></table>\n";
submit_button( __( 'Create the shortcode regex!', 'shortcode-regex-finder' ) );
echo '</form>';
if ( $results && is_array( $results ) ) {
echo '<p>' . sprintf( 'Regex for the shortcode: %s', '<strong>' . $results['shortcode'] . '</strong>' ) . '</p>';
echo '<pre style="border:1px solid #ddd;padding:1em;background-color:#fff;">';
print_r( $results['regex'] );
echo '</pre>';
}
echo '</div>';
}
function shortcode_regex_finder_get_regex() {
if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
return false;
}
check_admin_referer( 'shortcode_regex_finder_nonce' );
$_POST = stripslashes_deep( $_POST );
if ( isset( $_POST['shortcode_search'] ) ) {
$shortcode = sanitize_text_field ( trim( $_POST['shortcode_search'] ) );
if ( empty( $shortcode ) ) {
add_settings_error( 'shortcode_regex_finder', 'shortcode_search', __( 'Please submit a shortcode name', 'shortcode-regex-finder' ), 'error' );
return false;
}
global $shortcode_tags;
$temp_shortcode_tags = $shortcode_tags;
$shortcode_tags = array( $shortcode => '' );
$regex = '/' . get_shortcode_regex() . '/s';
$shortcode_tags = $temp_shortcode_tags;
add_settings_error( 'shortcode_regex_finder', 'shortcode_search', sprintf( 'Regex created for the shortcode: %s', '<strong>' . $shortcode . '</strong>' ), 'update' );
return array( 'regex' => $regex, 'shortcode' => $shortcode );
}
return false;
}
@keesiemeijer
Copy link
Author

This plugin finds the regex used by WordPress to find a single shortcode in post content. See https://keesiemeijer.wordpress.com/2015/05/25/how-to-remove-specific-shortcodes-from-post-content/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment