Skip to content

Commit

Permalink
v2 with selectable redirect type
Browse files Browse the repository at this point in the history
  • Loading branch information
infinitnet committed Mar 12, 2024
1 parent 3c91087 commit 8645651
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 64 deletions.
84 changes: 84 additions & 0 deletions wordpress-404-redirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Plugin Name: WordPress 404 Redirect
* Description: WordPress plugin to redirect 404 URLs to a specified URL.
* Author: Infinitnet
* Author URI: https://infinitnet.io/
* Plugin URI: https://github.com/infinitnet/wordpress-404-redirect
* Update URI: https://github.com/infinitnet/wordpress-404-redirect
* Version: 2.0.0
* License: GPLv3
* Text Domain: wordpress-404-redirect
*/

function wp_404_redirect_menu() {
add_options_page('404 Redirect Settings', '404 Redirect', 'manage_options', 'wp-404-redirect', 'wp_404_redirect_page');
}

function wp_404_redirect_page() {
if (isset($_POST['wp_404_redirect_nonce']) && wp_verify_nonce($_POST['wp_404_redirect_nonce'], 'wp_404_redirect_nonce_action')) {
update_option('wp_404_redirect_enabled', isset($_POST['wp_404_redirect_enabled']) ? '1' : '0');
update_option('wp_404_redirect_target', esc_url_raw($_POST['wp_404_redirect_target']));
update_option('wp_404_redirect_type', sanitize_text_field($_POST['wp_404_redirect_type']));
}
$redirect_types = array(
301 => '301 (Moved Permanently)',
308 => '308 (Permanent Redirect)',
302 => '302 (Found)',
303 => '303 (See Other)',
307 => '307 (Temporary Redirect)'
);
?>
<div class="wrap">
<h2>404 Redirect Settings</h2>
<form method="post">
<?php wp_nonce_field('wp_404_redirect_nonce_action', 'wp_404_redirect_nonce'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Redirect Target URL (Custom 404 Page)</th>
<td>
<input type="url" name="wp_404_redirect_target" value="<?php echo esc_url(get_option('wp_404_redirect_target')); ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row">Redirect Type</th>
<td>
<select name="wp_404_redirect_type">
<?php
$selected_type = get_option('wp_404_redirect_type', 301);
foreach ($redirect_types as $type => $label) {
$selected = ($type == $selected_type) ? 'selected' : '';
echo '<option value="' . $type . '" ' . $selected . '>' . $label . '</option>';
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">Enable Redirect</th>
<td>
<input type="checkbox" name="wp_404_redirect_enabled" value="1" <?php checked(get_option('wp_404_redirect_enabled'), '1'); ?> />
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}

function wp_404_redirect_redirect() {
if (get_option('wp_404_redirect_enabled') == '1') {
if (is_404()) {
$redirect_url = get_option('wp_404_redirect_target');
if (!empty($redirect_url)) {
$redirect_type = get_option('wp_404_redirect_type', 301);
wp_safe_redirect($redirect_url, $redirect_type);
exit();
}
}
}
}

add_action('admin_menu', 'wp_404_redirect_menu');
add_action('template_redirect', 'wp_404_redirect_redirect');
64 changes: 0 additions & 64 deletions wordpress-404-to-302-redirect.php

This file was deleted.

0 comments on commit 8645651

Please sign in to comment.