Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAndrew Chappell
12,782 PointsOnly allow editors or administrators to select the featured category in Wordpress admin.
I'm looking into the Wordpress filter get_terms_fields which is defined here: http://adambrown.info/p/wp_hooks/hook/get_terms_fields?version=3.9&file=wp-includes/taxonomy.php
// edit categories based on whether user can manage options
add_filter('get_terms_fields', 'only_editors');
function only_editors($selects = array(), $args = array(), $taxonomies = array() ) {
if (current_user_can = 'manage_options' ) {
$selects[] = 'featured';
$args[] = 'featured';
$taxonomies[] = 'featured';
}
}
I know this isn't correct but that is essentially what I want do. I'm very inexperienced with hooks though so any help appreciated.
2 Answers
Aaron Jackson
5,644 PointsIf you are trying to limit things in the back end of WordPress to certain users based on their roles then, an easier way to do that is by using the following plugin http://wordpress.org/plugins/user-role-editor/
Andrew Chappell
12,782 PointsI have found a solution. Credit to the author from this website: http://shailan.com/2598/how-to-exclude-categories-and-tags-from-your-widgets/
Basically the solution is to use the list_terms_exclusions hook and pass in the ID of the category you want to filter.
function shailan_filter_terms( $exclusions, $args ){
if (!current_user_can('manage_options') ) {
// IDs of terms to be excluded
$exclude = "239"; // CHANGE THIS TO IDs OF YOUR TERMS
// Generation of exclusion SQL code
$exterms = wp_parse_id_list( $exclude );
foreach ( $exterms as $exterm ) {
if ( empty($exclusions) )
$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
else
$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
}
// Closing bracket
if ( !empty($exclusions) )
$exclusions .= ')';
// Return our SQL statement
return $exclusions;
}
}
// Finally hook up our filter
add_filter( 'list_terms_exclusions', 'shailan_filter_terms', 10, 2 );
Now if the user is not an administrator they will not be able to select the featured category when editing the post. Very nice! Hopefully this helps someone :)
Andrew Chappell
12,782 PointsAndrew Chappell
12,782 PointsThanks Aaron. I will look into that If i can't find a solution.