我正在尝试将一些变量推送到google analytics,并希望能够检测页面上是否包含了短代码。
有人知道怎么做吗?
非常感谢。
诺埃尔
我正在尝试将一些变量推送到google analytics,并希望能够检测页面上是否包含了短代码。
有人知道怎么做吗?
非常感谢。
诺埃尔
我有时也在想同样的事情——wp是否会在保存时检查并保存一个注册表等
我快速查看了代码,但似乎没有。
然而,全球$shortcode_tags
, wp具有此功能
function get_shortcode_regex() {
global $shortcode_tags;
$tagnames = array_keys($shortcode_tags);
$tagregexp = join( \'|\', array_map(\'preg_quote\', $tagnames) );
// WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcodes()
return \'(.?)\\[(\'.$tagregexp.\')\\b(.*?)(?:(\\/))?\\](?:(.+?)\\[\\/\\2\\])?(.?)\';
}
此处用于应用短代码:$pattern = get_shortcode_regex();
return preg_replace_callback(\'/\'.$pattern.\'/s\', \'do_shortcode_tag\', $content);
我的正则表达式没那么好,但也许你可以用它做点什么?Alternatively:
然后是暴力方式-对于每个短代码标记,“简单地”检查\'[\'.$tag
是否在内容中?但是,一些开发人员/老插件会对带有注释或其他自定义标记的内容进行自己的时髦过滤。因此,您还可以检查wp global$wp_filter
对于$wp_filter[\'the_content\']
; 内容应该是“内容”所要求的功能。
To be clever
您可以在后期保存/更新时自己添加一个操作,检查过滤器和短代码,然后将某些内容存储在后期元数据中。然后,在显示时,您所要做的就是检查post meta。呼。。。值得吗?
对于任何遇到这个问题的人,以下是正确的答案:
使用has\\u shorcode()检测是否调用了特定的短代码。可在此处查看:
https://developer.wordpress.org/reference/functions/has_shortcode/
这是我的火车失事版(就目前的情况来看,安马里提出了很多好主意)。对于其他人来说,它也不是很灵活,但我们的托管平台和人们使用它的方式非常灵活(让我的生活更轻松):
function tfh_analytics() {
$client_ua = trim(get_option( \'tf_ua_analytics\' ));
?>
<script type=\'text/javascript\'>
//<![CDATA[
var _gaq = _gaq || [];
// TF Aggregate Tracking
_gaq.push([\'_setAccount\',\'UA-xxxxxxx-x\']);
_gaq.push([\'_trackPageview\'],[\'_trackPageLoadTime\']);
// Define Theme
_gaq.push([\'_setCustomVar\',1,\'Theme\',\'<?php echo TF_THEME; ?>\']);
// Define Category
<?php
if ( have_posts() && ( is_page() || is_single() ) ) {
$content = get_the_content(the_post());
// Check if Front Page
if ( is_front_page() ) {
if ( is_home() ) {
$category = \'home-blog\';
} else {
$category = \'home-static\';
}
} else {
// Check if Events
if ( stripos( $content , \'[tf-event\') ) { $category = \'events\'; }
// Check if Food Menu
if ( stripos( $content , \'[tf-food\') ) { $category = \'food-menu\'; }
// Check if Location
if ( stripos( $content , \'[tf-googlemaps\') ) { $category = \'location\'; }
// Check if Blog
if ( is_home() ) { $category = \'blog\'; }
}
if ( !$category ) { $category = \'other\'; }
}
?>
_gaq.push([\'_setCustomVar\',2,\'Category\',\'<?php echo $category; ?>\']);
// Define Yelp Bar
_gaq.push([\'_setCustomVar\',3,\'YelpEnabled\',\'<?php if( get_option( \'tf_yelp_enabled\' ) == true) { echo \'true\'; } else { echo \'false\';} ?>\']);
// Define User Type
_gaq.push([\'_setCustomVar\',4,\'User\',\'<?php if( is_user_logged_in() ) { echo \'logged_in\'; } else { echo \'logged_out\'; } ?>\',1]);
<?php if ( $client_ua != \'\' ) { ?>
// Client Tracking
_gaq.push([\'b._setAccount\',\'<?php echo $client_ua; ?>\']);
_gaq.push([\'b._trackPageview\']);
<?php } ?>
(function() {
var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);
})();
//]]>
</script>
<?php
}
简单的方法,使用$tag
短代码函数中的变量。从中获得此想法here
add_shortcode("your_shortcode_tag", "shortcode_function");
function shortcode_function($atts, $content = null, $tag){ // $tag variable is here
// check if your shortcode is called
if($tag == "your_shortcode_tag"){
// do your stuff
}
}
有没有办法为短代码创建一个空属性?示例:function paragraph_shortcode( $atts, $content = null ) { return \'<p class=\"super-p\">\' . do_shortcode($content) . \'</p>\'; } add_shortcode(\'paragraph\', \'paragraph_shortcode\'); 用户类型某物它显