Skip to content

Instantly share code, notes, and snippets.

@ericmann
Last active January 3, 2016 09:09
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 ericmann/e60ba7868d9ec16020c0 to your computer and use it in GitHub Desktop.
Save ericmann/e60ba7868d9ec16020c0 to your computer and use it in GitHub Desktop.
Proof-of-concept for a shortcode taxonomy.
<?php
function eam_register_taxonomy() {
$labels = array(
'name' => _x( 'Shortcodes', 'taxonomy general name' ),
'singular_name' => _x( 'Shortcode', 'taxonomy singular name' ),
'search_items' => __( 'Search Shortcodes' ),
'all_items' => __( 'All Shortcodes' ),
'parent_item' => __( 'Parent Shortcode' ),
'parent_item_colon' => __( 'Parent Shortcode:' ),
'edit_item' => __( 'Edit Shortcode' ),
'update_item' => __( 'Update Shortcode' ),
'add_new_item' => __( 'Add New Shortcode' ),
'new_item_name' => __( 'New Shortcode Name' ),
'menu_name' => __( 'Shortcode' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => false,
'show_admin_column' => false,
'query_var' => false,
'rewrite' => false,
);
register_taxonomy( 'eam_shortcode', array( 'post' ), $args );
}
add_action( 'init', 'eam_register_taxonomy' );
function eam_save_shortcodes( $post_id, $post ) {
global $shortcode_tags;
if ( 'post' !== $post->post_type ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
$shortcodes = array_keys( $shortcodes_tags );
$to_set = array();
$to_remove = array();
foreach( $shortcodes as $shortcode ) {
if ( has_shortcode( $post->post_content, $shortcode ) ) {
$to_set[] = $shortcode;
} else {
$to_remove[] = $shortcode;
}
}
wp_add_object_terms( $post_id, $to_set, 'eam_shortcode' );
wp_remove_object_terms( $post_id, $to_remove, 'eam_shortcode' );
}
add_action( 'save_post', 'eam_save_shortcodes', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment