Hide Wordpress Categories from Users by Role - wordpress

I want to be able to hide certain Wordpress Post Categories from Users dependent on their Role.
I've tried the code here:
Wordpress: Hide specific categories from User Role on the Add New page
I think its deprecated, and would really appreciate some help
add_filter('list_terms_exclusions', 'yoursite_list_terms_exclusions', 10, 2);
function yoursite_list_terms_exclusions( $exclusions, $args ) {
global $pagenow;
if (in_array($pagenow,array('post.php','post-new.php')) && !current_user_can('see_special_cats')) {
$exclusions = " {$exclusions} AND t.slug NOT IN ('slug-one','slug-two')";
}
return $exclusions;
}
With this code nothing happens. I've tried 10+ different plugins and am really getting desperate. Thanks in advance.

add_filter('list_terms_exclusions', 'yoursite_list_terms_exclusions', 10, 2);
function yoursite_list_terms_exclusions( $exclusions, $args ) {
global $pagenow;
if (in_array($pagenow,array('post.php','post-new.php')) &&
!current_user_can('see_special_cats')) {
$exclusions = " {$exclusions} AND t.slug NOT IN ('slug-one','slug-two')";
}
return $exclusions;
}
This code presumes that you've used a plugin like the Members plugin to create a capability called 'see_special_cats' and that you've assigned it to every role that you want to have access to the categories except of course 'Contributors'.
Since you found the plugin you may not need this, but maybe it will help someone else.

add_filter('list_terms_exclusions', 'yoursite_list_terms_exclusions', 10, 2);
function yoursite_list_terms_exclusions( $exclusions, $args ) {
$current_user = wp_get_current_user();
// Change 'see_special_cats' to capability user must have to be able see category or categories
if ( $current_user->has_cap('see_special_cats') ) {
$capCheck = 1;
} else {
$capCheck = 0;
}
global $pagenow;
if (in_array($pagenow,array('post.php','post-new.php')) && !$capCheck) {
// Change category-slug-one and two to desired categories to hide. Additional categories may be added
// by separating with a comma. Delete ", 'category-slug-two'" to only hide one category
$exclusions = " {$exclusions} AND t.slug NOT IN ('category-slug-one', 'category-slug-two')";
}
return $exclusions;
}
This code works without using current_user_can(). Paste this code in your functions.php file. If you want to hide a category from everyone except for the Administrator role, as set up by the default hierarchical structure, change 'see_special_cats' to 'create_users'. Change category-slug-one and category-slug-two to the category slugs that you want hidden. There is no additional plugin required (I'm not sure where 'see_special_cats' comes from).

Related

Hide plugin from admin bar & plugin list

I am trying to hide the following item from the following sections:
Admin bar: ID = wp-admin-bar-nitropack-top-menu
Plugin list: data-slug="nitropack"
I have tried these methods, but can not get it to work. Maybe i have the wrong IDs/Slugs?
Methods: https://divi.space/wordpress-and-divi-code-snippets/hide-any-plugin-from-the-wordpress-dashboard/
Would really appreciate some help, since a customer should not be able to change the settings within this plugin!
Best regards,
Isac
The css way
<style>
a[data-slug="nitropack"] { //hides all a href's with that data slug
display:none;
}
</style>
normally if its an wp admin menu you would do something like this:
//remove admin page item
function edit_admin_menus() {
remove_menu_page("index.php"); //Dashboard
remove_menu_page("itsec"); // wp-admin.php?page=itsec use this "itsec"
}
add_action("admin_menu", "edit_admin_menus");
or you need to remove admin bar item
//remove tool bar item
function remove_toolbar_node($wp_admin_bar) {
// replace 'updraft_admin_node' with your node id "nitropack" something
$wp_admin_bar->remove_node("avia");
$wp_admin_bar->remove_node("updates");
$wp_admin_bar->remove_menu("wp-logo");
$wp_admin_bar->remove_menu("themes");
$wp_admin_bar->remove_menu("widgets");
$wp_admin_bar->remove_menu("dashboard");
//$wp_admin_bar->remove_node("updraft_admin_node");
}
add_action("admin_bar_menu", "remove_toolbar_node", 999);
FYI, since you need to block access to the plugin you'll need to add a redirect based on member role. The customer may know the actual url and can still access the page.
//Admin or Editor role check, if else send to alt url
function block_pages_for_user() {
$blocked_pages = is_page('slug');
$url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if( !current_user_can('administrator') && !current_user_can('editor') && !current_user_can('subscriber') && $blocked_pages ) {
wp_redirect( 'http://www.example.dev/your-page/', 301 );
exit;
}
}
add_action( 'wp', 'block_pages_for_user', 8 );

How to add verified badge in front of author name across wordpress blog

Good Day,
I have been trying to add verification badge to WordPress users but I no success. I tried using administrator to do the testing by checking if user has role administrator and trying to enter code here update_user_meta (display_name) but I wasn't able to add icons to the display name.
I have as well tried creating a custom field in user profile called verify_user (text field) .... In which I entered the value "verified" and saved ... I have been searching for hooks to use but haven't seen one. I'm not sure which hook/filter to use here
Please is there any solution to adding this verification icon to author's display name in which when the word "verified" is entered into the custom field created in user profile or any other approach available (e.g if user has specific role). I don't mind if the little structure I wrote above would be changed.
Thanks
I was able to get a solution which worked exactly as i wanted. For anyone who might have same task to tackle;
function add_verification_bagdge_to_authors($display_name) {
global $authordata;
if (!is_object($authordata))
return $display_name;
$icon_roles = array(
'administrator',
'verified_author',
);
$found_role = false;
foreach ($authordata->roles as $role) {
if (in_array(strtolower($role), $icon_roles)) {
$found_role = true;
break;
}
}
if (!$found_role)
return $display_name;
$result = sprintf('%s <i title="%s" class="fa fa-check-circle"></i>',
$display_name,
__('This is a Verified Author', 'text-domain-here')
);
return $result;
}
add_filter( 'the_author', 'add_verification_bagdge_to_authors' );
The way i was able to tackle this was to create a user role called Verified Author (using add_role() function from Wordpress Codex ), which will be the role assigned to verified author across the website. All Users with Admin Role are automatically Verified while user role has to be switched from either contributor/Author role to Verified Author.
The above code was able to do 98% of the task but when a verified author / administrator comments, their display name doesn't show the verified badge. I was able to use the below code to fix that (combining the code with a shortcode).
function my_comment_author( $author = '' ) {
// Get the comment ID from WP_Query
$comment = get_comment( $comment_ID );
if ( ! empty($comment->comment_author) ) {
if (!empty($comment->user_id)){
$user=get_userdata($comment->user_id);
$author=$user->display_name.' '. do_shortcode('[this_is_verified-sc]'); // This is where i used the shortcode
} else {
$author = $comment->comment_author;
}
} else {
$author = $comment->comment_author;
}
return $author;
}
add_filter('get_comment_author', 'my_comment_author', 10, 1);
Shortcode to return the Verified Badge if user is admin or verified author
function add_verification_bagdge_to_authors_sc($display_name) {
global $authordata;
if (!is_object($authordata))
return $display_name;
$icon_roles = array(
'administrator',
'verified_author',
);
$found_role = false;
foreach ($authordata->roles as $role) {
if (in_array(strtolower($role), $icon_roles)) {
$found_role = true;
break;
}
}
if (!$found_role)
return $display_name;
$result = sprintf('%s <i title="%s" class="fa fa-check-circle"></i>', $display_name, __('This is a Verified Author', 'text-domain-here') );
return $result;
}
add_shortcode( 'this_is_verified-sc', 'add_verification_bagdge_to_authors_sc' );
Please Note: Not all Magazine theme are properly coded / have a hard coded Block and module elements, so the verified badge might not work on their block / module element. I experienced this during the whole website design process, so we had to change up theme and the only modification i had to make to the new theme was to remove the html escape added to their block/module code, so that the icon can be rendered. i.e. in their module/block code they had something like this esc_html( the_author() ) and i removed the esc_html to have just the_author() an it all worked.
Its not really a straight forward solution but that is how i was able to tackle the task without using a plugin. Just add the codes to your functions.php file and that's it. I hope it helps someone.
Thanks to Kero for pointing me in the right direction, providing the code i was able to work with and manipulate

Change Review Display Name on Woo-commerce Store

I want to be able to change the name that shows up on the reviews on a woocommerce platform. I have found some information but I am new to WordPress and no one seems to have the answer. Here is a link that I was able to find something but no installation specifics.
I need to know where and how to change this please. Thanks.
Here is the link that I found.
https://silicondales.com/tutorials/woocommerce-tutorials/woocommerce-change-review-author-display-name-username/
I have added a plugin called Username Changer and it let me change the username of the account but it won't update the review username.
Here are some images as of now (3/6/2017)
Snapshot of how user is configured with edited username:
Ok I have figured it out. You will need to post the code into the functions.php file if you do not have a child theme. It is recommended that you set one up but I haven't yet and I just wanted to get this fixed first. Here is a snapshot of what you need to too.
Add the below block of text to the bottom or your custom-functions.php or functions.php page within Appearance > Editor
add_filter('get_comment_author', 'my_comment_author', 10, 1);
function my_comment_author( $author = '' ) {
// Get the comment ID from WP_Query
$comment = get_comment( $comment_ID );
if (!empty($comment->comment_author) ) {
if($comment->user_id > 0){
$user=get_userdata($comment->user_id);
$author=$user->first_name.' '.substr($user->last_name,0,1).'.'; // this is the actual line you want to change
} else {
$author = __('Anonymous');
}
} else {
$author = $comment->comment_author;
}
return $author;
}
Make sure to update the user info.
Missing Sections from the Original Question

Custom product attribute URL

Maybe someone are able to help me. I want to change product attribute URL from pa_ to something else. (for example http://website.com/?pa_color=black to ?product_color=black.)
Any solution guys?
Best Regards
The functionality that allows you to use a custom base in your url instead of the pa_ in your product attribute are no longer standard with WooCommerce.
You will have to first make sure that you have set your taxonomy permalinks to the bases you would like the product urls to have. If you go into WordPress Dashboard > Settings > Permalink menu you can change the categories, terms and attributes to whatever makes the most sense for your product. By changing these preferences you can use a custom base on your products (be sure not to repeat bases within the taxonomy, this will create a conflict).
You will then need to add the following code to your theme's functions.php file:
// Change attribute rewrite rules
add_action('woocommerce_register_taxonomy', 'razorfrog_woo_register_taxonomy');
function razorfrog_woo_register_taxonomy() {
global $razorfrog_woo_attribute_labels;
$razorfrog_woo_attributes_labels = array();
if ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) {
foreach ( $attribute_taxonomies as $tax ) {
if ( $name = wc_attribute_taxonomy_name( $tax->attribute_name ) ) {
$razorfrog_woo_attribute_labels[ $tax->attribute_label ] = $tax->attribute_name;
add_filter('woocommerce_taxonomy_args_'.$name, 'razorfrog_woo_taxonomy_args');
}
}
}
}
function razorfrog_woo_taxonomy_args($taxonomy_data) {
global $razorfrog_woo_attribute_labels;
if (isset($taxonomy_data['rewrite']) && is_array($taxonomy_data['rewrite']) && empty($taxonomy_data['rewrite']['slug'])) {
$taxonomy_data['rewrite']['slug'] = $razorfrog_woo_attribute_labels[ $taxonomy_data['labels']['name'] ];
}
return $taxonomy_data;
}
Hope this helps!

How do you assign a specific order for a category in Wordpress?

I am having a small problem which I believe to be related to a syntax issue.
I have a specific script for home which calls the bloginfo("name") for the page title then an else statement below which titles pages based on the name of the first category associated them and it works fine:
else{
$category = get_the_category();
echo $category[0]->cat_name;
}
However, I also have a default "home" category for most posts(with a few exceptions) and I do not want this category to be picked up as the page title if it happens to come first in the list of categories associated for this page.
There are two ways I can think of making this possible but I do not know how to code them. The first would be to exclude the home category in the code above but don't know how to implement it. The second would be to somehow force wordpress to always add the category "home" after the first chosen category for a particular post.
I hope that this is clear. Anyone come up with a basic solution?
Many thanks.
This plugin does most of what you want out of the box:
http://wordpress.org/extend/plugins/sem-opt-in-front/
Alternatively, do something like:
function the_main_topic_link($link = '', $id = '')
{
if (!$id || $id != get_option('default_category')) return $link;
if (get_option('show_on_front') == 'page' && get_option('page_on_front')) {
if ($blog_page_id = get_option('page_for_posts')) {
return apply_filters('the_permalink', get_permalink($blog_page_id));
} else {
return $link;
}
} else {
return home_url('/');
}
}
add_filter('category_link', 'the_main_topic_link', 10, 2);
function pre_get_posts_redirect_main_topic($q)
{
global $wp_the_query;
if ($q !== $wp_the_query) return;
if ($q->is_category(get_option('default_category'))) {
wp_redirect(home_url('/'));
die;
}
}
if (!is_admin()) add_action('pre_get_posts', 'pre_get_posts_redirect_main_topic');
It'll treat linking to your default category as linking to your front page.

Resources