这是我的小部件中的按钮短代码。
[button title="EDIT" href="<?php ech edit_product_url( $post->ID ); ?>"]
您可以猜到href url不起作用。在短代码中添加php的最佳方法是什么?
谢谢
这是我的小部件中的按钮短代码。
[button title="EDIT" href="<?php ech edit_product_url( $post->ID ); ?>"]
您可以猜到href url不起作用。在短代码中添加php的最佳方法是什么?
谢谢
老实说,如果您认为自己需要将可执行的PHP代码添加到短代码属性中,或者在内容编辑器中,那么您就做错了。你应该再想想做什么,为什么,怎么做。接受来自用户输入的可执行PHP代码存在很高的安全风险。
相反,您应该在短代码属性中传递post ID,并在解析短代码时获取永久链接。
例如,解析短代码的PHP函数可以是这样的:
add_shortcode( \'button\' , \'render_button\' );
function render_button( $atts, $content = null ) {
$atts = shortcode_atts( array(
\'postID\' => 0,
\'href\' => \'\',
\'title\' => \'\'
), $atts );
//You may need sanitize title, not sure what your needs are
//change this if you need
$atts[\'title\'] = sanitize_text_field( $atts[\'title\'] );
if( (int) $atts[\'postID\'] > 0 ) {
//If postID is set and greater thant zero, get post permalink for that ID
//and override href
$atts[\'href\'] = edit_product_url( $atts[\'postID\'] );
//For standard posts uncomment next linke
//$atts[\'href\'] = get_permalink( $atts[\'postID\'] );
}
//Example output
$output = \'<a class="button" href="\' . esc_url( $atts[\'href\'] ) . \'">\' . $atts[\'title\'] . \'</a>\';
return $output;
}
现在,您无需在短代码属性中包含可执行PHP即可获得该功能。此外,您还可以使用render_button
如果需要,可以直接使用。您可以在内容编辑器中或任何解析短代码的地方使用短代码,例如:
[button title="EDIT" postID="1542"]
也可以通过直接调用函数来渲染按钮: $button = render_button( array( \'postID\' => 4578 ) );
echo $button;
如果你需要一个href
, 您可以使用此短代码: [button title="EDIT" href="http://www.example.com"]
或使用直接函数调用: $button = render_button( array( \'href\' => \'http://www.example.com\' ) );
echo $button;
//Or shorter
echo render_button( array( \'href\' => \'http://www.example.com\' ) );
我会将此更改为以下内容
do_shortcode(\'[button title="EDIT" href="\' . edit_product_url( $post->ID ) . \'"]\');
我想在中设置条件header.php 包装所有模板,除了front-page.php 和single-device.php, 在引导容器元素中。写一个条件实现了我想要的if( !is_singular(\'device\') ) { echo \'<div class=\"container\">\'; } 但是,当包含两个条件时,只有第一个条件适用。if( !is_singular(\'device\') || !is_front_page() ) {