| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Shortcode Redirect |
| 4 |
Plugin URI: https://github.com/cartpauj/shortcode-redirect |
| 5 |
Description: Redirect visitors from any post or page to a chosen URL — via a native block or the classic [redirect] shortcode. Optional delay and optional "redirecting" message. |
| 6 |
Author: cartpauj |
| 7 |
Version: 1.1.3 |
| 8 |
Author URI: https://github.com/cartpauj |
| 9 |
License: GPLv2 or later |
| 10 |
License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 11 |
Text Domain: shortcode-redirect |
| 12 |
|
| 13 |
This program is free software; you can redistribute it and/or modify |
| 14 |
it under the terms of the GNU General Public License, version 2, as |
| 15 |
published by the Free Software Foundation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, |
| 18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
GNU General Public License for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License |
| 23 |
along with this program; if not, write to the Free Software Foundation, |
| 24 |
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 25 |
*/ |
| 26 |
|
| 27 |
if ( ! defined( 'ABSPATH' ) ) { |
| 28 |
exit; |
| 29 |
} |
| 30 |
|
| 31 |
if ( ! defined( 'SCR_VERSION' ) ) { |
| 32 |
$scr_data = get_file_data( __FILE__, array( 'Version' => 'Version' ) ); |
| 33 |
define( 'SCR_VERSION', $scr_data['Version'] ?: '0.0.0' ); |
| 34 |
unset( $scr_data ); |
| 35 |
} |
| 36 |
|
| 37 |
add_shortcode( 'redirect', 'scr_do_redirect' ); |
| 38 |
function scr_do_redirect( $atts ) { |
| 39 |
ob_start(); |
| 40 |
$myURL = ( isset( $atts['url'] ) && ! empty( $atts['url'] ) ) ? (string) $atts['url'] : ''; |
| 41 |
$mySEC = ( isset( $atts['sec'] ) && ! empty( $atts['sec'] ) && is_numeric( $atts['sec'] ) ) ? intval( $atts['sec'] ) : 0; |
| 42 |
$showMsg = true; |
| 43 |
if ( isset( $atts['show_message'] ) ) { |
| 44 |
$v = $atts['show_message']; |
| 45 |
if ( is_bool( $v ) ) { |
| 46 |
$showMsg = $v; |
| 47 |
} else { |
| 48 |
$lv = strtolower( (string) $v ); |
| 49 |
$showMsg = ! in_array( $lv, array( 'false', '0', 'no', 'off' ), true ); |
| 50 |
} |
| 51 |
} |
| 52 |
if ( ! empty( $myURL ) ) { |
| 53 |
?> |
| 54 |
<meta http-equiv="refresh" content="<?php echo absint( $mySEC ); ?>; url=<?php echo esc_url( $myURL ); ?>"> |
| 55 |
<?php if ( $showMsg ) { ?> |
| 56 |
Please wait while you are redirected...or <a href="<?php echo esc_url( $myURL ); ?>">Click Here</a> if you do not want to wait. |
| 57 |
<?php } ?> |
| 58 |
<?php |
| 59 |
} |
| 60 |
return ob_get_clean(); |
| 61 |
} |
| 62 |
|
| 63 |
add_action( 'init', 'scr_register_block' ); |
| 64 |
function scr_register_block() { |
| 65 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
register_block_type( |
| 69 |
__DIR__ . '/block', |
| 70 |
array( |
| 71 |
'render_callback' => 'scr_render_block', |
| 72 |
) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
function scr_render_block( $attributes ) { |
| 77 |
return scr_do_redirect( |
| 78 |
array( |
| 79 |
'url' => isset( $attributes['url'] ) ? $attributes['url'] : '', |
| 80 |
'sec' => isset( $attributes['sec'] ) ? $attributes['sec'] : 0, |
| 81 |
'show_message' => isset( $attributes['showMessage'] ) ? (bool) $attributes['showMessage'] : true, |
| 82 |
) |
| 83 |
); |
| 84 |
} |
| 85 |
|