sexta-feira, 5 de setembro de 2014

How to Register Custom Taxonomy For WordPress Users [WordPress Tip]

The Custom Taxonomy underline has been introduced given WordPress 2.9. It allows we to emanate tradition groups for Post, Page as good as Custom Post Types.


Say that we are building a book office website, and we have combined a Custom Post Type for posting a Books. By regulating Custom Taxonomy, we can emanate a tradition taxonomy for it, called Genre. Within this Genre taxonomy, we can emanate a series of equipment (which technically is called terms) such as Fiction, Kids, or Biography for organisation a Books.


Unfortunately, during this point, we can’t register Custom Taxonomy to Users; during slightest not in a candid approach as we would register it in a other Post Types. One ideal focus that we could predict from this thought is that we can use it to allot additional user attributes, such as their occupation, profession or organizational position, in place of induction a new set of User Roles. It also opens a probability to query a users formed on a reserved taxonomy terms.


If this thought is something that might advantage your website, take a demeanour during this tip.



Getting Started


First, we will implement a plugin named User Taxonomies to facilitate a job.


Once a plugin is activated. Go to GenerateWP to beget a Taxonomy codes. Put a formula outlay in a functions.php record of your theme. This formula dash next is an example. Though, it has been nude out to make this essay demeanour shorter. You can follow this couple to see a full code.



if ( ! function_exists( 'user_staff_position' ) )
function user_staff_position()
register_taxonomy( 'staff_position', 'post', $args );

add_action( 'init', 'user_staff_position', 0 );


Now, change a Post Type parameter in a following line:



register_taxonomy( 'staff_position', 'post', $args );

…from post to user, like so:



register_taxonomy( 'staff_position', 'user', $args );

Now, go to a WP-Admin, and we should find a new menu combined underneath a Users menu, as seen below.



Assigning a Custom Taxonomy


Navigate to a new menu and emanate a few terms. For this example, we combined dual items: CEO and Managers.



Then go to user modifying shade and allot one intent from a taxonomy to a user.



Query a Users


We are going to arrangement a users in a thesis formed on a given tenure (of a taxonomy). But before going further, let’s emanate a new page template. We are going supplement a codes via a following territory within this new template.


In this sold case, we won’t be means to query a users with get_users or WP_User_Query; when we emanate a new WP_User_Query class, it does not outlay a Custom Taxonomy that is reserved to a users. Justin Tadlock, in his tutorial, shows us how to use a get_objects_in_term function, instead.


This duty outputs a intent ID (which in a box the intent means a user) that are tied with a term. To use it, we need dual parameters: a Term ID and a Taxonomy name. You can mark a Term ID during a Browser URL bar when we revise it as shown below.



Once you’ve found a ID, put it within a function, like so.



$users = get_objects_in_term(3, 'user_position');

You can use var_dump() to arrangement a intent IDs that have been retrieved; In my case, it earnings a users with a ID of 1 and 3.



Using these IDs, we can also retrieve, for example, a user name and avatar.



ul
?php if ( !empty( $users ) ) : ?
?php foreach ( $users as $id ) : ?
li class="user-entry"
figure?php relate get_avatar( get_the_author_meta('email', $id), '40' ); ?/figure
h4 class="user-title"a href="?php relate esc_url( get_author_posts_url( $id ) ); ?"?php the_author_meta( 'display_name', $id ); ?/a/h4
/li
?php endforeach; ?
?php endif; ?
/ul

…and, finally, here is a result.



That’s it. You can openly cgange a above codes to accommodate your requirement.




How to Register Custom Taxonomy For WordPress Users [WordPress Tip]

Nenhum comentário:

Postar um comentário