我们正在使用一个第三方插件,它有一个短码(如SHORTCODEHERE),我需要动态地修改传递给它的属性,但是当插件调用短码时,他们使用shortcode_atts而不传入短码参数,所以过滤器shortcode_atts_SHORTCODEHERE永远不会被调用。
有没有其他动态修改快捷码属性的方法?
发布于 2021-01-01 05:29:36
可以,您可以过滤和修改任何第三方插件的快捷码属性,并根据更新的快捷码更改快捷码输出
案例研究
例如,我们有一个第三方插件的短码usermeta,它需要一个属性user_id,我们想要修改user_id属性值。
以下解决方案已经过测试,并且100%正常工作。
// hook the custom callback function on the do_shortcode_tag action hook
add_action( 'do_shortcode_tag', 'update_shortcode_attr_and_output', 1, 4 );
//callback function to filter and modify the usermeta shortcode attr & output
function update_shortcode_attr_and_output( $output, $tag, $attr, $m ) {
//Filter the shortcode tag
if ( 'usermeta' == $tag && ! isset( $attr['user_id'] ) ) {
// Update ShortCode Attribute user_id value
$attr['user_id'] = $this->user_id;
/**
* update outup according updated attr
* Refrence: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/shortcodes.php?rev=38713#L342
*/
global $shortcode_tags;
$content = isset( $m[5] ) ? $m[5] : null;
$output = $m[1] . call_user_func( $shortcode_tags[ $tag ], $attr, $content, $tag ) . $m[6];
}
return $output;
}https://stackoverflow.com/questions/65122554
复制相似问题