I'm creating a Wordpress site in which I wanted to use a custom taxonomy in my theme options, which I had registered using the 'init' hook. But the problem was that the 'init' hooks gets fired after the theme options (I'm using the Redux Framework plugin for the theme options). So when I used the get_categories() function inside my theme options, and tried to get the custom taxonomy, I was getting an 'invalid_category' error.
So I decided to just register the taxonomy without any hooks and simply calling the register_taxonomy() function in the functions.php file and it worked.
I just want to know if this is 'safe', as in, what would be the consequences if I simply call register_taxonomy function without using any hooks.
Here is the code that doesn't work, while using the init hook:
function my_function(){
register_taxonomy( 'portfolio_category', 'portfolio', array(
'labels' => array(
'name' => _x( 'Portfolio Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Portfolio Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Portfolio Categories' ),
'all_items' => __( 'All Portfolio Categories' ),
'parent_item' => __( 'Parent Portfolio Category' ),
'parent_item_colon' => __( 'Parent Portfolio Category:' ),
'edit_item' => __( 'Edit Portfolio Category' ),
'update_item' => __( 'Update Portfolio Category' ),
'add_new_item' => __( 'Add New Portfolio Category' ),
'new_item_name' => __( 'New Portfolio Category' ),
'menu_name' => __( 'Portfolio Categories' ),
),
'hierarchical' => true,
) );
}
add_action('init', 'my_function');
$portfolio_categories = get_categories(array('taxonomy' => 'portfolio_category', 'hide_empty'=> 0));
echo "<pre>", print_r($portfolio_categories), "</pre>"; // This will return an error of 'invalid_taxonomy'
And here is one that works, (without using any hooks):
register_taxonomy( 'portfolio_category', 'portfolio', array(
'labels' => array(
'name' => _x( 'Portfolio Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Portfolio Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Portfolio Categories' ),
'all_items' => __( 'All Portfolio Categories' ),
'parent_item' => __( 'Parent Portfolio Category' ),
'parent_item_colon' => __( 'Parent Portfolio Category:' ),
'edit_item' => __( 'Edit Portfolio Category' ),
'update_item' => __( 'Update Portfolio Category' ),
'add_new_item' => __( 'Add New Portfolio Category' ),
'new_item_name' => __( 'New Portfolio Category' ),
'menu_name' => __( 'Portfolio Categories' ),
),
'hierarchical' => true,
) );
$portfolio_categories = get_categories(array('taxonomy' => 'portfolio_category', 'hide_empty'=> 0));
echo "<pre>", print_r($portfolio_categories), "</pre>";
Also the first code works, if I put it in a page template, so I know I'm doing it correctly. Please let me know about this
Thanks.
init
hook and just tested withget_categories()
and was able to use it on my taxonomy without problems. Also, are you trying to runget_categories()
in yourfunctions.php
file or are you registering your taxonomy outside yourfunctions.php
file? Register your taxonomy in yourfunctions.php
and useget_categories()
in a template file.