Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function aj_ajax_demo_shortcode() {
- return '<h4>Shortcode</h4>';
- }
- add_shortcode( 'ajax_demo', 'aj_ajax_demo_shortcode' );
- add_action( 'wp_ajax_nopriv_aj_ajax_demo', 'aj_ajax_demo_process' );
- add_action( 'wp_ajax_aj_ajax_demo', 'aj_ajax_demo_process' );
- function aj_ajax_demo_process() {
- wp_send_json((object) array('msg' => 'hello world'));
- }
- add_action( 'wp_enqueue_scripts', 'aj_enqueue_scripts' );
- function aj_enqueue_scripts() {
- wp_enqueue_script(
- 'aj-demo',
- plugin_dir_url( __FILE__ ) . 'aj-demo-ajax-code.js'
- );
- wp_localize_script(
- 'aj-demo',
- 'aj_ajax_demo',
- array(
- 'ajax_url' => admin_url( 'admin-ajax.php' ),
- 'aj_demo_nonce' => wp_create_nonce('aj-demo-nonce')
- )
- );
- }
- fetch(aj_ajax_demo.ajax_url, {
- method: 'POST',
- data: {
- action : 'aj_ajax_demo',
- nonce : aj_ajax_demo.aj_demo_nonce,
- }
- }).then(response => {
- if (response.ok) {
- response.json().then(response => {
- console.log(response);
- });
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement