0

I want to create a custom taxonomy using a shortcode. I don't want to use any plugin. I use this code in my plugin:

add_shortcode('taxnomy','project_taxonomy_init');

add_filter( 'widget_text', 'do_shortcode');

function project_taxonomy_init() {
    //create a new taxonomy
    register_taxonomy(
        'skills',
        'projects',
        array(
            'labels' => array(
                //'supports' => array( 
                //      'title', 'editor', 'comments', 'excerpt', 'custom-fields', 'thumbnail' 
                //),
                'name' => __( 'Skills' ),
                'singular_name' => __( 'skills' ),
                'edit_item' => __( 'Edit Skill' ),
                'update_item' => __('Update Skill' ),
                'new_item_name'=>__('New Skill Name'),
                'all_items'=>__('All Skill Name'),
                'add_new_item'=>__('Add New Skill'),
                'search_items'=>__('Seacrh Skill Name'),
                'popular_items' => __( 'Popular Skill' ),
                'separate_items_with_comments' => __( 'Separate Items With Comments'   ),
                'add_or_remove_items'=>__('Add Or Remove Items'),
                'choose_from_most_used'=>__('Choose From Most Used'),
            ),
            'public'          => true,
            'hierarchical'    => true,
            'rewrite'         => array(
                'slug'          => 'skills',
                'with_front'    => true
            ),
        )
    );
}
  • Not really clear what you are asking. Add more details. – Sisir Sep 5 '15 at 7:42
  • no you please check again i update the code with shortcode – Hafiz Usman aftab Sep 5 '15 at 7:55
  • 1
    I think what @Sisir means is that your question doesn't make sense from a logistical standpoint. The relationship between shortcodes and taxonomies isn't setup to work anything like this, based on the information you have provided this is not possible, but what you actually want doesn't seem to be represented in your question. – totels Sep 5 '15 at 8:57
4

It looks like you want to register custom taxonomies within shortcodes, but here's an important notice from the Codex on the register_taxonomy function:

Use the init action to call this function. Calling it outside of an action can lead to troubles. See #15568 for details.

Here's an example of the action run sequence from my Twenty Fifteen home page:

muplugins_loaded                     <-- FIRST HOOK AVAILABLE
registered_taxonomy
registered_taxonomy
registered_taxonomy
registered_taxonomy
registered_taxonomy
registered_post_type
registered_post_type
registered_post_type
registered_post_type
registered_post_type
plugins_loaded
sanitize_comment_cookies
setup_theme
unload_textdomain
load_textdomain
after_setup_theme
load_textdomain
load_textdomain
auth_cookie_malformed
auth_cookie_valid
set_current_user
init                              <-- RECOMMENDED HOOK TO REGISTER TAXONOMIES
registered_post_type
registered_post_type
registered_post_type
registered_post_type
registered_post_type
registered_taxonomy
registered_taxonomy
registered_taxonomy

... continue

registered_taxonomy
registered_taxonomy
widgets_init
register_sidebar
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_loaded
parse_tax_query
parse_tax_query
posts_selection
parse_tax_query
parse_tax_query

... continue

posts_selection
template_redirect
admin_bar_init
add_admin_bar_menus
get_header
wp_head
wp_enqueue_scripts
wp_print_styles
wp_print_scripts
get_sidebar
dynamic_sidebar_before
dynamic_sidebar
                                  <-- YOU ARE TRYING TO REGISTER TAXONOMIES HERE 
dynamic_sidebar_after

... and so on

So we can see that trying to register taxonomies with shortcodes inside widgets, happens deep inside the theme code, long way from the recommended init hook.

PS: I would in general recommend using prefixed shortcode names, instead of general ones like taxonomy or taxnomy, to avoid possible name collision.

Workaround

There might be a way around this, by peeking into the database within a init hook callback and search for the shortcode instances in the widget_text option. If it exists then we could register the custom taxonomy. We also don't want to register the same taxonomy multiple times. We would also have to check if the widget is active or inactive. I don't know if this is a good idea in general.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.