简体   繁体   English

函数名称中的php变量-wordpress shortcode

[英]php variable in function name - wordpress shortcode

I would like to use a php variable to input the value as the shortcode name for wordpress. 我想使用php变量输入值作为wordpress的简码名称。

Basically I have an option where people can create an options and give it a shortcode name. 基本上,我有一个选项,人们可以在其中创建一个选项并为其指定一个简称。

Something like the following: 类似于以下内容:

Is this possible? 这可能吗?

$shortcode_name = "name_here";
function $shortcode_name ($atts) {
// code here
}
add_shortcode ($shortcode_name , $shortcode_name);

Thanks 谢谢

In PHP 5.3+ you have closures. 在PHP 5.3+中,您有闭包。

$shortcode_name = 'awesome';
${"{$shortcode_name}_fn"} = function($atts) {
  print('Awesome shortcode');
};
add_shortcode($shortcode_name, ${"{$shortcode_name}_fn"});

You can also have variable variable names, in this case, the variable that contains the function is named: awesome_fn. 您也可以使用变量变量名,在这种情况下,包含该函数的变量名为:awesome_fn。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM