Last active
December 21, 2015 15:09
-
-
Save joshuafredrickson/6324758 to your computer and use it in GitHub Desktop.
WordPress Plugin: Custom 404 -- Redirect to any page and add a message
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: Custom 404 | |
Plugin URI: http://orangepineapple.com | |
Description: Send 404 page to another page or URL. | |
Version: 2013.08.23 | |
Author: Orangepineapple | |
Author URI: http://orangepineapple.com | |
License: GNU GPL | |
*/ | |
/* | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 2 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
*/ | |
add_action('admin_init', 'op404_register_settings' ); | |
add_action('admin_menu', 'op404_admin_menu'); | |
add_action('template_redirect', 'op404_output_header'); | |
add_action('wp_footer', 'op404_notice'); | |
register_activation_hook(__FILE__, 'op404_activate'); | |
register_deactivation_hook(__FILE__, 'op404_deactivate'); | |
register_uninstall_hook(__FILE__, 'op404_deactivate'); | |
function op404_activate() { | |
add_option('op404_type', '301'); | |
add_option('op404_target', home_url()); | |
add_option('op404_notice', 'The page you were looking for is no longer available.'); | |
} | |
function op404_deactivate() { | |
delete_option('op404_type'); | |
delete_option('op404_target'); | |
delete_option('op404_notice'); | |
} | |
function op404_register_settings() { | |
register_setting( 'op404_option-group', 'op404_type' ); | |
register_setting( 'op404_option-group', 'op404_target', 'op404_check_values'); | |
register_setting( 'op404_option-group', 'op404_notice' ); | |
} | |
function op404_admin_menu() { | |
add_options_page('Custom 404 Settings', "Custom 404", 'manage_options', basename(__FILE__), 'op404_plugin_options'); | |
} | |
function op404_plugin_options(){ | |
if (!current_user_can('manage_options')) { | |
wp_die( 'You do not have sufficient permissions to access this page.' ); | |
} | |
if (get_option("op404_target") == "") { | |
add_option('op404_target', home_url()); | |
} | |
?> | |
<div class="wrap"> | |
<h2>Custom 404 Redirect</h2> | |
<form method="post" action="options.php" id="op404_form"> | |
<?php | |
wp_nonce_field('update-options', '_wpnonce'); | |
settings_fields( 'op404_option-group'); | |
?> | |
<input type="hidden" name="page_options" value="op404_type, op404_target" /> | |
<p> | |
<select name="op404_type"> | |
<option value="301" <?= op404_checked("op404_type", "301") ?>>Enabled</option> | |
<option value="off" <?= op404_checked("op404_type", "off") ?>>Disabled</option> | |
</select> | |
<br/><br/> | |
</p> | |
<p> | |
Target Url:<br/> | |
<em>Use the entire URL, starting with <strong>http://</strong> .</em> | |
</p> | |
<p> | |
<input type="text" name="op404_target" value="<?= get_option("op404_target") ?>" size="100"> | |
<br/><br/> | |
</p> | |
<p> | |
Notice to display on redirected page:<br/> | |
<em>Leave empty to disable the notice.</em> | |
</p> | |
<p><textarea name="op404_notice" rows="3" cols="30" style="width: 500px;"><?= get_option("op404_notice") ?></textarea></p> | |
<input type="hidden" name="action" value="update" /> | |
<p class="submit"><input type="submit" name="submit" value="Save Changes" /></p> | |
</form> | |
</div> | |
<?php | |
} | |
// Test URL | |
function op404_check_values($input) { | |
if (!op404_is_valid_url($input)) { | |
add_settings_error('op404_option-group', 'settings_updated', 'This is not a valid Url', $type = 'error'); | |
} | |
return $input; | |
} | |
function op404_checked($checkOption, $checkValue) { | |
return get_option($checkOption)==$checkValue ? " selected" : ""; | |
} | |
// Collect 404 errors and redirect them to URL | |
function op404_output_header() { | |
if ( !is_404() || get_option("op404_type")=="off" ) { | |
return; | |
} | |
if (get_option('op404_target')=="") { | |
$target = home_url(); | |
} else { | |
$target = get_option("op404_target"); | |
$delim = '?'; | |
if (stristr($target,'?')) { | |
$delim = '&'; | |
} | |
$target .= $delim.'404'; | |
} | |
global $wp_query; | |
$wp_query->is_404 = true; | |
wp_redirect( $target, get_option("op404_type") ); | |
exit; | |
} | |
// Display notice | |
function op404_notice() { | |
if (!isset($_GET['404'])) { | |
return; | |
} | |
$notice = get_option("op404_notice"); | |
if ($notice != '') { | |
remove_action( 'init', '_wp_admin_bar_init' ); | |
global $wp_admin_bar; | |
if (isset($wp_admin_bar)) { | |
$top = '28'; | |
} else { | |
$top = 0; | |
} | |
?> | |
<style> | |
#op404notice { position: absolute; top: <?= $top ?>px; margin: 0 auto; z-index: 9999; text-align: center; width: 100%; } | |
.admin-bar #op404notice { top: 28px; } | |
#op404notice span { display: block; width: 600px; margin: 0 auto 15px; padding: 7px; border: 1px solid #d8d8d8; border-top: none; background: #f8f8f8; background: rgba(248,248,248,0.8); border-radius: 0 0 3px 3px; box-shadow: 0 1px 4px -1px rgba(0, 0, 0, .3); } | |
</style> | |
<div id="op404notice"> | |
<span><?= $notice; ?></span> | |
</div> | |
<?php | |
} | |
} | |
// http://www.roscripts.com/snippets/show/156 | |
function op404_is_valid_url($url) { | |
$url = @parse_url($url); | |
if ( ! $url) { | |
return false; | |
} | |
$url = array_map('trim', $url); | |
$url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port']; | |
$path = (isset($url['path'])) ? $url['path'] : ''; | |
if ($path == '') { | |
$path = '/'; | |
} | |
$path .= ( isset ( $url['query'] ) ) ? "?$url[query]" : ''; | |
if ( isset ( $url['host'] ) AND $url['host'] != gethostbyname ( $url['host'] ) ) { | |
if ( PHP_VERSION >= 5 ) { | |
$headers = get_headers("$url[scheme]://$url[host]:$url[port]$path"); | |
} else { | |
$fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30); | |
if ( !$fp ) { | |
return false; | |
} | |
fputs($fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n"); | |
$headers = fread ( $fp, 128 ); | |
fclose ( $fp ); | |
} | |
$headers = ( is_array ( $headers ) ) ? implode ( "\n", $headers ) : $headers; | |
return ( bool ) preg_match ( '#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers ); | |
} | |
return false; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment