Last active
November 5, 2024 11:36
-
-
Save ianpegg/29d16c74fea470046c1dacb4f9dd3fb9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: eggMUP: SEO Tools | |
* Plugin URI: https://gist.github.com/ianpegg/29d16c74fea470046c1dacb4f9dd3fb9 | |
* Description: Tools for SEO tweaks. | |
* Version: 1.0.1 | |
* Author: Ian Pegg | |
* Author URI: https://eggcupwebdesign.com | |
* php version 7.4.15 | |
* | |
* @category Must_Use_Plugin | |
* @package WordPress_Plugin | |
* @author Ian Pegg <[email protected]> | |
* @license GNU/GPLv3 https://www.gnu.org/licenses/gpl-3.0.txt | |
* @link https://eggcupwebdesign.com | |
**/ | |
namespace EggCup\MUP\SEO; | |
if (!defined('ABSPATH')) { | |
exit; | |
} | |
/** | |
* Prevents WP from guessing which page to return whenever the page | |
* request results in a 404. | |
* | |
* Letting WP guess is not necessarily a bad thing, but if you want | |
* to track _all_ 404s in analytics or have other mitigations in place | |
* then you have two options: | |
* 1. add a filter to 'do_redirect_guess_404_permalink' to prevent WP | |
* guessing when it encounters a 404. | |
* 2. remove the 'template_redirect' to deactivate all redirects, including | |
* canonical redirects. This function does a lot of work to clean up the URL | |
* so this is the nuclear option and may have an impact on your SEO! | |
* | |
* @link https://make.wordpress.org/core/2020/06/26/wordpress-5-5-better-fine-grained-control-of-redirect_guess_404_permalink/ | |
* @link https://developer.wordpress.org/reference/functions/redirect_canonical/ | |
*/ | |
add_filter('do_redirect_guess_404_permalink', '__return_false'); | |
remove_action('template_redirect', 'redirect_canonical'); | |
/** | |
* Redirects unauthenticated users to the login prompt. | |
* A handy way to stop bots from scraping staging sites. | |
* Important: assumes 'development' and 'local' sites are | |
* publicly inaccessible. | |
* | |
* @return void | |
*/ | |
add_action('template_redirect', function () { | |
if ('staging' === wp_get_environment_type() AND !is_admin()) { | |
// For belt and braces add robots noindex meta tag to public pages: | |
add_action('pre_option_blog_public', '__return_zero'); | |
// Redirect the user to login prompt unless they pass a GET var: | |
if (!is_user_logged_in() AND !isset($_GET['viewfrontend'])) { | |
auth_redirect(); | |
} | |
} | |
}); | |
/** | |
* Redirect the current request to the 404 page, whilst setting | |
* headers at the same time. | |
* | |
* Important: Only call this function if you understand why | |
* you need to use it! | |
* | |
* @return void | |
*/ | |
function Do_404_redirect() | |
{ | |
global $wp_query; | |
$wp_query->set_404(); | |
status_header(404); | |
get_template_part(404); | |
exit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment