如果你看看'Add New Site'页面的source code,你可以看到WordPress没有为此提供钩子。当然,你可以自己添加一个钩子,但通常是不好的做法来编辑WordPress核心。
但是,一旦提交并且所有信息都存在,页面将调用功能wpmu_create_blog()。在这个函数有一个名为钩,即动作wpmu_new_blog
:
do_action('wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta);
此时已创建了博客。但是,我们仍然可以通过挂接到此操作并将其保存到数据库中来检测是否提交了字段。我们添加下面进入我们的插件文件(或模板):
function add_new_blog_field($blog_id, $user_id, $domain, $path, $site_id, $meta) {
// Make sure the user can perform this action and the request came from the correct page.
switch_to_blog($blog_id);
// Use a default value here if the field was not submitted.
$new_field_value = 'default';
if (!empty($_POST['blog']['new_field']))
$new_field_value = $_POST['blog']['new_field'];
// save option into the database
update_option('new_field', $new_field_value);
restore_current_blog();
}
add_action('wpmu_new_blog', 'add_new_blog_field');
作为显示领域到页面上,你可以使用JavaScript的方法。您只需将一个JavaScript文件添加到“添加新网站”页面,然后在页面的onLoad中将该字段插入页面上的正确位置。您应该添加一个名为'blog [new_field]'的输入字段。我们创建,一旦加载以下JavaScript文件,增加了一个新的领域“添加新网站页面:
(function($) {
$(document).ready(function() {
$('<tr class="form-field form-required"></tr>').append(
$('<th scope="row">New field</th>')
).append(
$('<td></td>').append(
$('<input class="regular-text" type="text" title="New Field" name="blog[new_field]">')
).append(
$('<p>Explanation about your new field</p>')
)
).insertAfter('#wpbody-content table tr:eq(2)');
});
})(jQuery);
现在唯一剩下要做的就是包括这个文件到”添加新网站页面,加入给你的插件文件:
// Only add the script for the page site-new.php (the page hook).
add_action("admin_print_scripts-site-new.php", 'my_admin_scripts');
function my_admin_scripts() {
wp_register_script('yourScript', plugins_url('js/yourScript.js', __FILE__));
wp_enqueue_script('yourScript');
}
进一步的建议可能是,根据您的需要:在使用add_settings_field,常规设置页面添加一个输入框,使得用户可以在以后(也许只是编辑如果这是该字段的默认设置)。 A'以后不能改变这个字段'的方法。
我希望这是您所需要的帮助。
谢谢,这是一个非常好的答案!值得一个+100 :) – 2012-05-09 17:43:46
非常好,但一个小小的变化:'if(!empty($ _ POST ['blog'] ['new_field'])) $ new_field_value = $ _POST ['blog'] ['new_field '];'for'if(!isset($ _ POST ['blog'] ['new_field']))return FALSE;'...考虑new_field的空值,可以接受为正确的值 – Mariyo 2014-01-22 09:31:15
由于WP 4.5,在site-new.php的窗体标签内部存在一个动作挂钩,名为“network_site_new_form”。 – Berend 2016-10-27 11:08:43