使用ADD_SHORTROAD代码在WordPress<head>标记内注入HTML元标记

时间:2020-05-21 作者:UserHex

我正在尝试添加一些元标记,当短代码[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
}


?>

1 个回复
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 . \'">\';
}

相关推荐

What does this shortcode do?

function my_shortcode($atts, $content = null){ extract(shortcode_atts(array(\"type\" => \"warrning\"), $atts)); return \".$content.\"; } add_shortcode(\"warrning_dialog\", \"my_shortcode\"); 我知道它会创建短代码[warning\\u dialo