Registration of users in Wordpress with custom form

Asked

Viewed 836 times

1

I have a user registration form inside 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] ) );
    }
} 

but I can’t register a new user, comes the following log error:

Call to Undefined Function get_user_by()

  • 2

    Any, please organize your code logically. It makes it easier for you to find the problems for yourself, and whoever helps you will read the code quickly without wasting time deciphering the flow of things.

  • Please edit the Question, the Answers field is for solutions. . . . . Didn’t you say where your codes are located, which file? Is it the same? . . . Tip: you are programming without having the WP_DEBUG turned on, it shows small program errors (type $var = $_POST['NOME'], that drops a notice and should be avoided): http://codex.wordpress.org/Debugging_in_WordPress

  • I never used wp_debug, obg for this tip.

  • You still haven’t told me where this code is. What a file?

  • It is inside a folder called shortcodes, inside the folder of my plugin, the function is in another folder called functions, and both are called in my root folder where I define the name of my plugin.

  • Yeah, without having one Minimum, Complete and Verifiable Example your code makes it hard to answer. I’m sorry.

Show 1 more comment

1 answer

3

Your problem is in "redirects to the following page in PHP". Surely you made an independent page that is outside the scope of Wordpress and does not recognize its functions.

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

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/en/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"] );
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.