Last active
August 10, 2019 21:44
-
-
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.
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
// 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