我正在构建一个wordpress插件,这是我的shortcode,我想在我的帖子中使用[phx amount="20" color="green"]来呈现一个锚点链接,点击这个链接就可以转到一个可以接收get参数并做一些事情的页面。我已经制作了shortcode,但是如何使用插件机制来制作这样的页面呢?
add_shortcode( 'phx', array( $this, 'phx_shortcode' ) );
function phx_shortcode( $attrs ) {
$html = '';
$customized_atts = shortcode_atts( array(
'amount' => '10',
'color' => 'green',
), $attrs, 'phx');
$html .= "<a href='http://wordpress.dev?".
"amount={$customized_atts['amount']}'>Pay</a>";
return $html;
}发布于 2016-03-16 22:29:38
你的代码可以很容易地嵌入到自定义插件中。您所需要做的就是put a header on top of the file,将您的自定义插件放入站点的wp-content/plugins文件夹中,并在您登录时启用它。然后该功能将可用于您的可湿性粉剂网站。
下面是一个带有子标题的示例标题,我希望这样做可以增强代码的可读性(注意:子标题不是必需的,但主标题是必需的):
/*
Plugin Name: My Custom Plugin
Plugin URI: http://www.example.com/
Description: What my plugin does
Version: 0.1.0
Author: Phoenix
Author URI: http://example.co
License: CC Attribution-ShareAlike License
License URI: https://creativecommons.org/licenses/by-sa/4.0/
*/
/*
##################################
########### Shortcodes ###########
##################################
Explain in particular what this function does here.
*/
add_shortcode( 'phx', array( $this, 'phx_shortcode' ) );
function phx_shortcode( $attrs ) {
$html = '';
$customized_atts = shortcode_atts( array(
'amount' => '10',
'color' => 'green',
), $attrs, 'phx');
$html .= "<a href='http://wordpress.dev?".
"amount={$customized_atts['amount']}'>Pay</a>";
return $html;
}发布于 2016-03-16 23:19:00
您将需要使用query_vars filter向WordPress注册查询变量。
完成后,您可以使用get_query_var()检索它们。
在您的主题(functions.php)或插件中:
function my_custom_query_vars_filter($vars) {
$vars[] = 'amount';
$vars[] .= 'color';
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars_filter' );在模板文件中或其他位置:
$color = get_query_var('color');
$amount = get_query_var('amount');https://stackoverflow.com/questions/36036477
复制相似问题