Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Last active January 30, 2016 19:10
Show Gist options
  • Select an option

  • Save Ciantic/3e1c463d8ee7bc006033 to your computer and use it in GitHub Desktop.

Select an option

Save Ciantic/3e1c463d8ee7bc006033 to your computer and use it in GitHub Desktop.
WordPress - Export taxonomies or categories
<?php
function just_export_damn_taxonomies($taxs = array(), $include_cats = false, $include_tags = false) {
$WXR_VERSION = '1.2';
// functions from 'wp-admin/includes/export.php'
function wxr_cdata($str) {
if (seems_utf8 ( $str ) == false)
$str = utf8_encode ( $str );
// $str = ent2ncr(esc_html($str));
$str = '<![CDATA[' . str_replace ( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
return $str;
}
function wxr_site_url() {
// ms: the base url
if (is_multisite ())
return network_home_url ();
// wp: the blog url
else
return get_bloginfo_rss ( 'url' );
}
function wxr_cat_name($category) {
if (empty ( $category->name ))
return;
echo '<wp:cat_name>' . wxr_cdata ( $category->name ) . '</wp:cat_name>';
}
function wxr_category_description($category) {
if (empty ( $category->description ))
return;
echo '<wp:category_description>' . wxr_cdata ( $category->description ) . '</wp:category_description>';
}
function wxr_tag_name($tag) {
if (empty ( $tag->name ))
return;
echo '<wp:tag_name>' . wxr_cdata ( $tag->name ) . '</wp:tag_name>';
}
function wxr_tag_description($tag) {
if (empty ( $tag->description ))
return;
echo '<wp:tag_description>' . wxr_cdata ( $tag->description ) . '</wp:tag_description>';
}
function wxr_term_name($term) {
if (empty ( $term->name ))
return;
echo '<wp:term_name>' . wxr_cdata ( $term->name ) . '</wp:term_name>';
}
function wxr_term_description($term) {
if (empty ( $term->description ))
return;
echo '<wp:term_description>' . wxr_cdata ( $term->description ) . '</wp:term_description>';
}
if ( !empty($taxs) ) {
$custom_terms = (array) get_terms( $taxs, array( 'get' => 'all' ) );
}
if ($include_tags) {
$tags = (array) get_tags(array('get' => 'all'));
}
if ($include_cats) {
$categories = (array) get_categories(array('get' => 'all'));
}
$sitename = sanitize_key( get_bloginfo( 'name' ) );
if (!empty($sitename))
$sitename .= '.';
$filename = $sitename . 'wordpress.' . date( 'Y-m-d' ) . '.xml';
header( 'Content-Description: File Transfer' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
if(!empty($categories)) {
// put categories in order with no child going before its parent
while ($cat = array_shift($categories)) {
if ($cat->parent == 0 || isset($cats[$cat->parent]))
$cats[$cat->term_id] = $cat;
else
$categories[] = $cat;
}
}
if (!empty($custom_terms)) {
// put terms in order with no child going before its parent
while ($t = array_shift( $custom_terms ) ) {
if ($t->parent == 0 || isset($terms[$t->parent]))
$terms[$t->term_id] = $t;
else
$custom_terms[] = $t;
}
}
echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . "\" ?>\n";
?>
<?php the_generator( 'export' ); ?>
<rss version="2.0"
xmlns:excerpt="http://wordpress.org/export/<?php echo $WXR_VERSION; ?>/excerpt/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:wp="http://wordpress.org/export/<?php echo $WXR_VERSION; ?>/"
>
<channel>
<title><?php bloginfo_rss( 'name' ); ?></title>
<link><?php bloginfo_rss( 'url' ); ?></link>
<description><?php bloginfo_rss( 'description' ); ?></description>
<pubDate><?php echo date( 'D, d M Y H:i:s +0000' ); ?></pubDate>
<language><?php bloginfo_rss( 'language' ); ?></language>
<wp:wxr_version><?php echo $WXR_VERSION; ?></wp:wxr_version>
<wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
<wp:base_blog_url><?php bloginfo_rss( 'url' ); ?></wp:base_blog_url>
<?php
if( !empty($cats) ):
foreach ( $cats as $c ) : ?>
<wp:category><wp:term_id><?php echo $c->term_id ?></wp:term_id><wp:category_nicename><?php echo $c->slug; ?></wp:category_nicename><wp:category_parent><?php echo $c->parent ? $cats[$c->parent]->slug : ''; ?></wp:category_parent><?php wxr_cat_name( $c ); ?><?php wxr_category_description( $c ); ?></wp:category>
<?php endforeach;
endif;
?>
<?php
if( !empty($tags) ):
foreach ( $tags as $t ) : ?>
<wp:tag><wp:term_id><?php echo $t->term_id ?></wp:term_id><wp:tag_slug><?php echo $t->slug; ?></wp:tag_slug><?php wxr_tag_name( $t ); ?><?php wxr_tag_description( $t ); ?></wp:tag>
<?php endforeach;
endif;
?>
<?php
if( !empty($terms)):
foreach ( $terms as $t ) : ?>
<wp:term><wp:term_id><?php echo $t->term_id ?></wp:term_id><wp:term_taxonomy><?php echo $t->taxonomy; ?></wp:term_taxonomy><wp:term_slug><?php echo $t->slug; ?></wp:term_slug><wp:term_parent><?php echo $t->parent ? $terms[$t->parent]->slug : ''; ?></wp:term_parent><?php wxr_term_name( $t ); ?><?php wxr_term_description( $t ); ?></wp:term>
<?php endforeach;
endif;
?>
<?php do_action( 'rss2_head' ); ?>
</channel>
</rss><?php
die();
}
if (current_user_can("manage_options") && is_admin() && isset($_GET['export_taxes'])) {
just_export_damn_taxonomies(array("mytaxonomy"));
}
// visit wp-admin/?export_taxes be happy
@Ciantic
Copy link
Author

Ciantic commented Jan 30, 2016

I hereby place this to public domain.

Bits and pieces may be GPL'd, because they are from wordpress and some other plugin which included useless stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment