自定义注册表和自定义字段

时间:2014-08-26 作者:skifast

我已使用以下代码为我的网站创建了自定义注册表:

<?php
    /*
    Template Name: Registrazione-Custom
    */
    get_header(); ?>

<!-- Row for main content area -->

            <div id="content" class="left eight columns" role="main">

            <div class="post-box">

                            <?php get_template_part(\'/includes/content\', \'page\'); ?>     



                <div class="wrapper">   

        <?php

        $err = \'\';

        $success = \'\';



        global $wpdb, $PasswordHash, $current_user, $user_ID;

        if(isset($_POST[\'task\']) && $_POST[\'task\'] == \'register\' ) {

            $pwd1 = $wpdb->escape(trim($_POST[\'pwd1\']));

            $pwd2 = $wpdb->escape(trim($_POST[\'pwd2\']));

            $first_name = $wpdb->escape(trim($_POST[\'first_name\']));

            $last_name = $wpdb->escape(trim($_POST[\'last_name\']));

            $email = $wpdb->escape(trim($_POST[\'email\']));

            $username = $wpdb->escape(trim($_POST[\'username\']));


            if( $email == "" || $pwd1 == "" || $pwd2 == "" || $username == "" || $first_name == "" || $last_name == "") {

                $err = \'Devi compilare tutti i campi obbligatori\';

            } else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {

                $err = \'Indirizzo email non valido.\';

            } else if(email_exists($email) ) {

                $err = \'Indirizzo email gi&agrave registrato.\';

            } else if($pwd1 <> $pwd2 ){

                $err = \'Le password non corrispondono.\';        

            } else {



                $user_id = wp_insert_user( array (\'first_name\' => apply_filters(\'pre_user_first_name\', $first_name), \'last_name\' => apply_filters(\'pre_user_last_name\', $last_name), \'user_pass\' => apply_filters(\'pre_user_user_pass\', $pwd1), \'user_login\' => apply_filters(\'pre_user_user_login\', $username), \'user_email\' => apply_filters(\'pre_user_user_email\', $email), \'role\' => \'subscriber\' ) );

                if( is_wp_error($user_id) ) {

                    $err = \'Errore in fase di registrazione, nome utente gi&agrave registrato\';



                } else {

                    do_action(\'user_register\', $user_id);



                $success = \'You\\\'re successfully register\';

                echo \'<script language="javascript">

                  top.location.href = "http://www.miosito.it/";

            </script>\';

                // e inviamo una mail con la riuscita registazione

    mail ($email, "Registrazione Mio Sito", "Complimenti registrazione effettuata con successo. Ti ricordiamo i tuoi dati per l\'accesso al sito username: $username, e password: $pwd1", "From: [email protected]");

                           // e inviamo una mail ad admin di registrazione nuovo utente

    mail ("[email protected]", "Registrazione Nuovo Utente Area Riservata", "Il nuovo utente $first_name $last_name con nome utente $username si è appena registrato all\'area registrata del sito Alpe Adria Insurance Brokers", "From: [email protected]");



                     wp_set_current_user( $user_ID, $current_user );

                     do_action(\'set_current_user\');

                     $redirect_to = site_url(\'www.google.it\');

                     var_dump($redirect_to);



                     wp_safe_redirect($redirect_to);

                }



            }



        }

        ?>



            <!--display error/success message-->

        <div id="message">

            <?php 

                if(! empty($err) ) :

                    echo \'<p class="error">\'.$err.\'</p>\';

                endif;

            ?>



            <?php 

                if(! empty($success) ) :

                    echo \'<p class="error">\'.$success.\'</p>\';

                endif;

            ?>

        </div>



        <form method="post">

            <h3>Registrati al Portale Mio Sito</h3>

            <div class="customregistrazione">

            <label class="customregistrazione">Cognome*</label>

            <input type="text" value="" name="last_name" id="last_name" style="width: 50%;" />

            <label >Nome*</label>

            <input type="text" value="" name="first_name" id="first_name" style="width: 50%;" />

            <label>Email*</label>

            <input type="text" value="" name="email" id="email" style="width: 50%;" />

            <label>Username*</label>

            <input type="text" value="" name="username" id="username" style="width: 50%;" />

            <label>Password*</label>

            <input type="password" value="" name="pwd1" id="pwd1" style="width: 50%;" />

            <label>Reinserisci Password*</label>

            <input type="password" value="" name="pwd2" id="pwd2" style="width: 50%;" />

            </div> 

            <div class="alignleft"><p><?php if($sucess != "") { echo $sucess; } ?> <?php if($err != "") { echo $err; } ?></p></div>

            <div>Compilando il seguente form di registrazione il cliente accetta quanto previsto nel <a href="http://www.miosito.it/disclaimer-privacy/">disclaimer</a></div>

            <button type="submit" name="btnregister" class="button" >Registrati</button>

            <input type="hidden" name="task" value="register" />

        </form>



    </div>





                </div>



            </div><!-- End Content row -->



            <?php get_sidebar( \'right\' ); ?>



    <?php get_footer(); ?>
我想在自定义注册表中添加一些自定义字段。如果我需要在默认注册表上添加自定义字段,我知道我必须在函数中使用此代码。php:

 //Aggiunta del nuovo elemento al form di registrazione
    add_action(\'register_form\',\'myplugin_register_form\');

    function myplugin_register_form (){
        $hobby = ( isset( $_POST[\'hobby\'] ) ) ? $_POST[\'hobby\']: \'\'; //salvataggio del campo hobby
        ?>
        <p>
            <label for="hobby"><?php _e(\'Hobby principale\',\'registrazione-utenti\') ?><br />
                <input type="text" name="hobby" id="hobby" class="input" value="<?php echo esc_attr(stripslashes($hobby)); ?>" size="25" /></label>
        </p>
        <?php
    }

    // Validazione dei dati inseriti.
    add_filter(\'registration_errors\', \'myplugin_registration_errors\', 10, 3);
    function myplugin_registration_errors ($errors, $sanitized_user_login, $user_email) {

        if ( empty( $_POST[\'hobby\'] ) ) // il campo hobby è obbligatorio e non può essere vuoto
            $errors->add( \'hobby_error\', __(\'<strong>ERROR</strong>: Devi inserire un hobby principale\',\'registrazione-utenti\') );

        return $errors;

    }

    // Salvataggio dei dati
    add_action(\'user_register\', \'myplugin_user_register\');
    function myplugin_user_register ($user_id) {
        if ( isset( $_POST[\'hobby\'] ) )
            update_user_meta($user_id, \'hobby\', $_POST[\'hobby\']);
    }


/*
 * Visualizzazione dei dati aggiuntivi nel back end
 * 
 * */


 add_action(\'show_user_profile\',\'show_the_new_meta_values\'); // hook usato quando un utente sta visualizzando il proprio profilo
 add_action(\'edit_user_profile\',\'show_the_new_meta_values\'); // hook usato quando un utente sta visualizzando il profilo di un altro utente

 function show_the_new_meta_values($user)
 {

    $hobby= get_user_meta($user->ID,\'hobby\', true);
    ?>
    <h3><?php _e(\'Informazioni aggiuntive\',\'registrazione-utenti\')?></h3>
        <table class="form-table">
            <tr>
                <th><label for="hobby"><?php _e(\'Hobby principale\',\'registrazione-utenti\') ?></label></th>
                <td><input type="text" name="hobby" id="hobby" value="<?php echo esc_attr($hobby); ?>" class="regular-text" /></td>
            </tr>
        </table>
    <?php
 }



 //aggiornamento dei dati
add_action(\'personal_options_update\', \'update_the_new_meta_values\');
add_action(\'edit_user_profile_update\', \'update_the_new_meta_values\');

function update_the_new_meta_values($user_id)
{

    $new_value = $_POST[\'hobby\'];

    update_user_meta($user_id,\'hobby\',$new_value); // dati aggiornati

    if ( get_user_meta($user_id,  \'hobby\', true ) != $new_value ) // controlla che i dati siano stati salavati nel db
    wp_die(__( \'Errore nel Database\',\'registrazione-utenti\' ) );
}
但是,如何在自定义注册表中添加自定义字段?

谢谢

1 个回复
SO网友:physalis

你可以尝试我最近使用的一种非常不同的方法。尝试一下WordPress插件饼状寄存器,它为您提供了一个简单的拖放界面,用于自定义注册表、添加各种自定义字段、发送的电子邮件以及激活的用户。希望这就是你需要的!

结束

相关推荐