【问题标题】:How can I create a shortcode based on the current Wordpress user?如何根据当前 Wordpress 用户创建简码?
【发布时间】:2023-01-11 14:35:16
【问题描述】:
我想根据当前用户是谁创建一个带有条件的简码
我有我的简码的本质......
function my_shortcode_function() {
return 'here is the content of my shortcode!';
}
add_shortcode('coolshortcodename', 'my_shortcode_function');
但是我需要给它添加条件...我想说如果用户 ID 是 Jonny 则显示这个,如果用户是 Sally 则显示那个,如果用户是 Jamie 则显示其他...
function my_shortcode_function() {
IF JONNY
return reblex_display_block(610);
ELSE IF SALLY
return reblex_display_block(199);
ELSE IF JAMIE
return reblex_display_block(554);
}
add_shortcode('coolshortcodename', 'my_shortcode_function');
【问题讨论】:
标签:
php
wordpress
wordpress-gutenberg
wordpress-shortcode
【解决方案1】:
基本上你必须通过get_current_user_id()检查当前用户ID
这是我修改过的代码。我想这对你有用。
function my_shortcode_function() {
ob_start();
$jonny_id = 1; // You have to collect the specific user id from wp dashboard
$sally_id = 2; // You have to collect the specific user id from wp dashboard
$jamie_id = 3; // You have to collect the specific user id from wp dashboard
if(is_user_logged_in()){
if( $jonny_id == get_current_user_id() ){
return reblex_display_block(610);
}
elseif( $sally_id == get_current_user_id() ){
return reblex_display_block(199);
}
elseif( $jamie_id == get_current_user_id() ){
return reblex_display_block(554);
}
}
return ob_get_clean();
}
add_shortcode('coolshortcodename', 'my_shortcode_function');
【解决方案2】:
function my_shortcode_function() {
$current_user_id = get_current_user_id();
if ($current_user_id == 5) // where user ID of Jonny is 5
return reblex_display_block(610);
else if ($current_user_id == 6) // where user ID of Sally is 6
return reblex_display_block(199);
else if ($current_user_id == 7) // where user ID of Jamie is 7
return reblex_display_block(554);
}