Học WordPress + AI / WordPress cho người VIỆT !
WPShare247

Học wordpress cho người mới bắt đầu

  • Bắt đầu
    • Học WordPress
    • Elementor
    • WooCommerce
    • Tạo WordPress
    • Học WordPress kiểu mới 2025
    • Học PHP cơ bản
    • Tạo Website bằng WordPress
    • Quản trị WordPress
    • Sửa lỗi WordPress
    • Theme wordpress
    • Plugin WordPress
    • Hàm PHP
    • Seo WordPress
    • Bảo mật WordPress
    • Html & Css
    • Javascript và jQuery
    • Hướng dẫn lập trình
  • Video
    • Video TikTok
    • Video Shorts
  • Kho plugin
  • Tải Web Miễn Phí
  • Công cụ
    • Kiểm tra web WordPress
    • WordPress theme gì?
    • Check IP Website
    • Kiểm tra Tên Miền
    • Kiểm Tra SSL
    • Tạo File Disavow Google
    • Tạo QR Code
  • Giới thiệu
    • Khuyến Mãi
  • Đăng nhập
X
☰

Trang chủ » Cách tạo Form đăng ký, lưu dữ liệu trong WordPress bằng Ajax

Cách tạo Form đăng ký, lưu dữ liệu trong WordPress bằng Ajax

Ngày đăng: Xem: 401
Cách áp dụng các tính năng mới của WooCommerce 2025 vào cửa hàng WordPress của bạn Xem: 475

Cách áp dụng các tính năng mới của WooCommerce 2025 vào cửa hàng WordPress của bạn

Tích Hợp Gemini AI API Vào WordPress | Code Mẫu Cho Dev | Tạo Website Tự Động Viết Bài Chuẩn SEO Xem: 172

Tích Hợp Gemini AI API Vào WordPress | Code Mẫu Cho Dev | Tạo Website Tự Động Viết Bài Chuẩn SEO

Thử nghiệm 2025: AI có thực sự viết được Plugin WordPress? Xem: 182

Thử nghiệm 2025: AI có thực sự viết được Plugin WordPress?

Nhúng file này vào functions.php

require_once('course/course-init.php');

1. Nội dung file course-init.php

<?php
add_action( 'admin_init', 'flush_rewrite_rules' );
add_action( 'init', 'wpshare247_register_posttype' );
function wpshare247_register_posttype(){
    $posttype = 'wpshare247-course'; // dùng để query
    $posttype_slug = 'khoa-hoc'; // http://tenmien.com/dich-vu/ten-dich-vu-1
    $args = array(
              'labels' => array(
                            'name' => __( 'Đăng ký khóa học', 'twentyseventeen' ),
                            'singular_name' => __( 'Đăng ký khóa học' ),
                            'add_new' => __( 'Thêm mới', 'twentyseventeen' )
                          ),
              'public' => true, // true là cho phép query, tìm kiếm, hiển thị trong menu
              'publicly_queryable' => true, // true có tạo đường dẫn, http://tenmien.com/dich-vu/ten-dich-vu-1
              'show_ui' => true, // true cho phép hiển thị trong menu admin, menu top admin
              'show_in_menu' => true, // cho phép post type này hiển thị trong admin menu nào đó
              'capability_type' => 'post', // kế thừa phân quyền từ post hoặc page....
              'rewrite' => array( 'slug' => $posttype_slug ), 
              'menu_icon'=>'dashicons-images-alt2',
              'supports' => array('title','editor', 'thumbnail') //'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'
            );

    register_post_type( $posttype, $args);
}

//Xử lý ajax---------------
function wpshare247_course_register_display(){  
    $arr_response = array();
    
    //_REQUEST - 1 => Nhận dữ liệu từ Ajax Javascript
    $name = $_REQUEST['name'];
    $phone = $_REQUEST['phone'];
    $email = $_REQUEST['email'];
    $description = $_REQUEST['description'];

    //_ACTION - 2 => Xử lý tại đây
    $post_content = '';
    $post_content .= '<b>Thông tin đăng ký</b><br/>';
    $post_content .= 'Họ tên: '.$name.'<br/>';
    $post_content .= 'Điện thoại: '.$phone.'<br/>';
    $post_content .= 'Email: '.$email.'<br/>';
    $post_content .= 'Nội dung: '.$description.'<br/>';

    $my_post = array(
    	'post_type' => 'wpshare247-course',
    	'post_title' => wp_strip_all_tags( $name ),
    	'post_content' => $post_content,
    	'post_status' => 'publish',
    	'post_author'   => get_current_user_id()
    );
    $post_id = wp_insert_post( $my_post);

    //Send email
    if($email && $post_id){
    	$admin_email = 'email-gui@wpshare247.com';
    	$headers[] = "Content-Type: text/html; charset=utf-8\r\n";
        $headers[] = "From: " . $admin_email . " <" . $admin_email . ">\r\n";
    	$subject = $name . ' đã đăng ký khóa học với số điện thoại: '.$phone;	

    	$email_content  = $post_content;

    	$is_sent = wp_mail(trim($email), $subject, $email_content, $headers, "");
    }
    

    //_RESPONSE - 3 => Trả kết quả về cho Ajax Javascript

    
    $arr_response = array(
                            'myid' => $post_id
                        );
    wp_send_json($arr_response);
    die();
    
}
add_action( 'wp_ajax_wpshare247_course_register', 'wpshare247_course_register_display' );
add_action('wp_ajax_nopriv_wpshare247_course_register', 'wpshare247_course_register_display');

 

2. Nội dung page-course.php

<?php
/* Template Name: Khóa học */
get_header(); ?>

<section id="wpshare247-page">
    <div class="container">
    	<div class="page-content" style="padding-top:30px;">
    		<?php 
    		while ( have_posts() ) : the_post();
    			?>
    			<div id="course-content">
    				<h1><?php the_title(); ?></h1>

    				<div class="entry-content"><?php the_content(); ?></div>

    				<form id="course-form">
    					<div class="f-group">
                           <div id="notify" style="color:red;"></div>
                       </div>

    					<div class="f-group">
                           <input type="text" name="full-name" placeholder="Họ và tên" class="wpshare-text reset-data">
                       </div>

                       <div class="f-group">
                           <input type="tel" name="phone" placeholder="Điện thoại" class="wpshare-text reset-data">
                       </div>

                       <div class="f-group">
                           <input type="email" name="email" placeholder="email" class="wpshare-text reset-data">
                       </div>

                       <div class="f-group">
                           <textarea name="description" placeholder="Nội dung" class="wpshare-description reset-data"></textarea>
                       </div>

                       <div class="f-group">
                            <button id="register" type="button" class="btn button">Đăng ký <img style="display:none;" height="30" width="30" class="loading" src="<?php echo get_stylesheet_directory_uri(); ?>/course/loading.svg"></button>
                       </div>
    				</form>
    			</div>
    			<?php
    		endwhile; // End of the loop.
    		?>
    	</div>
    </div>
 </section>

 <script type="text/javascript">

 	var wpshare247_ajax_url = '<?php echo admin_url( 'admin-ajax.php' ); ?>';

 	jQuery(document).ready(function($) {
 		jQuery("#course-form #register").click(function(event) {
 			//console.log('vào đây.....');

 			var err = 0;

 			var name = jQuery("#course-form").find('[name=full-name]').val();
 			if(name==''){
 				err++;
 				jQuery("#course-form").find('[name=full-name]').focus();
 				return false;
 			}

 			var phone = jQuery("#course-form").find('[name=phone]').val();
 			if(phone.length < 10){
 				err++;
 				jQuery("#course-form").find('[name=phone]').focus();
 				return false;
 			}

 			var email = jQuery("#course-form").find('[name=email]').val();

 			var description = jQuery("#course-form").find('[name=description]').val();

 			if(err==0){
 				jQuery("#course-form").find('.loading').show();

 				jQuery.ajax({
                    url: wpshare247_ajax_url,
                    type: 'POST',
                    data:  {
                                action: "wpshare247_course_register",
                                name : name,
                                phone : phone,
                                email : email,
                                description : description

                            },
                    dataType: 'json',
                    success: function(data, textStatus, jqXHR){ 

                        var post_id = data.myid;
                        if(post_id >0){
                        	jQuery("#course-form").find('.reset-data').val('');
                        	jQuery("#course-form").find('#notify').html('Đã đăng ký thành công, vui lòng chờ liên hệ');
                        }
                        
                        jQuery("#course-form").find('.loading').hide();

                        //console.log(post_id);
                    },
                    error: function(jqXHR, textStatus, errorThrown){
                    
                    }          
                });
 			}

 			return false;
 		});
 	});
 </script>

<?php get_footer();

 

Shares
ChatGPT ChatGPT
Share
Share
Pin
Về WPShare247

Học wordpress cho người mới bắt đầu.

WPShare247 là blog chia sẻ các kiến thức về WordPress, sửa chữa lỗi wordpress, giới thiệu theme và plugin wordpress mới nhất. Đào tạo khóa học thiết kế Website bằng WordPress.

Menu
  • Trang chủ
  • Giới thiệu
  • Blog
  • Plugin WordPress
  • Liên hệ
Chủ đề wordpress
  • Sửa lỗi WordPress
  • Plugin WordPress
  • Theme wordpress
  • Seo WordPress
  • WooCommerce

Copyright © 2025 WPSHARE247. All Rights Reserved.
Một sản phẩm của WEB 366

  • Thiết kế web bởi Website366.com
  • Thiết kế website chuyên nghiệp TBAY.VN
  • Thiết kế website bán hàng