Получить текущий идентификатор пользователя внутри возврата короткого кода 0 - PullRequest
0 голосов
/ 14 марта 2020

Я пытаюсь настроить шорткод, вставленный в пользовательский плагин, но я не могу получить идентификатор пользователя, он всегда возвращает мне 0.

Возможно, также хорошо понять роль с current_user_can , но любая информация всегда пуста.

Здесь код:

add_action( 'plugins_loaded', 'check_current_user' );

function check_current_user() {
// Your CODE with user data
global $current_user;
$current_user = wp_get_current_user();
return $current_user->ID;
}

function appp_hide_content_shortcode( $atts, $content = '' ) {
if( class_exists('AppPresser') && AppPresser::is_app() )
    return check_current_user();
else
    return $content;
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');

Ответы [ 2 ]

0 голосов
/ 14 марта 2020

Вам необходимо проверить пользователя после его загрузки. Я бы подключился к init. Вы не можете вызывать подключаемые функции непосредственно в плагине, не задерживая выполнение. Более простым решением вашей проблемы будет включение вашего шорткода в функции вашей темы. php, а не в плагин. Но ниже выполню.

add_action( 'init', 'check_current_user' , 999);
function check_current_user() {
    // This returns current user id
    return get_current_user_id();
}

function appp_hide_content_shortcode( $atts, $content = '' ) {
if( class_exists('AppPresser') && AppPresser::is_app() ){
    // This is only returning an integer here.
    return check_current_user(); 
    }  else  {
    return $content;
    }
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
0 голосов
/ 14 марта 2020

Пожалуйста, попробуйте это

/**
* Hide content on app.
* 
* Use this shortcode to hide content when viewed using the app.
* 
* Use:
* [appp_hide_content]This content will not appear on the app.[/appp_hide_content]
*/
function appp_hide_content_shortcode( $atts, $content = '' ) {

ob_start();

    //GET CURRENT USER ID
    global $current_user;
    $current_user = wp_get_current_user();
    echo $current_user->ID;

    if (current_user_can('free')){
        if( class_exists('AppPresser') && AppPresser::is_app() )
            echo '';
        else
            echo $content;
     }

$sc_html = ob_get_contents();
ob_end_clean();


return $sc_html;
}

add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...