Skip to content

Instantly share code, notes, and snippets.

@ofyalcin
Last active August 10, 2019 21:44
Show Gist options
  • Save ofyalcin/74d32e7a4dcff1e0a42f43fc588db817 to your computer and use it in GitHub Desktop.
Save ofyalcin/74d32e7a4dcff1e0a42f43fc588db817 to your computer and use it in GitHub Desktop.
Wordpress: How to save custom taxonomy terms in json when a post saved.
// Add this code to bottom line of your active theme functions.php file.
function export_custom_terms_in_json () {
// get all terms of custom_taxonomy
$CustomTaxTerms = get_terms( 'custom_taxonomy', array( 'orderby'=>'count', 'hide_empty' => 0 ) );
if( ! is_wp_error( $CustomTaxTerms ) ) {
// encode term list
$data = json_encode( $CustomTaxTerms );
// define the directory where json file will be saved.
$folder = get_template_directory();
// specify the name of the file to save.
$file_name = 'MyTerms.json';
$fileUrl = $folder . '/' . $file_name;
file_put_contents( $fileUrl, $data );
}
}
add_action( 'save_post', 'export_custom_terms_in_json' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment