英文:
Dynamically execute several remove_action() calls based on a multidimensional array
问题
我正在尝试动态执行一系列基于具有特定结构的配置数组的remove_action()
函数调用,这是在我的WordPress应用程序中进行的操作。
硬编码的情况下,正确的执行如下:
remove_action('genesis_after_header', 'genesis_do_nav');
remove_action('genesis_after_header', 'genesis_do_subnav');
remove_action('genesis_site_title', 'genesis_seo_site_title', 5);
我希望基于以下多维数组执行相同的操作:
$config = array(
'remove_actions' => [
'genesis_after_header' => [
'genesis_do_nav',
'genesis_do_subnav'
],
'genesis_site_title' => [
'genesis_seo_site_title' => 5
],
],
);
以下是我的尝试迭代配置数组以执行这三个remove_action()
调用的代码:
foreach ($config as $hook => $functions) {
foreach ($functions as $function => $priority) {
if (! is_null($priority)) {
remove_action($hook, $function);
} else {
remove_action($hook, $function, $priority);
}
}
}
这段代码未能正确访问值,存在问题。
英文:
I'm trying dynamically execute a series of remove_action()
function calls in my WordPress application based on a config array with a specific structure.
Hard-coded, the correct execution would be:
remove_action('genesis_after_header', 'genesis_do_nav');
remove_action('genesis_after_header', 'genesis_do_subnav');
remove_action('genesis_site_title', 'genesis_seo_site_title', 5);
I want to perform the same execution based on a multidimensional array like this:
$config = array(
'remove_actions' => [
'genesis_after_header' => [
'genesis_do_nav',
'genesis_do_subnav'
],
'genesis_site_title' => [
'genesis_seo_site_title' => 5
],
],
);
Here is my attempt to iterate the config array to execute the three remove_action()
calls:
foreach ($config as $hook => $functions) {
foreach ($functions as $function => $priority) {
if (! is_null($priority)) {
remove_action($hook, $function);
} else {
remove_action($hook, $function, $priority);
}
}
}
This isn't correctly accessing the values as intended.
答案1
得分: 0
Adjust your config structure so that $function
always comes from a key and its value (which may be null
) will be the $priority
.
$config = [
'remove_actions' => [
'genesis_after_header' => [
'genesis_do_nav' => null,
'genesis_do_subnav' => null,
],
'genesis_site_title' => [
'genesis_seo_site_title' => 5
],
],
];
When accessing the data, the first loop must access $config['remove_actions']
. Demo
foreach ($config['remove_actions'] as $hook => $functions) {
foreach ($functions as $function => $priority) {
if (is_null($priority)) {
remove_action($hook, $function);
} else {
remove_action($hook, $function, $priority);
}
}
}
英文:
Adjust your config structure so that $function
always comes from a key and its value (which may be null
) will be the $priority
.
$config = [
'remove_actions' => [
'genesis_after_header' => [
'genesis_do_nav' => null,
'genesis_do_subnav' => null,
],
'genesis_site_title' => [
'genesis_seo_site_title' => 5
],
],
];
When accessing the data, the first loop must access $config['remove_actions']
. Demo
foreach ($config['remove_actions'] as $hook => $functions) {
foreach ($functions as $function => $priority) {
if (is_null($priority)) {
remove_action($hook, $function);
} else {
remove_action($hook, $function, $priority);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论