// Use the wp_enqueue_scripts function for registering and enqueueing scripts on the front end.
add_action( 'wp_enqueue_scripts', 'register_and_enqueue_a_script' );
function register_and_enqueue_a_script() {
// Register a script with a handle of `my-script`
// + that lives inside the theme folder,
// + which has a dependency on jQuery,
// + where the UNIX timestamp of the last file change gets used as version number
// to prevent hardcore caching in browsers - helps with updates and during dev
// + which gets loaded in the footer
wp_register_script(
'my-script',
get_template_directory_uri().'/js/functions.js',
array( 'jquery' ),
filemtime( get_template_directory().'/js/functions.js',
true
);
// Enqueue the script.
wp_enqueue_script( 'my-script' );
}
add_action( 'wp_enqueue_scripts', 'register_localize_and_enqueue_a_script' );
function register_localize_and_enqueue_a_script() {
wp_register_script(
'my-script',
get_template_directory_uri().'/js/functions.js',
array( 'jquery' ),
filemtime( get_template_directory().'/js/functions.js' ),
true
);
wp_localize_script(
'my-script',
'scriptData',
// This is the data, which gets sent in localized data to the script.
array(
'alertText' => 'Are you sure you want to do this?',
)
);
wp_enqueue_script( 'my-script' );
}
// Triggered for users that are logged in.
add_action( 'wp_ajax_create_new_post', 'wp_ajax_create_new_post_handler' );
// Triggered for users that are not logged in.
add_action( 'wp_ajax_nopriv_create_new_post', 'wp_ajax_create_new_post_handler' );
function wp_ajax_create_new_post_handler() {
// This is unfiltered, not validated and non-sanitized data.
// Prepare everything and trust no input
$data = $_POST['data'];
// Do things here.
// For example: Insert or update a post
$post_id = wp_insert_post( array(
'post_title' => $data['title'],
) );
// If everything worked out, pass in any data required for your JS callback.
// In this example, wp_insert_post() returned the ID of the newly created post
// This adds an `exit`/`die` by itself, so no need to call it.
if ( ! is_wp_error( $post_id ) ) {
wp_send_json_success( array(
'post_id' => $post_id,
) );
}
// If something went wrong, the last part will be bypassed and this part can execute:
wp_send_json_error( array(
'post_id' => $post_id,
) );
}
add_action( 'wp_enqueue_scripts', 'register_localize_and_enqueue_a_script' );
function register_localize_and_enqueue_a_script() {
wp_register_script(
'my-script',
get_template_directory_uri().'/js/functions.js',
array( 'jquery' ),
filemtime( get_template_directory().'/js/functions.js' ),
true
);
// Send in localized data to the script.
wp_localize_script(
'my-script',
'scriptData',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
)
);
wp_enqueue_script( 'my-script' );
}
そしてJavaScriptは以下のようになります:
( function( $, plugin ) {
$( document ).ready( function() {
$.post(
// Localized variable, see example below.
plugin.ajax_url,
{
// The action name specified here triggers
// the corresponding wp_ajax_* and wp_ajax_nopriv_* hooks server-side.
action : 'create_new_post',
// Wrap up any data required server-side in an object.
data : {
title : 'Hello World'
}
},
function( response ) {
// wp_send_json_success() sets the success property to true.
if ( response.success ) {
// Any data that passed to wp_send_json_success() is available in the data property
alert( 'A post was created with an ID of ' + response.data.post_id );
// wp_send_json_error() sets the success property to false.
} else {
alert( 'There was a problem creating a new post.' );
}
}
);
} );
} )( jQuery, scriptData || {} );
( function( $, plugin ) {
"use strict";
$.when(
$.ajax( {
url : pluginURl,
data : { /* ... */ }
} )
.done( function( data ) {
// 2nd call finished
} )
.fail( function( reason ) {
console.info( reason );
} );
)
// Again, you could leverage .done() as well. See jQuery docs.
.then(
// Success
function( response ) {
// Has been successful
// In case of more then one request, both have to be successful
},
// Fail
function( resons ) {
// Has thrown an error
// in case of multiple errors, it throws the first one
},
);
//.then( /* and so on */ );
} )( jQuery, scriptData || {} );