0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
/*
Plugin Name: WP Turbo Shortcode Search and Replace
Plugin URI: https://wpturbo.dev
Description: Automates the process of scanning and replacing shortcodes within posts.
Version: 1.0.0
Author: WP Turbo
Author URI: https://wpturbo.dev
License: GPL2
Text Domain: wpturbo
*/
// Hook into the admin menu
add_action('admin_menu', 'wpturbo_shortcode_search_replace_admin_menu');
function wpturbo_shortcode_search_replace_admin_menu() {
// Add a new submenu under the "Tools" menu
add_submenu_page(
'tools.php',
'Shortcode Search and Replace',
'Shortcode Search and Replace',
'manage_options',
'wpturbo-shortcode-search-replace',
'wpturbo_shortcode_search_replace_page'
);
}
function wpturbo_shortcode_search_replace_page() {
// Handle form submission
if (isset($_POST['wpturbo_shortcode_scan_submit'])) {
// Perform the scan operation in the database
global $wpdb;
$shortcode_pattern = '/[[^]]+]/'; // Regular expression pattern to match shortcodes
$posts = $wpdb->get_results(
"SELECT ID, post_content FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'"
);
$detected_shortcodes = array();
foreach ($posts as $post) {
preg_match_all($shortcode_pattern, $post->post_content, $matches);
if (!empty($matches[0])) {
$detected_shortcodes[$post->ID] = $matches[0];
}
}
// Display the detected shortcodes
echo '<div class="wrap">';
echo '<h1>Detected Shortcodes</h1>';
if (empty($detected_shortcodes)) {
echo '<p>No shortcodes detected in the posts.</p>';
} else {
foreach ($detected_shortcodes as $post_id => $shortcodes) {
echo '<h2>Post ID: ' . $post_id . '</h2>';
echo '<ul>';
foreach ($shortcodes as $shortcode) {
echo '<li>' . $shortcode . '</li>';
}
echo '</ul>';
}
}
echo '<form method="post" style="margin-top: 20px;">';
echo '<input type="hidden" name="wpturbo_shortcode_remove_submit" value="true">';
echo '<input type="submit" name="wpturbo_shortcode_remove_submit" class="button button-primary" value="Remove All Shortcodes">';
echo '</form>';
echo '</div>';
} elseif (isset($_POST['wpturbo_shortcode_remove_submit'])) {
// Perform the remove operation in the database
global $wpdb;
$shortcode_pattern = '[[]]]'; // Regular expression pattern to match shortcodes
$posts = $wpdb->get_results(
"SELECT ID, post_content FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'"
);
foreach ($posts as $post) {
$post_content = preg_replace($shortcode_pattern, '', $post->post_content);
wp_update_post(array(
'ID' => $post->ID,
'post_content' => $post_content
));
}
// Display success message
echo '<div class="notice notice-success"><p>All shortcodes have been removed.</p></div>';
}
// Display the scan form
echo '<div class="wrap">';
echo '<h1>Shortcode Search and Replace</h1>';
echo '<form method="post">';
echo '<input type="submit" name="wpturbo_shortcode_scan_submit" class="button" value="Scan for Shortcodes">';
echo '</form>';
echo '</div>';
}
In the SQL queries, I added post_type = 'post' to filter for the 'post' post type only. This ensures that the scanning and removal of shortcodes will only be applied to posts.
Now, the code will scan and remove shortcodes only from posts, excluding other post types such as pages and custom post types.
Please note that this code assumes the standard post type is 'post'. If you're using a custom post type, you'll need to update the post_type value accordingly.