通过自定义插件对注册表中的字段重新排序

时间:2013-11-18 作者:Osu

我想对默认WP注册表中的字段重新排序。我正在用插件添加其他字段,但它们显示在用户名和电子邮件字段下面。我的代码如下(基本上是从here).

有人知道我怎么做吗?我假设它涉及到向操作添加一个参数register_form, 但我在法典里找不到任何关于它的东西。此外,如果您对我如何改进验证(如有必要)有任何建议,我将不胜感激,因为我正在努力学习如何正确构建插件。

// Set variables
$theme_name = \'ibm\';

/* ****************************************************************** */
                    /* !ADD FORM ELEMENTS */
/* ****************************************************************** */ 

function osu_register_form() {
    // Get and set any values already sent
    $first_name = ( isset( $_POST[\'first_name\'] ) ) ? $_POST[\'first_name\']: \'\';
    $last_name = ( isset( $_POST[\'last_name\'] ) ) ? $_POST[\'last_name\']: \'\'; ?>
    <p>
        <label for="first_name"><?php _e(\'First Name\', $theme_name) ?><br />
        <input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr(stripslashes($first_name)); ?>" size="25" /></label>
    </p>
    <p>
        <label for="last_name"><?php _e(\'Last Name\', $theme_name) ?><br />
        <input type="text" name="last_name" id="last_name" class="input" value="<?php echo esc_attr(stripslashes($last_name)); ?>" size="25" /></label>
    </p>
<?php }

/* ****************************************************************** */
                        /* !VALIDATION */
/* ****************************************************************** */ 

function osu_registration_errors($errors, $sanitized_user_login, $user_email) {
    if ( empty( $_POST[\'first_name\'] ) ) {
        $errors->add( \'first_name_error\', __(\'<strong>ERROR</strong>: You must include a first name.\', $theme_name) );
    }
    if ( empty( $_POST[\'last_name\'] ) ) {
        $errors->add( \'last_name_error\', __(\'<strong>ERROR</strong>: You must include a last name.\', $theme_name) );
    }
    return $errors;
}


/* ****************************************************************** */
                    /* !SAVE USER META DATA */
/* ****************************************************************** */ 

function osu_user_register($user_id) {
    if ( isset( $_POST[\'first_name\'] ) ) {
        update_user_meta($user_id, \'first_name\', $_POST[\'first_name\']);
    }
    if ( isset( $_POST[\'last_name\'] ) ) {
        update_user_meta($user_id, \'last_name\', $_POST[\'last_name\']);
    }
}


// Add functions to WP actions and filters
add_action(\'register_form\',\'osu_register_form\');
add_filter(\'registration_errors\', \'osu_registration_errors\', 10, 3);
add_action(\'user_register\', \'osu_user_register\');

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

默认的登录表单对开发人员不太友好(现在比过去更友好)。如果你browse the source for that page 你应该能够说服自己。

你的插件将无法按你希望的方式操作页面。一个主题可以创建自己的登录页面,但即使这样也不能消除默认页面。

您可以尝试绕过登录页面,并通过以下方式替换它this answer 但正如回答中所述,它似乎容易出错wp-login.php 多次硬编码到核心源中。

结束

相关推荐

WP_QUERY ORDERBY POST__IN在循环中保持无效

我试图为主页查询一个自定义帖子数组,试图维护数组中给定的顺序。在某种程度上,这很好,WordPress可以找到帖子,但在循环中,顺序又被混淆了。首先,我使用pre_get_posts 钩子以更改主页上的主查询,如下所示:if ( is_home() && $query->is_main_query() ) { $query->set( \'post_type\', array( \'post\', \'page\', \'product\' ) );