WordPress user registration with custom form

1

I have a user registration form within my shortcode.

Complete code

     <?php
         $user_login_RU = $_POST['username_RU'];
         $user_email_RU= $_POST['email_RU'];
            if(isset($user_login_RU, $user_email_RU)){
               $teste = register_new_user($user_login, $user_email);
               if ( !is_wp_error($teste) ) {
                  $redirect_to = !empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : 'wp-login.php?checkemail=registered';
            wp_safe_redirect( $redirect_to );
            }

        //register meta values
          register_users_save_dados( $id_new_user, $_POST);
    }

      function register_users_shortcode(){
       global $post;
     ?>
        <div id="form1">
           <form id="form1" name="form1"action="" method="POST" ectype="multipart/form-data">
             <p>
                <label for="nome_RU">Username:</label>
                <br />
                <input type="text" name="username_RU" id="username_RU" value="<?php echo get_post_meta( $post->ID, '_username_RU', true ); ?>" />
            </p>
            <p>
                <label for="email_RU">E-mail:</label>
                <br />
                <input type="email" name="email_RU" id="email_RU"value="<?php echo get_post_meta( $post->ID, '_email_RU', true ); ?>" />
            </p>
            <p>
                <h2>Tipo de cadastro</h2>
                    Empregador <input type="radio" name="type_user" onclick="document.getElementById('empregador').style.display = 'block'; document.getElementById('trabalhador').style.display = 'none'"value="<?php echo get_post_meta( $post->ID, '_empregador', true ); ?>">
                    Trabalhador <input type="radio" name="type_user" onclick="document.getElementById('empregador').style.display = 'none'; document.getElementById('trabalhador').style.display = 'block'"value="<?php echo get_post_meta( $post->ID, '_Empregador', true ); ?>">
            </p>
            <div id="empregador" style="display:none">
                <h3>Identificação do empregador</h3>
                    <strong>*Tipo de indentificação:</strong><br />
                        <?php echo register_render("tipo_id_empregador", "radio","tipo_id_empregador" ,array("CNPJ","CPF","CEI")); ?><br />
                    <strong>*Número de indentificação:</strong>
                        <?php echo register_render('numeroIdEmpregador','text'); ?>
                <h3>Dados referente ao empregador</a></h3>
                    <strong>*Razão social:</strong> 
                        <?php echo register_render('razaoSocialEmpregador','text'); ?>
                    <strong>*Nome fantasia:</strong>
                        <?php echo register_render('nomeFantasiaEmpregador','text'); ?>

                <h3>Localização da empresa</h3>
                    <strong>*Logradouro:</strong>
                        <?php echo register_render('logradouroEmpregador','text'); ?>
                <br />
            </div>

            <div id="trabalhador" style="display:none">
                <h3>Indentificação do Trabalhador</h3>
                    <strong>*Número de Indentificação (PIS/PASEP/NIS/NIT)</strong>
                        <?php echo register_render('numeroIdTralhador','text'); ?> <br />
                    <strong>*Nome da mãe do trabalhador</strong>
                        <?php echo register_render('maeTrabalhador','text'); ?>
            </div>
            <input type="submit" name="sub" value="cadastrar">
        </form>
    </div>
        <?php }

    add_shortcode('my-form','register_users_shortcode');

function to save dynamic inputs

      function register_users_save_dados( $id_register_users,$value) {
         foreach ($value as $key) {
            if(isset($_POST[$key]))
                add_post_meta( $id_register_users, '_$key', strip_tags( $_POST[$key] ) );
    }
} 

I can not register a new user, the following log error occurs:

  

Call to undefined function get_user_by()

    
asked by anonymous 09.10.2014 / 17:01

1 answer

3

Your issue is in "redirects to the following page in PHP" . Surely you have made a separate page that is out of the scope of WordPress and does not recognize its functions.

Here is an example of how to do it using the same page where the shortcode is printed. Note the use nonce to validate the form request . It is essential to use custom names in form, name="my-msg" , this avoids conflicts with WP's own variables.

add_shortcode( 'my-form', function() {
    global $post;
    $nonce = wp_nonce_field( 'sopt_35412', 'sopt_35412', true, false ); // false == não faz echo
    // Sintaxe Heredoc
    // http://php.net/manual/pt_BR/language.types.string.php#language.types.string.syntax.heredoc
    $form = <<<HTML
    <form id="form1" name="form1" method="post" action="">
        $nonce
        <label>Sua mensagem
            <input type="text" name="my-msg" id="msg" />
        </label>
        <p>
        <label>Submit
            <input type="submit" name="my-submit" id="submit" value="Submit" />
        </label>
        </p>
    </form>
HTML;

    if( wp_verify_nonce( $_POST['sopt_35412'], 'sopt_35412' ) && isset( $_POST["my-submit"] ) ) {
        $form = $form . sopt_acessivel();
    } else if( isset( $_POST["my-submit"] ) ) { // Só para testar o nonce, troque o nome do nonce no form para ver o efeito 
        $form = $form . '<h1 style="color:#f00">ERRO DE SEGURANÇA</h1>';
    }
    return $form;
});

function sopt_acessivel() {
    return sprintf( '<h3>Postou msg:</h3><p>%s</p>', $_POST["my-msg"] );
}
    
09.10.2014 / 18:50