在自定义插件中覆盖网络面板的默认添加/编辑站点表单

时间:2020-08-25 作者:princeexpedition

My requirement is to add couple of custom fields in the add/edit a site forms without touching the WordPress core. I tried to copy the site-new.php from /wp-admin/network and paste it into my custom plugin but it fails due to many dependencies. I dont think so it is good exercise to copy and paste the whole network folder inside the plugin.

I also tried to callout this hook but instead of override, it just show me the fields underneath of the form. I guess I need to add hook when users submit the form. but couldn\'t find the hook that I need to callout to save additional fields data in DB.

 add_action(\'network_site_new_form\',  array($this, \'add_extra_new_site_fields\'));

Added this function

function add_extra_new_site_fields()
{
    ?>
    <form method="post" action="<?php echo network_admin_url( \'site-new.php?action=add-site\' ); ?>" novalidate="novalidate">
        <?php wp_nonce_field( \'add-blog\', \'_wpnonce_add-blog\' ); ?>
        <table class="form-table" role="presentation">
            <tr class="form-field form-required">
                <th scope="row"><label for="site-address"><?php _e( \'Site s Address (URL)\' ); ?> <span class="required">*</span></label></th>
                <td>
                    <?php if ( is_subdomain_install() ) { ?>
                        <input name="blog[domain]" type="text" class="regular-text" id="site-address" aria-describedby="site-address-desc" autocapitalize="none" autocorrect="off" required /><span class="no-break">.<?php echo preg_replace( \'|^www\\.|\', \'\', get_network()->domain ); ?></span>
                        <?php
                    } else {
                        echo get_network()->domain . get_network()->path
                        ?>
                        <input name="blog[domain]" type="text" class="regular-text" id="site-address" aria-describedby="site-address-desc" autocapitalize="none" autocorrect="off" required />
                        <?php
                    }
                    echo \'<p class="description" id="site-address-desc">\' . __( \'Only lowercase letters (a-z), numbers, and hyphens are allowed.\' ) . \'</p>\';
                    ?>
                </td>
            </tr>
            <tr class="form-field form-required">
                <th scope="row"><label for="site-title"><?php _e( \'Site Title\' ); ?> <span class="required">*</span></label></th>
                <td><input name="blog[title]" type="text" class="regular-text" id="site-title" required /></td>
            </tr>
            <?php
            $languages    = get_available_languages();
            $translations = wp_get_available_translations();
            if ( ! empty( $languages ) || ! empty( $translations ) ) :
                ?>
                <tr class="form-field form-required">
                    <th scope="row"><label for="site-language"><?php _e( \'Site Language\' ); ?></label></th>
                    <td>
                        <?php
                        // Network default.
                        $lang = get_site_option( \'WPLANG\' );

                        // Use English if the default isn\'t available.
                        if ( ! in_array( $lang, $languages ) ) {
                            $lang = \'\';
                        }

                        wp_dropdown_languages(
                            array(
                                \'name\'                        => \'WPLANG\',
                                \'id\'                          => \'site-language\',
                                \'selected\'                    => $lang,
                                \'languages\'                   => $languages,
                                \'translations\'                => $translations,
                                //\'show_available_translations\' => current_user_can( \'install_languages\' ) && wp_can_install_language_pack(),
                            )
                        );
                        ?>
                    </td>
                </tr>
            <?php endif; // Languages. ?>
            <tr class="form-field form-required">
                <th scope="row"><label for="admin-email"><?php _e( \'Admin Email\' ); ?> <span class="required">*</span></label></th>
                <td><input name="blog[email]" type="email" class="regular-text wp-suggest-user" id="admin-email" data-autocomplete-type="search" data-autocomplete-field="user_email" aria-describedby="site-admin-email" required /></td>
            </tr>
            <tr class="form-field">
                <td colspan="2" class="td-full"><p id="site-admin-email"><?php _e( \'A new user will be created if the above email address is not in the database.\' ); ?><br /><?php _e( \'The username and a link to set the password will be mailed to this email address.\' ); ?></p></td>
            </tr>
            <tr class="form-field form-required">
                <th scope="row"><label for="admin-email"><?php _e( \'Account ID\' ); ?> <span class="required">*</span></label></th>
                <td><input name="blog[account_id]" type="input" class="regular-text" id="admin-account_id" required /></td>
            </tr>
        </table>

        <?php
        /**
         * Fires at the end of the new site form in network admin.
         *
         * @since 4.5.0
         */
        //do_action( \'network_site_new_form\' );

        submit_button( __( \'Add Site\' ), \'primary\', \'add-site\' );
        ?>
    </form>
    <?php
}

give me your suggestions.

2 个回复
SO网友:Tom J Nowell

将标记添加到“添加新网站”表单中可以如下所示将HTML标记添加到该表单中:

add_action(
    \'network_site_new_form\',
    function() {
        ?>
        <p>Hello World</p>
        <?php
    }
);

enter image description here

向编辑网站表单添加标记

No. This cannot be done via PHP.

对于站点编辑信息屏幕,不存在执行此操作的挂钩。

However, 您可以通过network_edit_site_nav_links 过滤器,允许您创建自定义网络管理页面,该页面将站点ID作为URL参数传递。但是,您需要通过重新创建选项卡network_edit_site_nav 并保存和展示自己。

创建站点时,如何保存额外的输入

有点

没有专用的钩子来保存这些字段。添加HTML的钩子不是现有字段所在表的一部分,它们不会对齐。如果没有用户使用所选的管理员电子邮件,则在创建用户之前和之后都有一个钩子,但它发生在创建站点之前。您可以使用wp_insert_site 钩子在网站创建后在网站上执行工作,但是您应该检查您的字段是否存在,它甚至可能不是web请求,也可能不是网络管理员:

do_action( \'wp_insert_site\', $new_site );
也要小心,当这种情况发生时,WP将处于安装模式,通常可用的东西可能不可用。这些事情是什么还不清楚。

我的建议是不要这样做。自定义网络管理区域的最佳方法是完全删除它们,然后通过管理菜单API使用全新的网络管理页面重新实现它们。

network admin API不是用于扩展的,network admin的构建也不是为了按照您想要的方式进行扩展。通常开发起来微不足道的东西应该被视为顶级超硬产品。

此外,即使您可以存储这些值,也没有用于查看它们的UI,并且无法扩展现有UI。你必须从头开始构建它。

SO网友:princeexpedition

找到了在“编辑站点”多站点设置页面上添加带有选项的自定义选项卡的非常有用的提示。

检查此项->;https://rudrastyh.com/wordpress-multisite/custom-tabs-with-options.html

相关推荐

Multisite installation steps?

我启动了Multisite 特色我想在子域中创建一个站点。我有两个问题:在多站点管理仪表板中创建站点之前,还是之后,我必须在服务器上创建子域</如果是,在multisite admindashboard中创建站点之前,还是之后,我必须在此子域中安装WordPress安装我在StackExchange上找到了这个,但它对我没有帮助,或者我就是不明白。Subdomain multisite gives 404 on sub sites谢谢弗朗索瓦