如果您查看“添加新站点”页面的源代码,您可以看到WordPress没有为此提供挂钩。当然,您可以自己添加一个钩子,但编辑WordPress核心通常是不好的做法。
但是,一旦提交并显示所有信息,页面将调用该函数wpmu_create_blog()
. 在这个函数中有一个称为钩子的函数,即actionwpmu_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\\u 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
, 这样,用户以后可以编辑它(可能只有当它是此字段的默认设置时)。“以后不能更改此字段”方法。