动态执行多个 remove_action() 调用,基于多维数组。

huangapple go评论55阅读模式
英文:

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);
        }
    }
}

huangapple
  • 本文由 发表于 2023年4月17日 14:16:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定