Skip to content

Instantly share code, notes, and snippets.

@freddielore
Last active March 14, 2022 02:59
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 freddielore/9d53b956c73cbefb36b85e840628cd0a to your computer and use it in GitHub Desktop.
Save freddielore/9d53b956c73cbefb36b85e840628cd0a to your computer and use it in GitHub Desktop.
[Export Tags and Categories] Adds "tags" and "categories" column to the export CSV file for Smart SEO CSV Import/Export tool #smarseo #php
<?php
// add this to your theme's functions.php or ask your developer to append the following lines
add_filter( 'smart_seo_export_fields', 'child_theme_export_tags_categories', 8, 1 );
function child_theme_export_tags_categories( $fields ){
$fields[] = array( 'categories', 'child_theme_callback_categories' );
$fields[] = array( 'tags', 'child_theme_callback_tags' );
return $fields;
}
function child_theme_callback_categories( $post ){
if( $post->post_type == 'post' ){
return strip_tags( get_the_category_list( ', ', '', $post->ID ) );
}
return '';
}
function child_theme_callback_tags( $post ){
if( $post->post_type == 'post' ){
return strip_tags( get_the_tag_list( '', ', ', '', $post->ID ) );
}
return '';
}
// to process `categories` and `tags` columns during CSV import, make sure you grab the category and tag id
add_action( 'smart_seo_import_update', 'demo_update_categories_tags', 10, 3 );
function demo_update_categories_tags($post_id, $data, $headings){
// check if `categories` column is present on CSV. Value should be integer
if( isset( $data['categories'] ) && !empty($data['categories']) ){
wp_set_object_terms($post_id, $data['categories'], 'category');
}
// check if `tags` column is present on CSV. Value should be integer
if( isset( $data['tags'] ) && !empty($data['tags']) ){
wp_set_object_terms($post_id, $data['tags'], 'post_tag');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment