admin管理员组文章数量:1122846
In the theme functions.php, a global variable is set.
// This is in functions.php
add_action( 'init', 'me_set_globals', 1, 1 );
function me_set_globals () {
global $my_global;
if (1==1) {
$my_global = 'yes';
} else $my_global = 'no';
}
In a custom plugin, the global variable is recognized only if it's inside a function in the plugin file.
It is not recognized outside a function. I need to use the global variable outside a function to determine whether or not to add_action or add_filter.
That global variable is not being recognized - it has no value. NOTE: using $GLOBALS will not work, either.
// This is in the custom plugin file.
global $my_global;
if ($my_global == 'yes') {
add_action( 'some_actionable_thingy', 'my_custom_function', 10, 4 );
}
function my_custom_function() {
// Do something here
// NOTE: The $global var has a value here.
}
How can I set global variables one time in functions.php and use it in custom plugins outside functions. These are for the front-end pages as well as admin.
Thank you!
In the theme functions.php, a global variable is set.
// This is in functions.php
add_action( 'init', 'me_set_globals', 1, 1 );
function me_set_globals () {
global $my_global;
if (1==1) {
$my_global = 'yes';
} else $my_global = 'no';
}
In a custom plugin, the global variable is recognized only if it's inside a function in the plugin file.
It is not recognized outside a function. I need to use the global variable outside a function to determine whether or not to add_action or add_filter.
That global variable is not being recognized - it has no value. NOTE: using $GLOBALS will not work, either.
// This is in the custom plugin file.
global $my_global;
if ($my_global == 'yes') {
add_action( 'some_actionable_thingy', 'my_custom_function', 10, 4 );
}
function my_custom_function() {
// Do something here
// NOTE: The $global var has a value here.
}
How can I set global variables one time in functions.php and use it in custom plugins outside functions. These are for the front-end pages as well as admin.
Thank you!
Share Improve this question asked Apr 9, 2024 at 17:44 CatsyCatsy 113 bronze badges2 Answers
Reset to default 1There are 2 problems here
Problem 1: Scope
The missing piece of the puzzle is scope. Scope isn't a WordPress thing, it's a fundamental programming feature, to use a global variable in a scope you have to declare to PHP that it's a global variable. If you use it somewhere else without declaring that it's a global variable it's a new and separate variable that has the same name.
Problem 2: Order and Timing Of Events
When you add your action based on the value of $my_global
the function has not happened yet. The code as it is written in your question requires time travel to work as is.
Remember:
- PHP programs are loaded from a blank slate for every request, you can't assign a variable a value in one request, then read it in another. Some people think this is what
global
does but that's not true.global
does not mean across your entire server, it only extends across that request being handled for that specific person at that specific time. add_action
tells WordPress "when this action/event happens, call this function"
With these things in mind the code should instead check at the time it's needed if it should run, e.g.:
add_action( 'some_actionable_thingy', 'my_custom_function', 10, 4 );
function my_custom_function() {
global $my_global;
if ($my_global != 'yes') {
return;
}
// Do something here
}
Fundamentally though, global
variables are incredibly bad practice, make sites difficult to work with, and are unnecessary. You should avoid them as much as possible. At a minimum they should be replaced or wrapped with functions, if not superior methods.
How can I set global variables one time in functions.php and use it in custom plugins outside functions.
The code in functions.php
and in plugins are all loaded into the same place, WordPress doesn't sandbox plugin code from each other, it's loaded into a big giant melting pot along with WordPress itself.
Tom made excellent points about scope and timing. Here's couple examples more demonstrating how to avoid global variable and add/execute action conditionally.
I would also recommmend to have a look at the Action Reference, which you can find in the Common APIs Handbook. It describes nicely the different actions and in which they run during a typical front/back end request.
Example 1
Do the conditional check in the init
callback and based on the check either add the custom action or don't.
add_action( 'init', 'my_plugin_setup' );
function my_plugin_setup(): void {
if ( should_my_action_be_added() ) {
add_action( 'some_actionable_thingy', 'my_custom_function', 10, 4 );
}
}
function should_my_action_be_added(): bool {
// some logic...
return true;
}
function my_custom_function( $param1, $param2, $param3, $param4 ): void {
// do something..
}
Example 2
Add a filter controlled guard clause to your custom function to conditionally allow or disallow its execution. Apply truthy callback to the filter before the target action is supposed to run so that the custom function gets executed.
add_action( 'some_action_running_before_some_actionable_thingy_eg_init', 'my_plugin_condition_checker' );
function my_plugin_condition_checker(): void {
if ( my_logic_should_my_custom_function_run() ) {
add_filter( 'should_my_custom_function_run', '__return_true' );
}
}
function my_logic_should_my_custom_function_run(): bool {
// some logic...
return true;
}
add_action( 'some_actionable_thingy', 'my_custom_function', 10, 4 );
function my_custom_function( $param1, $param2, $param3, $param4 ): void {
if ( apply_filters( 'should_my_custom_function_run', false ) ) {
return;
}
// do something..
}
Alternatively attach the logic function to the filter.
add_filter( 'should_my_custom_function_run', 'my_logic_should_my_custom_function_run' );
The key point here is to execute the logic code before some_actionable_thingy
action is fired so that you can have a say in whether my_custom_function
should do something or not.
本文标签: Wordpress Global Var in Plugin but Outside Function
版权声明:本文标题:Wordpress Global Var in Plugin but Outside Function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310632a1934447.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论