| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Simple Post Redirect |
| 4 |
Plugin URI: http://wordpress.org/plugins/simple-post-redirect/ |
| 5 |
Description: This plugin allows you to make simple redirects of single pages of any custom post type to any url. |
| 6 |
Author: Mohit Agarwal |
| 7 |
Author URI: https://simpleproplugins.com/product/simple-page-redirect/ |
| 8 |
Version: 1.8 |
| 9 |
Text Domain: simple-post-redirect |
| 10 |
Stable tag: "trunk" |
| 11 |
License: GPLv2 or later |
| 12 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html |
| 13 |
|
| 14 |
|
| 15 |
Simple Post Redirect is free software: you can redistribute it and/or modify |
| 16 |
it under the terms of the GNU General Public License as published by |
| 17 |
the Free Software Foundation, either version 2 of the License, or |
| 18 |
any later version. |
| 19 |
|
| 20 |
Simple Post Redirect is distributed in the hope that it will be useful, |
| 21 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
GNU General Public License for more details. |
| 24 |
|
| 25 |
You should have received a copy of the GNU General Public License |
| 26 |
along with Simple Post Redirect. If not, see http://www.gnu.org/licenses/gpl-2.0.html |
| 27 |
*/ |
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* @package Simple Post Redirect |
| 32 |
* @version 1.8 |
| 33 |
*/ |
| 34 |
|
| 35 |
function me_spr_redirect_add_meta_boxes() |
| 36 |
{ |
| 37 |
foreach (array_keys($GLOBALS['wp_post_types']) as $post_type) |
| 38 |
{ |
| 39 |
// Skip: |
| 40 |
if (in_array($post_type, array( |
| 41 |
'attachment', |
| 42 |
'revision', |
| 43 |
'nav_menu_item' |
| 44 |
))) continue; |
| 45 |
|
| 46 |
$args = array(); |
| 47 |
|
| 48 |
add_meta_box('me_spr_redirect_url', __('Simple Redirect to: ', 'simple-post-redirect') , // Title |
| 49 |
'me_spr_redirect_callback_function', // Callback function that renders the content of the meta box |
| 50 |
$post_type, // Admin page (or post type) to show the meta box on |
| 51 |
'side', // Context where the box is shown on the page |
| 52 |
'high', // Priority within that context |
| 53 |
$args |
| 54 |
// Arguments to pass the callback function, if any |
| 55 |
); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
add_action('add_meta_boxes', 'me_spr_redirect_add_meta_boxes'); |
| 60 |
|
| 61 |
function me_spr_redirect_callback_function($args) |
| 62 |
{ |
| 63 |
$saved_url = get_post_meta(get_the_ID(), 'me_spr_post_redirect', true); |
| 64 |
|
| 65 |
echo '<input style="width:100%;margin-top:5px;" placeholder="' . |
| 66 |
__('Type a URL here.', 'simple-post-redirect') . |
| 67 |
'" type="text" name="me_spr_post_redirect" value="' . esc_attr($saved_url) . '">'; |
| 68 |
|
| 69 |
echo '<p style="font-size:12px;color:#666;margin-top:6px;"> |
| 70 |
Need bulk redirects, labels, or expiration dates? |
| 71 |
<a href="https://simpleproplugins.com/product/simple-page-redirect/" target="_blank"> |
| 72 |
Upgrade to Pro |
| 73 |
</a> |
| 74 |
</p>'; |
| 75 |
|
| 76 |
wp_nonce_field('me_spr_redirect_nonce_action', 'me_spr_redirect_nonce'); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
function me_spr_redirect_load_plugin_textdomain() |
| 81 |
{ |
| 82 |
load_plugin_textdomain('simple-post-redirect', false, basename(dirname(__FILE__)) . '/languages/'); |
| 83 |
} |
| 84 |
add_action('plugins_loaded', 'me_spr_redirect_load_plugin_textdomain'); |
| 85 |
|
| 86 |
function me_spr_redirect_metabox_save($post_id) |
| 87 |
{ |
| 88 |
|
| 89 |
if (!isset($_POST['me_spr_redirect_nonce']) || !wp_verify_nonce($_POST['me_spr_redirect_nonce'], 'me_spr_redirect_nonce_action')) |
| 90 |
{ |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
if (!current_user_can('edit_post', $post_id)) |
| 95 |
{ |
| 96 |
// print 'Sorry, you do not have sufficient permission to edit this page.'; |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
if (isset($_POST['me_spr_post_redirect'])) |
| 101 |
{ |
| 102 |
update_post_meta($post_id, 'me_spr_post_redirect', esc_url_raw($_POST['me_spr_post_redirect'])); |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
delete_post_meta($post_id, 'me_spr_post_redirect'); |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
add_action('save_post', 'me_spr_redirect_metabox_save'); |
| 111 |
|
| 112 |
function me_spr_permalink_redirect() { |
| 113 |
if (!is_singular()) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
$post_id = get_queried_object_id(); |
| 118 |
if (!$post_id) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
$url = get_post_meta($post_id, 'me_spr_post_redirect', true); |
| 123 |
if (!empty($url)) { |
| 124 |
wp_redirect($url, 301); |
| 125 |
exit; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
add_action('template_redirect', 'me_spr_permalink_redirect'); |
| 130 |
|
| 131 |
function me_spr_enqueue_block_assets() { |
| 132 |
wp_enqueue_style('me-spr-block-styles', plugin_dir_url(__FILE__) . 'css/block-styles.min.css'); |
| 133 |
} |
| 134 |
add_action('enqueue_block_assets', 'me_spr_enqueue_block_assets'); |
| 135 |
|
| 136 |
// Add "Premium Support" link to the plugin row on the plugins page |
| 137 |
function me_spr_add_premium_support_link($links, $file) { |
| 138 |
// Check if this is our plugin |
| 139 |
if (strpos($file, 'simple-post-redirect.php') !== false) { |
| 140 |
// Add the "Premium Support" link |
| 141 |
$links[] = '<a style="color:#d63638;font-weight:600;" href="https://simpleproplugins.com/product/simple-page-redirect/" target="_blank">Upgrade to Pro</a>'; |
| 142 |
|
| 143 |
} |
| 144 |
return $links; |
| 145 |
} |
| 146 |
|
| 147 |
add_filter('plugin_row_meta', 'me_spr_add_premium_support_link', 10, 2); |
| 148 |
|