-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin-ajax-demo.php
145 lines (113 loc) · 3.01 KB
/
admin-ajax-demo.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Admin Ajax
*
* @package admin-ajax
* @author Hit Bhalodia
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: Admin Ajax
* Plugin URI: https://github.com/hbhalodia/admin-ajax-demo
* Description: This is the Example plugin to show the admin-ajax demo.
* Version: 1.0.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Hit Bhalodia
* Author URI: https://hitbhalodia.wordpress.com/
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: admin-ajax-demo
*/
/**
* This function is used to add the submenu page inside the settings menu page.
*
* @return void
*/
function admin_ajax_settings_page() {
global $admin_ajax;
$admin_ajax = add_options_page(
__( 'Admin Ajax Demo', 'admin-ajax-demo' ),
__( 'Admin Ajax', 'admin-ajax-demo' ),
'manage_options',
__( 'admin_ajax', 'admin-ajax-demo' ),
'admin_ajax_settings_html',
);
}
add_action( 'admin_menu', 'admin_ajax_settings_page' );
/**
* This function is used to create the UI part of the custom settings page of the admin-ajax-plugin.
*
* @return void
*/
function admin_ajax_settings_html() {
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form id="admin_ajax_form" action="" method="POST">
<div>
<?php
submit_button( __( 'Get Posts', 'admin-ajax' ) );
?>
</div>
</form>
<div id="response">
</div>
</div>
<?php
}
/**
* Function is used to load the javascript file.
*
* @param string $hook - The page at which it loads.
*
* @return void
*/
function admin_ajax_load_scripts( $hook ) {
global $admin_ajax;
if ( $hook !== $admin_ajax ) {
return;
}
wp_enqueue_script( 'admin-ajax-demo', plugin_dir_url( __FILE__ ) . 'js/admin-ajax-js.js', array( 'jquery' ), '1.0', true );
wp_localize_script(
'admin-ajax-demo',
'admin_ajax_var',
array(
'ajax_action' => 'admin_ajax_action',
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'ajax_nonce' => wp_create_nonce( 'admin-ajax-demo-nonce' ),
)
);
}
add_action( 'admin_enqueue_scripts', 'admin_ajax_load_scripts' );
/**
* This function is used to get the results and return the response.
*
* @return void
*/
function admin_ajax_action_result() {
if ( ! ( isset( $_POST['add_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['add_nonce'] ) ), 'admin-ajax-demo-nonce' ) ) ) {
die( 'Permission denied!!!' );
}
$args_get_posts = array(
'post_type' => 'post',
'post_status' => 'publish',
'post_per_page' => 5,
'fields' => 'ids',
);
$result = new WP_Query( $args_get_posts );
$result = $result->posts;
?>
<ol>
<?php
foreach ( $result as $post_id ) {
?>
<h3><li><?php printf( '<a href="%s">%s</a>', esc_url( get_permalink( $post_id ) ), esc_html( get_the_title( $post_id ) ) ); ?></li><h3>
<?php
}
?>
</ol>
<?php
die();
}
add_action( 'wp_ajax_admin_ajax_action', 'admin_ajax_action_result' );