-
Notifications
You must be signed in to change notification settings - Fork 2
/
webdev.php
50 lines (42 loc) · 1.35 KB
/
webdev.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/*
Plugin Name: WebDev AJAX
Plugin URI: http://#
Description: Quick AJAX Tutorial
Version: 0.1a
Author: Anonymous
Author URI: http://#
*/
class WDS_Shortcode_Ajax_Handler {
function hooks(){
add_action( 'wp_ajax_webdev', array( $this, 'handle_ajax' ) );
add_action( 'wp_ajax_nopriv_webdev', array( $this, 'handle_ajax' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_shortcode( 'webdev_tut', array( $this, 'shortcode_output' ) );
}
function handle_ajax(){
if( ! wp_verify_nonce( $_REQUEST['nonce'], 'webdev' ) ){
wp_send_json_error();
}
wp_send_json_success( array(
'script_response' => 'AJAX Request Recieved',
'nonce' => wp_create_nonce( 'webdev' ),
) );
}
function enqueue_scripts(){
wp_enqueue_script( 'webdev_js', plugins_url( '/js/webdev.js', __FILE__ ), array( 'jquery' ), '1.0', true );
wp_localize_script( 'webdev_js', 'webdev', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'webdev' ),
) );
}
function shortcode_output( $atts ){
$output = '<form action="" method="POST" class="ajax_tut_form">';
$output .= ' <input type="text" name="ajax_tut">';
$output .= ' <input type="submit" value="submit">';
$output .= '</form>';
return $output;
}
}
$web_dev_plugin = new WDS_Shortcode_Ajax_Handler();
$web_dev_plugin->hooks();