Skip to content

Instantly share code, notes, and snippets.

@Idealien
Last active December 11, 2017 08:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Idealien/d51823e440f15a57d65971028da319ca to your computer and use it in GitHub Desktop.
Save Idealien/d51823e440f15a57d65971028da319ca to your computer and use it in GitHub Desktop.
ACF + Rest API = Powerful!
<?php
//REST API - Enable more fields to be searched via Rest API
add_filter( 'rest_query_vars', function ( $valid_vars ) {
$update_vars = $valid_vars;
//Vehicle Meta Data
$update_vars = array_merge( $update_vars, array( 'make', 'meta_query' ) );
$update_vars = array_merge( $update_vars, array( 'model', 'meta_query' ) );
return $update_vars;
} );
<?php //REST API - Vehicle - Add make and model as a filterable parameters
add_filter( 'rest_vehicle_query', function( $args, $request ) {
$make = $request->get_param( 'make' );
if ( ! empty( $make ) ) {
$args['meta_query'][] = array(
'key' => 'vehicle_make',
'value' => $make,
'compare' => '=',
);
}
$model = $request->get_param( 'model' );
if ( ! empty( $model ) ) {
$args['meta_query'][] = array(
'key' => 'vehicle_model',
'value' => $model,
'compare' => '=',
);
}
return $args;
}, 10, 2 );
<?php
//Both call admin and front-end call the same JS file with same nonce
//ACF - Vehicle - Enqueue scripts for REST dynamic management of fields on front-end
add_action( "wp_enqueue_scripts", "enqueue_vehicle_rest" );
function enqueue_vehicle_rest() {
if( is_singular( 'vehicle' )):
wp_enqueue_script( 'vehicle-rest-edit', js/vehicle_rest_edit.js', array( 'jquery', 'underscore', 'acf-input') );
wp_localize_script( 'vehicle-rest-edit', 'rb_vars', array(
'rb_nonce' => wp_create_nonce( 'wp_rest' ),
));
endif;
}
//ACF - Vehicle - Enqueue scripts for REST dynamic management of fields on admin interface
add_action( 'admin_enqueue_scripts', 'enqueue_admin_vehicle_rest' );
function enqueue_admin_vehicle_rest( $hook ) {
$type = get_post_type(); // Check current post type
$types = array( 'vehicle' ); // Allowed post types
if( !in_array( $type, $types ) )
return;
wp_enqueue_script( 'vehicle-rest-edit', js/vehicle_rest_edit.js', array( 'jquery', 'underscore', 'acf-input') );
wp_localize_script( 'vehicle-rest-edit', 'rb_vars', array(
'rb_nonce' => wp_create_nonce( 'wp_rest' ),
)
);
}
(function($) {
//Customize the filter process for select fields where dynamic REST calls are required from one or more fields
acf.add_filter('select2_args', function( args, $select, settings ){
//Vehicle select
if( settings['key'] == "field_577bee5ca4141") {
if (args['ajax']) {
args['ajax'] = {
url: "https://autosearch.dev/wp-json/wp/v2/server",
dataType: 'json',
type: 'get',
cache: false,
beforeSend: function (xhr) {
xhr.setRequestHeader('X-WP-NONCE', rb_vars.rb_nonce);
},
data: function () {
//List is based on the vehicles selected make and model
var data = acf.prepare_for_ajax({
make: $("#vehicle_make input").val(),
model: $("#vehicle_model input").val()
});
return data;
},
//Parse WP REST API data out into post ID and slug for display in select
results: function (data) {
if (!data) return [];
var output = [];
$.each(data, function (i) {
output[i] = {
id: data[i].id,
text: data[i].slug
};
});
return {
results: output
};
}
};
}
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment