Created
February 27, 2019 15:08
-
-
Save csalzano/f9e5490ff070b1b6a3e1b48fd3c0aa3f to your computer and use it in GitHub Desktop.
JavaScript that edits a term meta value in a WordPress custom taxonomy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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