在 WordPress 中创建短代码时如何 return 许多条件字符串
How to return many conditional strings when creating shortcode in WordPress
我是短代码的新手 PHP。我正在通过关注许多 tutorials such as this 创建简码。尝试在短代码中添加长输出条件字符串时,以下是否安全并建议执行?或者有更好的方法吗?
function some_shortcode() {
if(/*condition*/) {
$return = "Something...";
}
$return .= "...interesting";
return $return;
}
解决方案 1
检查用户 ID,以防未登录 return“未登录”
检查你自己的条件并组合成一个字符串后
add_shortcode('yourShortcodeName', 'yourShortcodeFunction');
function yourShortcodeFunction(){
$user_id = get_current_user_id();
if(!$user_id || $user_id == 0){
return "not logged in";
}
$return = "";
if ( wp_is_application_passwords_available_for_user( $user_id ) || ! wp_is_application_passwords_supported() ) {
$return.= "Whatever";
}
$return.= " another value";
return $return;
}
解决方案 2
检查用户 ID,以防未登录 return“未登录”
检查你自己的条件并组合成一个数组并内爆输出字符串
add_shortcode('yourShortcodeName', 'yourShortcodeFunction');
function yourShortcodeFunction(){
$user_id = get_current_user_id();
if(!$user_id || $user_id == 0){
return "not logged in";
}
$return = [];
if ( wp_is_application_passwords_available_for_user( $user_id ) || ! wp_is_application_passwords_supported() ) {
$return[] = "Whatever";
}
$return[] = " another value";
return implode(' ' , $return);
}
我是短代码的新手 PHP。我正在通过关注许多 tutorials such as this 创建简码。尝试在短代码中添加长输出条件字符串时,以下是否安全并建议执行?或者有更好的方法吗?
function some_shortcode() {
if(/*condition*/) {
$return = "Something...";
}
$return .= "...interesting";
return $return;
}
解决方案 1
检查用户 ID,以防未登录 return“未登录” 检查你自己的条件并组合成一个字符串后
add_shortcode('yourShortcodeName', 'yourShortcodeFunction');
function yourShortcodeFunction(){
$user_id = get_current_user_id();
if(!$user_id || $user_id == 0){
return "not logged in";
}
$return = "";
if ( wp_is_application_passwords_available_for_user( $user_id ) || ! wp_is_application_passwords_supported() ) {
$return.= "Whatever";
}
$return.= " another value";
return $return;
}
解决方案 2
检查用户 ID,以防未登录 return“未登录” 检查你自己的条件并组合成一个数组并内爆输出字符串
add_shortcode('yourShortcodeName', 'yourShortcodeFunction');
function yourShortcodeFunction(){
$user_id = get_current_user_id();
if(!$user_id || $user_id == 0){
return "not logged in";
}
$return = [];
if ( wp_is_application_passwords_available_for_user( $user_id ) || ! wp_is_application_passwords_supported() ) {
$return[] = "Whatever";
}
$return[] = " another value";
return implode(' ' , $return);
}