30
loading...
This website collects cookies to deliver better user experience
plugin_name()->scripts()->add( 'test', [
'handle' => 'test',
'src' => 'path/to/script/src',
'name' => 'test',
'description' => 'The description',
'deps' => [ 'wp-api-fetch', 'wp-polyfill' ],
'middlewares' => [
'Underpin_Scripts\Factories\Enqueue_Script'
]
] );
wp_register_script( 'test', 'path/to/script/src', ['wp-api-fetch', 'wp-polyfill'] );
add_action( 'wp_enqueue_scripts', function(){
wp_enqueue_script( 'test' );
} );
import
statements inside your Javascript for any @wordpress
package, just like as-if you had installed the NPM package. plugin_name()->scripts()->add( 'test', [
'handle' => 'test',
'src' => 'path/to/script/src',
'name' => 'test',
'description' => 'The description',
'deps' => plugin_name()->dir() . 'build/test.asset.php', // path to dependency file generated by webpack
'middlewares' => [
'Underpin_Scripts\Factories\Enqueue_Script' // Enqueue the script on the front-end
]
] );
// Check to see if the file exists.
$deps_file = plugin_dir_path(__FILE__) . 'path/to/file';
// Set default fallback to dependencies array
$deps = [];
// If the file can be found, use it to set the dependencies array.
if ( file_exists( $deps_file ) ) {
$deps_file = require( $deps_file );
$deps = $file['dependencies'];
}
// Register script
wp_register_script( 'test', 'path/to/script/src', $deps );
// Enqueue the script later-on
add_action( 'wp_enqueue_scripts', function(){
wp_enqueue_script( 'test' );
} );
apiFetch( {
path: '/wp/v2/posts'
} );
then
statement, or async/await
. Then is a little simpler to set up, but I find that async/await is easier to read.apiFetch( {
path: '/wp/v2/posts'
} ).then( ( posts ) => /** do something with the posts **/ );
const posts = await apiFetch( {
path: '/wp/v2/posts'
} );
// Do something with your posts
wp_add_inline_script
, like so:// Add a nonce to all requests
wp_add_inline_script(
'test' // Script handle to add after
'apiFetch.use( apiFetch.createNonceMiddleware( '. wp_create_nonce( 'wp_rest' ) .' ) )', // Javascript to inject after the script. This will add the nonce header to all rest requests on the apiFetch object.
);
// Set the root URL for all requests
wp_add_inline_script(
'test' // Script handle to add after
'apiFetch.use( apiFetch.createRootURLMiddleware( '. get_rest_url() .' ) )', // Javascript to inject after the script. This will add the nonce header to all rest requests on the apiFetch object.
);
wp_localize_script
, and even at that time it was necessary to pass one, or both of these things in every request.add_action('wp_enqueue_scripts', function(){
// Preload the fetch data
$current_page = get_query_var( 'paged' ) ?? 1; // Get the current page, and use page 1 as the default.
$request = new \WP_REST_Request( $type, $endpoint ); // Instantiate a fake REST request.
$request->set_query_params( ['page' => $current_page ); // Set current page
$response = rest_do_request( $request ); // Run the REST request.
$preloaded_data = [
'wp/v2/posts?page=' . $current_page => $response->get_data()
];
wp_add_inline_script(
'test' // Script handle to add after
'apiFetch.use( apiFetch.createPreloadingMiddleware( '. wp_json_encode( $preloaded_data ) .' ) )', // Javascript to inject after the script. This will add the nonce header to all rest requests on the apiFetch object.
);
} );
apiFetch.use( ( options, next ) => {
// Do things, like manipulate the provided options
return next( options );
} );