多亏了@Shazzad和@G.M.,我找到了正确的方向。
以下是我所做工作的简要概述:
JavaScript/AJAX操作
jQuery(function($) {
for (var i = 0; i < localizedData.postTypes.length; ++i) {
$.ajax({
type: \'post\',
url: \'post-new.php?post_type=\'+localizedData.postTypes[i],
data: {
action: \'my-plugin-action\',
_ajax_nonce: localizedData.nonce
},
success: function(data) {
if (data) {
doSomethingWithTheBoxes(data.metaBoxes);
}
}
});
}
});
将JavaScript文件排入“我的自定义用户选项”页面的队列
$post_types = array(
\'post\',
\'page\',
\'my_custom_post_type\',
);
if (\'users\' === basename($pagenow, \'.php\'))
add_action(\'admin_print_scripts-users_page_my-plugin-page\', \'MyPlugin_enqueue_scripts\');
function MyPlugin_enqueue_scripts() {
$data = array(
\'nonce\' => wp_create_nonce(\'my_plugin_nonce\'),
\'postTypes\' => $post_types,
);
$handle = \'my-plugin-js\';
wp_enqueue_script(
$handle,
plugin_dir_url(__FILE__).\'my-plugin.js\',
array(\'jquery\'),
filemtime(plugin_dir_path(__FILE__).\'my-plugin.js\'),
true
);
wp_localize_script($handle, \'localizedData\', $data);
} // function MyPlugin_enqueue_scripts
设置正确的钩子来处理AJAX请求
global $pagenow;
if (\'post-new\' === basename($pagenow, \'.php\'))
add_action(\'do_meta_boxes\', \'MyPlugin_get_meta_boxes\', PHP_INT_MAX);
function MyPlugin_get_meta_boxes($post_type) {
if (
\'my-plugin-action\' == filter_input(INPUT_POST, \'action\')
&& in_array($post_type, $post_types)
) {
check_ajax_referer(\'my_plugin_nonce\');
wp_send_json(array(
\'metaBoxes\' => $GLOBALS[\'wp_meta_boxes\']
));
}
} // function MyPlugin_get_meta_boxes
现在我可以得到所有
actually registered 我喜欢的任何帖子类型的元框&mdash;在其他管理页面上。