<?php
/**
* Plugin Name: RedirectDeletedTag
* Plugin URI: httphttps://wordpress.stackexchange.com/questions/136018/
*/
class RedirectDeletedTag {
/**
* Adjust this variable according to real tag slug.
* The key is the tag slug, and the value is the base url for the redirect.
* If you want, you can add other tag slugs you want ot move to separate sites.
*/
protected $target_tags = array(
'business-consultants' => 'http://www.example.com/business-consultants/',
);
function cacheUrls() {
if ( ! is_admin() || ! current_user_can( 'install_plugins' ) ) return;
$t = new ArrayIterator( $this->target_tags );
while( $t->valid() ) {
$tag = $t->key();
if ( $this->shouldCache( $tag ) ) $this->cacheTagUrls( $tag );
$t->next();
}
}
function redirect() {
if ( is_admin() ) return;
$url = $this->getUrl();
if ( ! empty( $url ) ) {
$t = new ArrayIterator( $this->target_tags );
while( $t->valid() ) {
$this->redirectTag( $t->key(), $url, $t->current() );
$t->next();
}
}
}
protected function cacheTagUrls ( $tag ) {
$args = array( 'post_type' => 'post', 'nopaging' => true, 'tag' => $tag );
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
$slugs = wp_list_pluck( $query->posts, 'post_name' );
$id_list = implode( ',', wp_list_pluck( $query->posts, 'ID') );
$cache = '<?php return array("' . implode('","', $slugs ) . '");';
if ( @file_put_contents( $this->getCachePath( $tag ), $cache ) !== FALSE ) {
set_transient( __CLASS__ . $tag, '1' );
// cache saved, delete all posts and term
global $wpdb;
$wpdb->query( "DELETE FROM $wpdb->posts WHERE ID IN ($id_list)" );
$term = get_term_by('slug', $tag, 'post_tag');
if ( ! empty( $term ) && isset( $term->term_id ) ) {
wp_delete_term( $term->term_id, 'post_tag' );
}
}
}
}
protected function redirectTag( $tag, $url, $base ) {
if ( file_exists( $this->getCachePath( $tag ) ) ) {
$cached = (array) include $this->getCachePath( $tag );
if ( in_array( $url, $cached ) ) {
wp_redirect( trailingslashit( $base ) . $url );
exit();
}
}
}
protected function shouldCache( $tag = '' ) {
$should = FALSE;
if ( is_admin() && current_user_can( 'install_plugins' ) ) {
$transient = get_transient( __CLASS__ . $tag );
$file = file_exists( $this->getCachePath( $tag ) );
$should = empty($transient) && ! $file;
}
return $should;
}
protected function getCachePath( $tag = '' ) {
$upload_dir = wp_upload_dir();
$base = trailingslashit( $upload_dir['basedir'] );
return $base . 'redirect_deleted_' . $tag . '.php';
}
protected function getUrl() {
$home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' );
$full = trim( str_replace( $home_path, '', add_query_arg( array() ) ), '/' );
$stripped = explode( '?', $full );
return trim( $stripped[0], '/' );
}
}
$rd = new RedirectDeletedTag();
add_action( 'shutdown', array( $rd, 'cacheUrls' ) );
add_action( 'after_setup_theme', array( $rd, 'redirect' ) );
* To run a one-time task I'll use a transient plus a check if current user is an administrator, plus the existence of the cache file. If you want, take a look at this question/answersquestion/answers to know different approach to one-time tasks in WordPress.