简体   繁体   English

带有自定义页面的Wordpress中的联系表

[英]contact form in wordpress with custom pages

I'm trying to do sign-up/login on my website but I can not understand how to do it because I'm trying to do a wordpress theme and in my theme there isn't widget setion. 我正在尝试在我的网站上进行注册/登录,但是我不明白该怎么做,因为我正在尝试做一个wordpress主题,而在我的主题中没有小部件设置。 I've already created template pages and I want to integrate those pages. 我已经创建了模板页面,并且想要集成这些页面。 Help me 帮我

Adding Sign in and Sign Up in Custom WP Theme 添加登录并注册自定义WP主题

Here is the form fields which you can edit later on according to your requirement. 这是表单字段,您以后可以根据需要进行编辑。

You have user_nick_name which can be used as username. 您有user_nick_name可用作用户名。 Email and then password in last submit button. 电子邮件,然后在最后一个提交按钮中输入密码。

<form id="sign_up_form" action="" method="post">
    <input type="text" id="user_nickname" name="user_nickname" placeholder="Enter user desire username" />
    <input type="text" id="user_email" name="user_email" placeholder="Enter your email" />
    <input id="user_pass" name="user_pass" type="password" placeholder="Enter your password">
    <input type="submit" value="Register now" />
</form>

You need to place below code in JS file this Code will help you to generate Ajax Request. 您需要将以下代码放置在JS文件中,该代码将帮助您生成Ajax请求。

Important Note: I have added below code in ajax-signup-script.js which i have placed in js folder make sure you keep the same name in order to proceed with same code, if you want to rename this file you will also need to replace this from one more place to get things working. 重要说明:我在ajax-signup-script.js中添加了以下代码,该代码已放置在js文件夹中,请确保您使用相同的名称以继续执行相同的代码,如果要重命名此文件,还需要从另一个地方替换掉它以使事情正常进行。

jQuery(document).ready(function($){
    "use strict";

 // Sign Up
 $('form#sign_up_form').on('submit', function(e){
    $('form#sign_up_form p.status').show().text(ajax_signup_object.loadingmessage);
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: ajax_signup_object.ajaxurl,
        data: { 
            'action': 'custom_signup', //calls wp_ajax_nopriv_ajaxlogin
            'nickname': $('form#sign_up_form #user_nickname').val(), 
            'user_email': $('form#sign_up_form #user_email').val(), 
            'user_pass': $('form#sign_up_form #user_pass').val() },
        success: function(data){
            $('form#sign_up_form p.status').text(data.message);
            if (data.signup == true){
                document.location.href = ajax_signup_object.redirecturl;
            }
        }
    });
    e.preventDefault();
});
});

Almost finished what we have started you need to place this code in functions.php. 几乎完成了我们已经开始的工作,您需要将此代码放置在functions.php中。

  function wordpress_custom_signup_function(){

    //Get Defined Role by WordPress
    $default_role = get_option('default_role');

    $nickname = $_POST['nickname'];
    $user_email = $_POST['user_email'];
    $user_pass = $_POST['user_pass'];       

    //Check For the NickName Empty
    if(isset($nickname) && $nickname <> ''){
        $userdata = array(
            'user_login'    => $nickname,
            'user_email'  => $user_email,
            'user_pass'  => $user_pass,
            'role' => $default_role
        );
        $exists = email_exists($user_email);
        $user_signup = wp_insert_user( $userdata );
        if ( !$exists ){
            if ( is_wp_error($user_signup) ){
                echo json_encode(array('signup'=>false, 'message'=>esc_attr__('Please verify the details you are providing.','custom-theme')));
            } else {
                echo json_encode(array('signup'=>true, 'message'=>esc_attr__('Your request submitted successfully, Redirecting you to login page!','custom-theme')));
            }
        }else{
            echo json_encode(array('signup'=>false, 'message'=>'Notice: Email already exists!'.$exists.''));            
        }
    }else{
        echo json_encode(array('signup'=>false, 'message'=>esc_attr__('Please verify the details you are providing.','custom-theme')));
    }

    die();
}   

 function custom_function_for_signup(){

    wp_register_script('ajax-signup-script', get_template_directory_uri().'/js/ajax-signup-script.js', array('jquery') ); 
    wp_enqueue_script('ajax-signup-script');

    wp_localize_script( 'ajax-signup-script', 'ajax_signup_object', array( 
        'ajaxurl' => esc_url(admin_url( 'admin-ajax.php' )),
        'redirecturl' => esc_url(home_url()),
        'loadingmessage' => esc_attr__('Sending user info, please wait...','custom-theme')
    ));

    // Enable the user with no privileges to run ajax_login() in AJAX
    add_action('wp_ajax_custom_signup', 'wordpress_custom_signup_function');
    add_action('wp_ajax_nopriv_custom_signup', 'wordpress_custom_signup_function' );
}

add_action('init', 'custom_function_for_signup');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM