Since WordPress 2.3.0, the redirect_guess_404_permalink() function has existed to attempt guessing the desired URL A specific web address of a website or web page on the Internet, such as a websiteโs URL www.wordpress.org based on the query variables available. This is especially useful when a postโs parent changes (for hierarchical post types), or a postโs slug is changed.
Guessing a URL to redirect 404 requests is fine for the majority of WordPress sites, but site owners, developers, and plugin A plugin is a piece of software containing a group of functions that can be added to a WordPress website. They can extend functionality or add new features to your WordPress websites. WordPress plugins are written in the PHP programming language and integrate seamlessly with WordPress. These can be free in the WordPress.org Plugin Directory https://wordpress.org/plugins/ or can be cost-based plugin from a third-party authors may want more fine grained control over the guessing logic.
This is a long-standing issue, as the original ticket Created for both bug reports and feature development on the bug tracker. was opened 9 years ago. So long, WordPress 5.5 will add the ability to manage that feature properly.
Short-circuiting default guessing logic
The new pre_redirect_guess_404_permalink filter Filters are one of the two types of Hooks https://codex.wordpress.org/Plugin_API/Hooks. They provide a way for functions to modify data of other functions. They are the counterpart to Actions. Unlike Actions, filters are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output. can now be used to short-circuit the function, bypassing the default guessing logic. This is useful to execute custom logic that better fits each individual siteโs needs to make a more accurate guesses.
Returning a non-false value to the filter will cause the function to return the filtered value early.
Example
function mysite_pre_redirect_guess_404_permalink() {
// Custom redirect URL guessing logic.
return $new_redirect_url;
}
add_filter( 'pre_redirect_guess_404_permalink', 'mysite_pre_redirect_guess_404_permalink' );
Controlling โstrictโ vs. โlooseโ comparison
The strict_redirect_guess_404_permalink can now be used to filter whether a โstrictโ or โlooseโ comparison is used to make a redirect URL guess.
โStrictโ comparisons (true) will make a guess suggestion for a redirect only when exact post_name matches are found.
โLooseโ comparisons (false) is the default option and will perform a LIKE query on post_name.
Example
The following example will enable โstrictโ comparisons in redirect_guess_404_permalink():
add_filter( 'strict_redirect_guess_404_permalink', '__return_true' );
Disable 404 redirect guessing
The do_redirect_guess_404_permalink filter can now be used to completely disable redirect guessing. Returning false to the filter will disable the feature entirely.
Example
add_filter( 'do_redirect_guess_404_permalink', '__return_false' );
For more information on these changes, consult the related Trac An open source project by Edgewall Software that serves as a bug tracker and project management tool for WordPress. ticket: #16557.
#5-5, #dev-notes, #permalinks