PMC Smart 404
Plugin generates a list of Suggested Links based on the current post slug to inject into the 404 templates.
Issue
It’s been brought to our attention that taking down content in bulk involves 301 redirecting article URLs to the nearest category or subcategory they were assigned to. This is not the best approach because over time, this can exacerbate redirect loops or lead Google to flag these pages as SOFT 404s instead of legitimate 301 redirects.
In general, 301s are best used when the content on page A and page B is identical. For example: /movie-news/there-will-be-blood-best-movie and /movie-news/there-will-be-blood-best-movie/ (the only difference is the trailing slash at the end of the URL).
It’s less ideal when we have /movie-news/there-will-be-blood-best-movie/ 301 redirecting to /c/movie-news/.
Solution
We need to convert 404 page into Smart template which can detect keywords in the URL slug and provide alternative pages for users to visit when the original page they were looking for 404s.
Benefits
- A better user experience
- Making our error pages work for us by recirculating traffic on the site
Implementation
- We are disabling WordPress default redirect guesses, so it won't redirect the user to the nearest category/subcategory on the post taken down and return a 404 page instead.
- We are grabbing the
slugfrom the 404 URL, removing common words (i.e. a, an, the, this, that, etc.), and perform a default search query with the remaining words. - We are creating links out of resultant posts out of search query and inject those links into the 404 page as suggested links.
Please note if ElasticSearch is available for the environment, search query will automatically be offloaded to ElasticSearch otherwise it will perform standard WP Search.
Usage
- This plugin is added to the list of must-have plugins via pmc-global-functions (opens in a new tab) to automatically be loaded on all themes so there is no need to load the plugin separately at theme level (via
plugin-loader.php). - Go to theme's 404 template and add below code to render 404 suggestions:
<?php if ( class_exists( '\PMC\Smart_404\Search', false ) ) { \PMC\Smart_404\Search::get_instance()->render_404_suggestions(); } ?>
Templates
./templates/404-related-links.php- Template to render suggested links.
- It receives data from
Search::get_instance()->render_404_suggestions(); - To provide your own template, you can use pmc_smart_404_template and filter template path.
Filters
pmc_common_words_list
apply_filters( 'pmc_common_words_list', $common_word_list )Filters common words to remove from URL slug.
pmc_smart_404_template
apply_filters( 'pmc_smart_404_template', $template );Filters Smart 404 template to use.
pmc_smart_404_heading
apply_filters( 'pmc_smart_404_heading', $heading ); Filters suggestion section heading.
pmc_smart_404_bottomline
apply_filters( 'pmc_smart_404_bottomline', $bottom_line ); Filters suggestion section bottom line.
pmc_smart_404_filter_search_query_args
apply_filters( 'pmc_smart_404_filter_search_query_args', $bottom_line ); Filters the search query arguments.