WordPress + 多站点:如何将自定义博客选项添加到网络管理中的“添加新站点"表单? [英] WordPress + Multisite: How to add custom blog options to Add New Site form in Network Admin?

查看:20
本文介绍了WordPress + 多站点:如何将自定义博客选项添加到网络管理中的“添加新站点"表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我自定义的 WordPress 多站点安装中,我需要在用于创建新博客站点的输入表单中添加一个简单的文本字段,该表单位于

In a WordPress Multisite installation I'm customizing, I need to add a simple text field to the entry form for creating new blog sites, which is located at

网络管理员>网站 >添加新

当然,我需要这个字段与来自该表单的其他元数据一起保存在 {new_blog_prefix}_options 表中.

Naturally I need this field to get saved along with the other meta data from that form, in the {new_blog_prefix}_options table.

我对实现这一目标的最简单、最直接和/或正确的方式"(即 WordPress 方式)特别感兴趣,但我会选择 The Way That Works™!

I'm particularly interested for the simplest, most straightforward, and/or the "right way" (i.e. The WordPress Way) to accomplish this, but I'll settle for The Way That Works™!

到目前为止,我在研究中遇到了许多死胡同:

So far I've encountered numerous dead-ends in my research:

据我所知,设置 API (a) 可能不适用于网络管理部分(尽管这可能在最近版本的 WP);此外,(b) 似乎只允许您在仪表板的 Settings 部分添加/修改屏幕.

As far as I can tell, the Settings API (a) may not work for the Network Admin section (although this may have changed in a recent version of WP); furthermore, (b) it seems like this only lets you add/modify screens in the Settings section of the Dashboard.

wpmu_options 钩子 - [钩子数据库]

似乎是专门用于向网络设置屏幕添加选项的钩子.

Seems to be a hook exclusively for adding options to the Network Settings screen.

add_site_option/add_blog_option - [WP Codex]

我能找到的最近的;似乎允许将特定于站点的选项添加到 {blogsite_prefix}_options 表,但仍然无助于向管理表单添加选项.

Closest I can find; seems to allow adding site-specific options to the {blogsite_prefix}_options table, but still doesn't help with adding options to the Admin form.

所以...没有骰子.任何帮助表示赞赏!

So... no dice. Any help is appreciated!

推荐答案

如果你看看 源代码 '添加新站点' 页面,你可以看到 WordPress 没有为此提供钩子.当然,您可以自己添加一个钩子,但通常不好的做法编辑 WordPress 核心.

If you look at the source code of the 'Add New Site' page, you can see that WordPress does not provide a hook for this purpose. Of course, it would be possible to add a hook yourself, but it is generally bad practice to edit the WordPress core.

但是,一旦提交并且所有信息都存在,页面就会调用函数wpmu_create_blog().在这个函数中有一个钩子被调用,即动作wpmu_new_blog:

However, once submitted and all information is present, the page calls the function wpmu_create_blog(). In this function there is a hook called, namely the action wpmu_new_blog:

do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );

此时博客已经创建.但是,我们仍然可以通过挂钩此操作并将其保存到数据库中来检测字段是否已提交.我们将以下内容添加到我们的插件文件(或模板)中:

At this point the blog is already created. However, we can still detect if a field was submitted by hooking onto this action and saving it into the database. We add the following into our plugin file (or template):

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 文件添加到添加新站点"页面,然后在页面加载时将该字段插入页面上的正确位置.您应该添加一个名为blog[new_field]"的输入字段.我们创建以下 JavaScript 文件,加载后,会向添加新站点"页面添加一个新字段:

As for displaying the field onto the page, you could use a JavaScript approach. You add a javascript file, solely to the 'Add New Site' page, and onLoad of the page you insert the field into the correct position on the page. You should add an input field with the name 'blog[new_field]'. We create the following JavaScript file which, once loaded, adds a new field to the 'Add New Site' page:

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

现在唯一要做的就是将此文件添加到添加新站点"页面中,方法是将其添加到您的插件文件中:

Now the only thing left to do is include this file onto the 'Add New Site' page, by adding this to your plugin file:

// 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,以便用户可以稍后对其进行编辑(并且可能仅当它是该字段的默认设置时).一种以后不能更改此字段"的方法.

Further suggestion could be, depending on your needs: Add an input field in your general settings page using add_settings_field, such that a user can edit it later (and maybe only if it is the default setting for this field). A 'you cannot change this field later' approach.

我希望这是您需要的帮助.

I hope this is the help you needed.

这篇关于WordPress + 多站点:如何将自定义博客选项添加到网络管理中的“添加新站点"表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆