I am trying to add some meta-tag when shortcode [refresh url='http://stackoverflow.com'] 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.

* Plugin Name: Plugin
* Description: Plugin
* Version: 1.0
* Author: yolo yolo
* Author URI: https://example.com

$url = '';

function metaRefresh( $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; ?>">

  • 2
    Shortcodes are only parsed in post_content, so this approach isn't possible.
    – WebElaine
    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
    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
    May 21, 2020 at 18:57
  • I am doing it vai "plugin", so I don't have access to "header.php"
    – UserHex
    May 21, 2020 at 18:58
  • 1
    Any method that can be done via "plugin" and still be injected in the head tag?
    – UserHex
    May 21, 2020 at 19:01

1 Answer 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.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 . '">';
  • How can I add arguments inside refresh_page function so that shortcode can pass [refresh url='$url'] - I need to pass argument inside refresh_page function.
    – UserHex
    May 21, 2020 at 20:29
  • possible to pass an argument inside refresh_page function?
    – UserHex
    May 21, 2020 at 20:46
  • You cannot pass argument to the function. Reason is because the wp_head action hook doesn't accept any argument at all. The functionality you are talking about contradict it's implementation. You've got the short code already, then use it. If you need to pass more arguments, then create that support within the short code, not the callback function that is passed to the wp_head action hook. May 21, 2020 at 21:58
  • The context the function is used doesn't allow for passing arguments, but you can pass the arguments in the shorcode, it's the same effect! May 21, 2020 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.