Skip to content

Instantly share code, notes, and snippets.

@smeric
Last active May 27, 2020 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smeric/4a90e341be8c1a9f4d75 to your computer and use it in GitHub Desktop.
Save smeric/4a90e341be8c1a9f4d75 to your computer and use it in GitHub Desktop.
WordPress plugin. Removes selected old slugs/permalinks from database.
<?php
/**
* Remove old slugs redirects.
*
* Removes selected old slugs/permalinks from database.
*
* @package WordPress_Remove_Old_Slugs_Redirects
* @author Sébastien Méric <sebastien.meric@gmail.com>
* @credit Algoritmika Ltd. <http://www.algoritmika.com>
* @license GPL-2.0+
* @link http://www.sebastien-meric.com/
* @copyright 2015 Sébastien Méric
*
* @wordpress-plugin
* Plugin Name: Remove old slugs redirects.
* Plugin URI: https://gist.github.com/smeric/4a90e341be8c1a9f4d75
* Description: Removes selected old slugs/permalinks from database. This plugin is a revamp of <a href="https://wordpress.org/plugins/remove-old-slugspermalinks/" target="_blank">WordPress Remove Old Slugs</a> from <a href="http://www.algoritmika.com" target="_blank">Algoritmika Ltd.</a>.
* Version: 0.2.0
* Author: Sébastien Méric <sebastien.meric@gmail.com>
* Author URI: http://www.sebastien-meric.com/
* Text Domain: remove-old-slugs-redirects
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Plugin class.
*/
if ( ! class_exists( 'WordPress_Remove_Old_Slugs_Redirects' ) ) {
class WordPress_Remove_Old_Slugs_Redirects{
/**
* Slug of the plugin.
*
* @since 0.1.0
* @access protected
* @var string
*/
protected $plugin_slug = 'remove-old-slugs-redirects';
/**
* Initialize the plugin by adding an administration page and menu item.
*
* @since 0.1.0
*/
public function __construct(){
// Add the options page and menu item.
add_action( 'admin_menu', array( $this, 'add_admin_page' ) );
}
/**
* Register the administration menu for this plugin into the WordPress Dashboard menu.
*
* @since 0.1.0
*/
public function add_admin_page(){
add_submenu_page(
'tools.php',
__( 'Remove old slugs redirections', $this->plugin_slug ),
__( 'Remove old slugs redirects', $this->plugin_slug ),
'manage_options',
$this->plugin_slug,
array( $this, 'create_admin_page' )
);
}
/**
* Render the administration page for this plugin.
*
* @since 0.1.0
*/
public function create_admin_page(){
global $wpdb;
?>
<div class="wrap">
<?php
if ( isset( $_POST['remove_old_slugs'] ) ) {
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'remove_old_slugs' ) ) {
wp_die( __( 'Failed security check', $this->plugin_slug ) );
}
$meta_id = array();
foreach ( $_POST['meta_id'] as $id ) {
$meta_id[] = ( int ) $id;
}
$query = $wpdb->prepare("
DELETE FROM {$wpdb->prefix}postmeta
WHERE meta_id in (" . implode( ',', $meta_id ) . ")
");
$results = $wpdb->query( $query );
if ( $results ) {
$slugs = _n( 'slug', 'slugs', $results, $this->plugin_slug );
?>
<div id="setting-error-settings_updated" class="updated settings-error">
<p><?php printf( __( '<strong>Removing old %1$s from database finished ! %2$s old %1$s deleted.</strong>', $this->plugin_slug ), $slugs, $results ) ?></p>
</div>
<?php
}
else {
?>
<div id="setting-error-settings_updated" class="error settings-error">
<p><?php _e( '<strong>No slug removed.</strong>', $this->plugin_slug ) ?></p>
</div>
<?php
}
}
$wp_old_slugs = $wpdb->get_results("
SELECT meta_id, post_id, meta_value
FROM {$wpdb->prefix}postmeta
WHERE meta_key = '_wp_old_slug'
");
$count = count( $wp_old_slugs );
?>
<h2><?php echo esc_html( get_admin_page_title() ) ?></h2>
<p><?php _e( 'This tool removes old slugs/permalinks from database. Select the one(s) you want to remove then press Remove button to proceed.', $this->plugin_slug ) ?></p>
<?php
if ( $count > 0 ) {
$post_id = 0;
?>
<form method="post" action="">
<p><?php printf( __( '<strong>%s</strong> old slug(s) found.', $this->plugin_slug ), $count ) ?></p>
<table class="wp-list-table widefat fixed posts">
<thead>
<tr>
<th scope="col" id="cb" class="manage-column column-cb check-column"><label class="screen-reader-text" for="cb-select-all-1"><?php _e( 'Select all', $this->plugin_slug ) ?></label><input id="cb-select-all-1" type="checkbox"></th>
<th scope="col" id="title" class="manage-column column-title"><?php _e( 'Post title', $this->plugin_slug ) ?></th>
<th scope="col" id="slug" class="manage-column column-title"><?php _e( 'Active slug', $this->plugin_slug ) ?></th>
<th scope="col" id="old-slug" class="manage-column column-title"><?php _e( 'Old slug', $this->plugin_slug ) ?></th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="col" id="cb" class="manage-column column-cb check-column"><label class="screen-reader-text" for="cb-select-all-1"><?php _e( 'Select all', $this->plugin_slug ) ?></label><input id="cb-select-all-1" type="checkbox"></th>
<th scope="col" id="title" class="manage-column column-title"><?php _e( 'Post title', $this->plugin_slug ) ?></th>
<th scope="col" id="slug" class="manage-column column-title"><?php _e( 'Active slug', $this->plugin_slug ) ?></th>
<th scope="col" id="old-slug" class="manage-column column-title"><?php _e( 'Old slug', $this->plugin_slug ) ?></th>
</tr>
</tfoot>
<tbody>
<?php
usort( $wp_old_slugs, function( $a, $b ){
return $a->post_id - $b->post_id;
});
$i = 0;
foreach ( $wp_old_slugs as $wp_old_slug ) {
$title = get_the_title( $wp_old_slug->post_id );
?>
<tr class="<?php echo $i%2 ? 'alternate' : '' ?>">
<th scope="row" class="check-column">
<label class="screen-reader-text" for="cb-select-<?php echo $wp_old_slug->meta_id ?>"><?php printf( __( 'Select %s', $this->plugin_slug ), $wp_old_slug->meta_value ) ?></label>
<input id="cb-select-<?php echo $wp_old_slug->meta_id ?>" type="checkbox" name="meta_id[]" value="<?php echo $wp_old_slug->meta_id ?>"></div>
</th>
<td class="post-title page-title column-title row-title">
<?php echo $title ? $title : __( '[untitled]', $this->plugin_slug ) ?>
</td>
<td class="post-title page-title column-title row-title">
<label for="cb-select-<?php echo $wp_old_slug->meta_id ?>"><code><?php echo $this->get_the_slug( $wp_old_slug->post_id ) ?></code></label>
</td class="post-title page-title column-title row-title">
<td>
<label for="cb-select-<?php echo $wp_old_slug->meta_id ?>"><code><?php echo $wp_old_slug->meta_value ?></code></label>
</td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
<?php wp_nonce_field( 'remove_old_slugs' ) ?>
<div class="tablenav bottom">
<input type="submit" name="remove_old_slugs" value="Remove" class="button button-primary" />
</div>
</form>
<?php
}
else {
?>
<p><?php _e( 'No old slugs found in database.', $this->plugin_slug ) ?></p>
<?php
}
}
/**
* Get the slug of a post type by id.
*
* @since 0.1.0
* @author Tom Barrett (@TCBarrett)
* @see http://www.tcbarrett.com/2013/05/wordpress-how-to-get-the-slug-of-your-post-or-page/
*/
public function get_the_slug( $id = null ){
if ( empty( $id ) ) {
global $post;
if ( empty( $post ) ) {
// No global $post var available.
return '';
}
$id = $post->ID;
}
$slug = basename( get_permalink( $id ) );
return $slug;
}
/**
* Display the post type slug.
*
* @since 0.1.0
* @author Tom Barrett (@TCBarrett)
* @see http://www.tcbarrett.com/2013/05/wordpress-how-to-get-the-slug-of-your-post-or-page/
*/
public function the_slug( $id = null ){
echo apply_filters( 'the_slug', $this->get_the_slug( $id ) );
}
}
}
$WPROSR = new WordPress_Remove_Old_Slugs_Redirects();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment