Created
March 4, 2013 19:16
-
-
Save jeremyclark13/5084645 to your computer and use it in GitHub Desktop.
Display a helpful 404 page. With links to posts/pages/CPT related to url keywords.
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: Helpful 404 Page | |
| Plugin URI: http://clark-technet.com/ | |
| Description: Displays helpful list of post/pages releated to the url on a 404 page. Simply edit 404 page and use echo helpful_404(). | |
| Author: Jeremy Clark | |
| Author URI: http://clark-technet.com | |
| Version: 1.0 | |
| Author URI: | |
| */ | |
| function helpful_404() { | |
| global $wp_query; | |
| $output = ""; | |
| //Removes extension from url | |
| $s = preg_replace( "/(.*)-(html|htm|php|asp|aspx)$/", "$1", $wp_query->query_vars['name'] ); | |
| //Queries for post by name | |
| $posts = new WP_Query( 'post_type=any&name=' . $s ); | |
| //If no posts are found use a search query | |
| if ( $posts->found_posts == 0 ) { | |
| //Removes dashes and underlines and replaces with spaces | |
| $s = str_replace( array( "-", "_" ), " ", $s ); | |
| //Searches post and page content for string | |
| $posts = new WP_Query( 'post_type=any&s=' . $s ); | |
| } | |
| if ( $posts->found_posts > 0 ) { | |
| $output = "<p>" . __('Maybe try one of the following pages?', 'theme_slug') . "</p>"; | |
| $output .= "<ul>"; | |
| while ( $posts->have_posts() ) { | |
| $posts->the_post(); | |
| $output .= '<li><a href="' . get_permalink( ) . '">' . get_the_title() . '</a></li>'; | |
| } | |
| $output .= "</ul>"; | |
| } | |
| return $output; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment