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
|
Show 2 more comments
1 Answer
Reset to default -1First 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
版权声明:本文标题:plugins - Inject HTML meta tag inside wordpress <head> tag using add_shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742412087a2469991.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
post_content
, so this approach isn't possible. – WebElaine Commented May 21, 2020 at 18:47wp_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'sheader.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