我正在尝试添加一些元标记,当短代码[refresh url=\'http://stackoverflow.com\']
使用标记。
我已经完成了以下代码,但它不起作用,我想在“<head>
“仅当使用快捷码时才标记。
<?php
/*
* Plugin Name: Plugin
* Description: Plugin
* Version: 1.0
* Author: yolo yolo
* Author URI: https://example.com
*/
$url = \'\';
function metaRefresh( $atts = array() ) {
extract(shortcode_atts(array(
\'url\' => \'https://example.com\',
), $atts));
return true;
}
add_shortcode(\'refresh\', \'metaRefresh\');
add_action(\'wp_head\', \'injectHead\', $url);
function injectHead($url){
?>
<meta http-equiv="refresh" content="<?php echo $url; ?>">
<?php
}
?>
SO网友:John Zenith
首先,wp_head
操作挂钩根本不接受任何参数,因此不确定$url
将传递变量。
要正确运行短代码,必须调用do\\u shortcode():
add_action( \'wp_head\', \'refresh_page\' );
function refresh_page()
{
echo do_shortcode( "[refresh url=\'http://stackoverflow.com\']" );
}
add_shortcode( \'refresh\', \'refresh_shortcode\' );
function refresh_shortcode( $atts ) {
$atts = shortcode_atts( array(
\'url\' => \'\',
), $atts, \'refresh\' );
$url = esc_url_raw( $atts[\'url\'] );
return \'<meta http-equiv="refresh" content="\' . $url . \'">\';
}