admin管理员组

文章数量:1336711

I am trying to add some meta-tag when shortcode [refresh url=''] tag is used.

I have done below code, but it is not working, I want to inject HTML code between "<head>" tag only when the shortcode is used.

<?php
/*
* Plugin Name: Plugin
* Description: Plugin
* Version: 1.0
* Author: yolo yolo
* Author URI: 
*/

$url = '';

function metaRefresh( $atts = array() ) {
    extract(shortcode_atts(array(
     'url' => '',
    ), $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
}


?>

I am trying to add some meta-tag when shortcode [refresh url='http://stackoverflow'] tag is used.

I have done below code, but it is not working, I want to inject HTML code between "<head>" tag only when the shortcode is used.

<?php
/*
* Plugin Name: Plugin
* Description: Plugin
* Version: 1.0
* Author: yolo yolo
* Author URI: https://example
*/

$url = '';

function metaRefresh( $atts = array() ) {
    extract(shortcode_atts(array(
     'url' => 'https://example',
    ), $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
}


?>
Share Improve this question asked May 21, 2020 at 18:37 UserHexUserHex 111 bronze badge 7
  • 2 Shortcodes are only parsed in post_content, so this approach isn't possible. – WebElaine Commented May 21, 2020 at 18:47
  • Yes, but can I trigger "wp_head" content inside shortcode function? so that when Wordpress is parsing shortcode, it sees "wp_head" action and takes the other action as well? if you can help me - what would be the best approach, that would be very helpful. – UserHex Commented May 21, 2020 at 18:54
  • 2 No, because shortcodes run after wp_head() has already finished running. It looks like you're trying to allow editors to enter a redirect; you could set up custom postmeta with a URL input, and in the theme's header.php file inside the <head> you can have a conditional - if that postmeta isn't empty, output the meta tag with the URL from the postmeta. – WebElaine Commented May 21, 2020 at 18:57
  • I am doing it vai "plugin", so I don't have access to "header.php" – UserHex Commented May 21, 2020 at 18:58
  • 1 Any method that can be done via "plugin" and still be injected in the head tag? – UserHex Commented May 21, 2020 at 19:01
 |  Show 2 more comments

1 Answer 1

Reset to default -1

First thing, wp_head action hook does not accept any argument at all, so am not sure whether the $url variable will be passed.

To run the short code properly, you have to call do_shortcode():

add_action( 'wp_head', 'refresh_page' );

function refresh_page()
{
    echo do_shortcode( "[refresh url='http://stackoverflow']" );
}


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 . '">';
}

本文标签: pluginsInject HTML meta tag inside wordpress ltheadgt tag using addshortcode