prefix . 'advred_tool_redirections';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
url_from VARCHAR(255) NOT NULL,
url_to VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// Add admin menu
add_action('admin_menu', 'advred_tool_register_menu');
function advred_tool_register_menu() {
add_menu_page(
__('Advanced Redirection Tool', 'advanced-redirection-tool'),
__('Redirection Tool', 'advanced-redirection-tool'),
'manage_options',
'advred-tool',
'advred_tool_redirection_page',
'dashicons-randomize',
20
);
}
// Admin page content
function advred_tool_redirection_page() {
global $wpdb;
$table_name = $wpdb->prefix . 'advred_tool_redirections';
// Handle Add Request
if (isset($_POST['advred_tool_add_redirection']) && check_admin_referer('advred_tool_add_redirection_nonce')) {
if (!empty($_POST['url_from']) && !empty($_POST['url_to'])) {
$url_from = trailingslashit(sanitize_text_field(wp_unslash($_POST['url_from'])));
$url_to = trailingslashit(sanitize_text_field(wp_unslash($_POST['url_to'])));
if (filter_var($url_from, FILTER_VALIDATE_URL) && filter_var($url_to, FILTER_VALIDATE_URL)) {
$wpdb->insert(
$table_name,
[
'url_from' => $url_from,
'url_to' => $url_to
],
['%s', '%s']
);
wp_cache_delete('advred_tool_redirections');
echo '
' . esc_html__('Redirection added successfully!', 'advanced-redirection-tool') . '
';
} else {
echo '' . esc_html__('Invalid URL format. Please try again.', 'advanced-redirection-tool') . '
';
}
}
}
// Handle Delete Request
if (isset($_GET['delete']) && !empty($_GET['delete']) && isset($_GET['_wpnonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'advred_tool_delete_redirection_nonce')) {
$delete_id = intval($_GET['delete']);
$wpdb->delete(
$table_name,
['id' => $delete_id],
['%d']
);
wp_cache_delete('advred_tool_redirections');
echo '' . esc_html__('Redirection deleted successfully!', 'advanced-redirection-tool') . '
';
}
// Handle Edit Request
if (isset($_POST['advred_tool_edit_redirection']) && check_admin_referer('advred_tool_edit_redirection_nonce')) {
if (!empty($_POST['id']) && !empty($_POST['url_from']) && !empty($_POST['url_to'])) {
$id = intval($_POST['id']);
$url_from = trailingslashit(sanitize_text_field(wp_unslash($_POST['url_from'])));
$url_to = trailingslashit(sanitize_text_field(wp_unslash($_POST['url_to'])));
$wpdb->update(
$table_name,
['url_from' => $url_from, 'url_to' => $url_to],
['id' => $id],
['%s', '%s'],
['%d']
);
wp_cache_delete('advred_tool_redirections');
echo '' . esc_html__('Redirection updated successfully!', 'advanced-redirection-tool') . '
';
}
}
?>
|
|
|
|
get_results("SELECT id, url_from, url_to FROM {$wpdb->prefix}advred_tool_redirections");
wp_cache_set($cache_key, $redirections);
}
if ($wpdb->last_error) {
echo '| ' . esc_html__('Database error:', 'advanced-redirection-tool') . ' ' . esc_html($wpdb->last_error) . ' |
';
} elseif (!empty($redirections)) {
foreach ($redirections as $redirect) {
?>
| id); ?> |
url_from); ?> |
url_to); ?> |
|
|
get_row(
$wpdb->prepare("SELECT * FROM {$wpdb->prefix}advred_tool_redirections WHERE id = %d", $edit_id)
);
wp_cache_set($cache_key_edit, $edit_redirection);
}
if ($edit_redirection) {
?>
prefix . 'advred_tool_redirections';
if (!isset($_SERVER['REQUEST_URI']) || empty($_SERVER['REQUEST_URI'])) {
return;
}
$current_path = trailingslashit(sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])));
$cache_key_redirect = "advred_tool_redirect_{$current_path}";
$redirect = wp_cache_get($cache_key_redirect);
if ($redirect === false) {
$redirect = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}advred_tool_redirections WHERE url_from = %s",
$current_path
)
);
wp_cache_set($cache_key_redirect, $redirect);
}
if ($redirect) {
wp_redirect($redirect->url_to, 301);
exit;
}
}
add_action('template_redirect', 'advred_tool_handle_redirection');