Skip to content

Instantly share code, notes, and snippets.

@csalzano
Created February 27, 2019 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csalzano/f9e5490ff070b1b6a3e1b48fd3c0aa3f to your computer and use it in GitHub Desktop.
Save csalzano/f9e5490ff070b1b6a3e1b48fd3c0aa3f to your computer and use it in GitHub Desktop.
JavaScript that edits a term meta value in a WordPress custom taxonomy
//edit a term meta value in our Transmission custom taxonomy
let terms = new wp.api.collections.Tranmission();
terms.fetch().done( function( t ){
let target = t.find( x => 'automatic' === x.slug );
if( ! target ) { return; }
jQuery.ajax({
method: 'POST',
url: myplugin.rest_base_url + 'wp/v2/transmission/' + target.id,
//save the number 6 in a term meta key 'speeds'
data: { meta: { 'speeds': 6 } },
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', myplugin.nonce ); //must be wp_create_nonce( 'wp_rest' ), see paragraph 4 https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
},
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment