The do_redirect_guess_404_permalink WordPress PHP filter allows you to control whether WordPress should try to guess a redirect URL for a 404 request or not.
Usage
add_filter('do_redirect_guess_404_permalink', 'your_custom_function_name'); function your_custom_function_name($do_redirect_guess) { // Your custom code here return $do_redirect_guess; }
Parameters
$do_redirect_guess
(bool): Whether to attempt to guess a redirect URL for a 404 request. Default istrue
.
More information
See WordPress Developer Resources: do_redirect_guess_404_permalink
Examples
Disable URL guessing for 404 requests
Prevent WordPress from trying to guess a redirect URL for a 404 request.
add_filter('do_redirect_guess_404_permalink', '__return_false');
Enable URL guessing only for certain user roles
Allow URL guessing for 404 requests only for users with the “administrator” role.
add_filter('do_redirect_guess_404_permalink', 'enable_url_guessing_for_admins'); function enable_url_guessing_for_admins($do_redirect_guess) { if (current_user_can('administrator')) { return true; } return false; }
Disable URL guessing for specific post types
Prevent URL guessing for 404 requests related to the “custom_post_type” post type.
add_filter('do_redirect_guess_404_permalink', 'disable_url_guessing_for_cpt'); function disable_url_guessing_for_cpt($do_redirect_guess) { if (is_singular('custom_post_type')) { return false; } return $do_redirect_guess; }
Enable URL guessing only for logged-in users
Allow URL guessing for 404 requests only for logged-in users.
add_filter('do_redirect_guess_404_permalink', 'enable_url_guessing_for_logged_in_users'); function enable_url_guessing_for_logged_in_users($do_redirect_guess) { if (is_user_logged_in()) { return true; } return false; }
Disable URL guessing on specific pages
Prevent URL guessing for 404 requests on a specific page with the ID “123”.
add_filter('do_redirect_guess_404_permalink', 'disable_url_guessing_on_specific_page'); function disable_url_guessing_on_specific_page($do_redirect_guess) { if (is_page(123)) { return false; } return $do_redirect_guess; }