This code snippet is used to export WordPress categories and tags into a CSV file and adds a page in the WordPress admin area to run this export function. It also enqueues a JavaScript file that could add additional functionality to the export page.
PHP
function export_tags_and_categories() {
$terms = get_terms( array( 'category', 'post_tag' ), array( 'hide_empty' => false ) );
$output = fopen( 'php://output', 'w' );
fputcsv( $output, array( 'Term ID', 'Name', 'Slug', 'Description', 'Taxonomy' ) );
foreach ( $terms as $term ) {
fputcsv( $output, array(
$term->term_id,
$term->name,
$term->slug,
$term->description,
$term->taxonomy
) );
}
fclose( $output );
}
function add_export_tags_and_categories_page() {
add_menu_page(
'Export Tags and Categories',
'Export Tags and Categories',
'manage_options',
'export-tags-and-categories',
'export_tags_and_categories'
);
}
add_action( 'admin_menu', 'add_export_tags_and_categories_page' );
function add_export_tags_and_categories_script() {
wp_enqueue_script(
'export-tags-and-categories-script',
plugins_url( 'export-tags-and-categories.js', __FILE__ ),
array( 'jquery' ),
'1.0',
true
);
}
add_action( 'admin_enqueue_scripts', 'add_export_tags_and_categories_script' );