如何在自定义注册表中添加字段、进行验证并保存到数据库?

时间:2021-08-15 作者:Vladimir Dimitrow

我将WP主题与自定义注册表一起使用。我想给它添加两个字段(电话号码和公司名称)。在表单的php文件中添加什么,在主题的函数文件中添加什么来验证字段和保存值​​在数据库中?

提前感谢

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

可以修改主题的注册,但绝对不推荐。您可能只能通过直接修改主题文件来添加其他字段,尤其是使用诸如用户注册之类的方法。如果你想让你的主题保持最新,那就很难坚持下去。

我建议您改为:添加您自己的页面,用户将在其中添加您需要捕获的其他信息,并在注册后捕获这些信息。

让用户继续使用正常的未修改注册表

  • 用户登录检查是否通过user_meta

    使用操作挂钩检查用户是否填写了其他信息。我建议为以下内容使用childtheme或制作自己的插件:

    //\'template_redirect\' hook works well for what we need to do. 
    add_action( \'template_redirect\', \'vlad_additional_info_check\' );
    
    /**
     * Check if a user has filled out additional required information to complete registration, if not redirect them to page to complete registration.
     */
    function vlad_additional_info_check() {
        //If the user is not logged in, or an administrator, we can just return out of the  function and do nothing further.
        if ( ! is_user_logged_in() || current_user_can( \'administrator\' ) ) {
            return;
        }
    
        //Check if user has filled out their additional information or not. I am using vlad_additional_info as the meta key, you can use anything.
        if ( ! get_user_meta( get_current_user_id(), \'vlad_additional_info\', true ) === "" ) {
            //Here the user has not filled out the fields
            global $post;
            $page_slug = $post->post_name;
            //You want to redirect the user to a page, for example \'complete-registration\', with the for to capture the additional data. First check that they are not already on the page.
            if ( $page_slug !== \'complete-registration\' ) {
                wp_redirect( network_site_url( \'/complete-registration/\' ) );
                exit();
            }
        }
    }
    
    对于您的表单和捕获数据,您将拥有以下内容:

    //complete-registration.php
    //Check if the form data has been submitted
    if ( !isset( $_POST[\'submit_complete_registration\'] ) ) {
        //Here is where the form goes
        ?>
        <form action="?" method="post">
            <label>Phone Number *
                <input type="text" value="" name="vlad_phone_number">
            </label>
        </form>
        <?php
    } else {
        //Form data has been submitted
        //Here you can validate the forms posted fields. Make sure you collectively validate that all fields are valid, before updating any user meta.
        if ( isset( $_POST[\'vlad_phone_number\'] ) ) {
            //Do validations...
            $valid_number = $_POST[\'vlad_phone_number\'];
        }
    
        if ( $everything_is_valid ) {
            $user_id = get_current_user_id();
            update_user_meta( $user_id, \'vlad_phone_number\', $valid_number ); //can use any name for your meta
            //...
            //...
            //Now finally update the user meta that we use to check if a user has submitted this info:
            update_user_meta( $user_id, \'vlad_additional_info\', true ); //can use any name for your meta
    
        } else {
            ?><h2>Please enter a valid...</h2><?php
        }
    }
    
    然后,要在页面上呈现表单,可以使用一个短代码来加载complete-registration.php 文件:

    add_shortcode( \'vlad-additional-form\',\'vlad_additional_info_form_render\' );
    
    function vlad_additional_info_form_render() {
        ob_start();
        require \'PLUGIN_OR_CHILDTHEME_DIRECTORY/complete-registration.php\'; //Replace with your own file directory
        return ob_get_clean();
    }
    
    我们正在使用的一些WordPress函数的一些链接和信息:

    https://developer.wordpress.org/reference/functions/get_user_meta/https://developer.wordpress.org/reference/functions/update_user_meta/https://developer.wordpress.org/reference/functions/add_shortcode/https://developer.wordpress.org/reference/functions/add_action/https://developer.wordpress.org/reference/functions/wp_redirect/

  • 相关推荐

    Forms cut off when on mobile

    信用卡处理器生成的表单通过javascript加载。正常加载时,可以看到模板工作良好here, 但是移动用户看到下面的内容,无法滚动。据说表单有一个移动版本,但显然无法加载?使用以下方法将表单加载到页面上:<div id=\"bbox-root\"></div> <script type=\"text/javascript\"> window.bboxInit = function () { bbox.show