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?
本文标签:
版权声明:本文标题:wordpress - Constants defined in functions.php are not available to other functions in the same file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744517966a2610277.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论