Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

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 rniswonger/d53bd1978cb6de78777825c68bbb6a4a to your computer and use it in GitHub Desktop.
Save rniswonger/d53bd1978cb6de78777825c68bbb6a4a to your computer and use it in GitHub Desktop.
WordPress: Export into theme all "Custom Post Type UI" post types and taxonomies
/**
* Saves post type and taxonomy data to JSON files in the theme directory.
* @param array $data Array of post type data that was just saved.
* Code originally provided by the author of the plugin
*/
function cptui_local_json( $data = array() ) {
$theme_dir = get_stylesheet_directory();
// Create our directory if it doesn't exist
if ( ! is_dir( $theme_dir .= '/cptui-json' ) ) {
mkdir( $theme_dir );
}
if ( array_key_exists( 'cpt_custom_post_type', $data ) ) {
// Fetch all of our post types and encode into JSON.
$cptui_post_types = get_option( 'cptui_post_types', array() );
$content = json_encode( $cptui_post_types );
// Save the encoded JSON to a primary file holding all of them.
file_put_contents( get_stylesheet_directory() . '/cptui-json/' . 'post_type_data.json', $content );
}
if ( array_key_exists( 'cpt_custom_tax', $data ) ) {
// Fetch all of our taxonomies and encode into JSON.
$cptui_taxonomies = get_option( 'cptui_taxonomies', array() );
$content = json_encode( $cptui_taxonomies );
// Save the encoded JSON to a primary file holding all of them.
file_put_contents( get_stylesheet_directory() . '/cptui-json/' . 'taxonomy_data.json', $content );
}
}
add_action( 'cptui_after_update_post_type', __NAMESPACE__ . '\\cptui_local_json' );
add_action( 'cptui_after_update_taxonomy', __NAMESPACE__ . '\\cptui_local_json' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment