admin管理员组

文章数量:1387288

I have defined two constants in my functions.php file using the set_current_user hook.

function define_constants() {
    if ( ! defined( '_S_IS_USER_LOGGED_IN' ) ) {
        if( is_user_logged_in() ) {
            define( '_S_IS_USER_LOGGED_IN', TRUE );
        } else {
            define( '_S_IS_USER_LOGGED_IN', FALSE );
        }
    }
    if ( ! defined( '_S_IS_BUSINESS_CARD_USER_1' ) ) {
        if( _S_IS_USER_LOGGED_IN ) {
            $current_user = wp_get_current_user();
            if (in_array('business_card_user_1', $current_user->roles)) {
                define( '_S_IS_BUSINESS_CARD_USER_1', TRUE );
            } else {
                define( '_S_IS_BUSINESS_CARD_USER_1', FALSE );
            }
        } else {
            define( '_S_IS_BUSINESS_CARD_USER_1', FALSE );
        }
    }
}
add_action( 'set_current_user', 'define_constants' );

These constants value are available in the template files like single.php, page.php etc but not available when used for other functions within the same functions.php file.

function add_styles_to_admin() {
    if( _S_IS_BUSINESS_CARD_USER_1 ) {
            wp_enqueue_style( 'admin-styles', get_template_directory_uri().'/admin-styles.css', _S_VERSION );
        }
    }
add_action( 'admin_enqueue_scripts', 'add_styles_to_admin' );

This function is defined further down in the functions.php file and uses one of the constant defined above.

However, it throws the error that the constant is undefined.

PHP Fatal error: Uncaught Error: Undefined constant "_S_IS_BUSINESS_CARD_USER_1"

I checked the hooks order and see that admin_enqueue_scripts comes way after set_current_user so how is it not able to still access that constant value?

本文标签: