Skip to content

Instantly share code, notes, and snippets.

@TimBHowe
Last active April 28, 2022 21:24
Show Gist options
  • Save TimBHowe/6674467 to your computer and use it in GitHub Desktop.
Save TimBHowe/6674467 to your computer and use it in GitHub Desktop.
WordPress Redirect any pages that are in draft/trash for non login users to the home page
<?php
//redirect any draft/trashed posts
add_action('wp', 'trash_redirect');
function trash_redirect(){
if ( !current_user_can( 'edit_pages' ) ) {
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' || $post_status == 'draft'){
wp_redirect(home_url(), 302);
die();
}
}
}
}
@contemplate
Copy link

@TimBHowe thanks so much for this!
I made a version that allows for redirecting to a custom url:
https://gist.github.com/contemplate/11737073fdc24ac79216ff86a05f9fe4

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