Skip to content

Instantly share code, notes, and snippets.

@ericmann ericmann/gist:e60ba7868d9ec16020c0 Secret
Last active Jan 3, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.