Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. brokenfunction revised this gist Apr 2, 2018. 1 changed file with 3 additions and 9 deletions.
    12 changes: 3 additions & 9 deletions Trash Redirect WordPress Plugin
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,10 @@
    <?php
    /*
    Plugin Name: Trash Redirect
    Description: 301 Redirect trashed posts to the home page instead of a 404 page
    */

    add_action('template_redirect', 'trash_redirect');
    function trash_redirect(){
    if (is_404()){
    global $wp_query, $wpdb;
    $page_id = $wpdb->get_var( $wp_query->request );
    $post_status = get_post_status( $page_id );
    if($post_status == 'trash'){
    $vacancyName = $wp_query->query['name'];
    $deletedVacancies = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'cpt_vacancies' AND post_name like '%$vacancyName%' AND post_status = 'trash' LIMIT 1");
    if(count($deletedVacancies) > 0) {
    wp_redirect(home_url(), 301);
    die();
    }
  2. @jonbish jonbish created this gist Oct 22, 2012.
    18 changes: 18 additions & 0 deletions Trash Redirect WordPress Plugin
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <?php
    /*
    Plugin Name: Trash Redirect
    Description: 301 Redirect trashed posts to the home page instead of a 404 page
    */

    add_action('template_redirect', 'trash_redirect');
    function trash_redirect(){
    if (is_404()){
    global $wp_query, $wpdb;
    $page_id = $wpdb->get_var( $wp_query->request );
    $post_status = get_post_status( $page_id );
    if($post_status == 'trash'){
    wp_redirect(home_url(), 301);
    die();
    }
    }
    }