How to hide a tag from a download page of Easy Digital Downloads - wordpress

I created a function to redirect all the products with the tag 'ABC' to a different page (for logged-out users only).
is_singular('download') && has_term(array('ABC'), 'download_tag')
The problem is that logged-in users can still access the page and see the 'ABC' tag in the tags list.
What's the function that I can add to the functions.php file that will only hide the ABC tag specifically?
Thank you

You can hook into get_object_terms to filter terms before they are displayed. Here's an example:
function custom_hide_terms( $terms, $object_ids, $taxonomies, $args )
{
if ( !is_admin() ) {
// note that we are filtering by the term slug, not the term name,
// so be careful with "ABC" versus "abc"
$exclude = array( 'ABC' );
// Loop through terms and remove ABC
if ( $terms ) {
foreach ( $terms as $key => $term ) {
if (is_object($term) && in_array( $term->slug, $exclude ) ) {
unset( $terms[$key] );
}
}
}
}
return $terms;
}
add_filter( 'get_object_terms', 'custom_hide_terms', 10, 4 );

Related

How can I replace a custom-post-type with a custom post category in permalink without getting a 404 in wordpress?

I'm a wordpress newbie. I will try to describe my problem in the clearest way possible.
I'm trying to do two things here:
Remove CPT from a permalink.
Add a custom taxonomy type where it used to be the CPT in the permalink.
Permalinks on site used to be like this:
http://example.com/custom-post-type/post-name
I managed to remove the CPT from the permalink based on this:
how to remove custom post type from wordpress url?
Then I modify my code to add the custom taxonomy type to the permalink to be like this:
http://example.com/post-category/post-name
This is my code:
function remove_cpt_slug( $post_link, $post ) {
if ( 'custom-post-type-name' === $post->post_type && 'publish' === $post->post_status ) {
$post_tags = get_the_terms($post->ID, 'custom-post-type-name-category');
$post_link = str_replace( '/' . $post->post_type . '/', '/' . $post_tags[0]->slug . '/', $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'remove_cpt_slug', 10, 2 );
function add_cpt_post_names_to_main_query( $query ) {
// Return if this is not the main query.
if ( ! $query->is_main_query() ) {
return;
}
// Return if this query doesn't match our very specific rewrite rule.
if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
return;
}
// Return if we're not querying based on the post name.
if ( empty( $query->query['name'] ) ) {
return;
}
// Add CPT to the list of post types WP will include when it queries based on the post name.
$query->set( 'post_type', array( 'post', 'page', 'custom-post-type-name' ) );
}
add_action( 'pre_get_posts', 'add_cpt_post_names_to_main_query' );
This worked but now every post from my CPT gives a 404. How can I solve this?

Detect a screen option change in WordPress Admin

In WordPress code, how can I detect a screen option change? Ie, in the below image I want to hook when one of the column screen options changes. Is there a specific hook for this?
My usecase:
I need to add a screen option to the 'All Posts' page. If the user has unchecked my screen option then the All Posts table should not display posts that have a certain category. I have pretty much all my code for this usecase working (see below) apart from being able to hook/detect when 'My Custom Screen Option' is changed (ticked on or off). If I can do that I can then update the users meta with this decision.
function add_custom_columns( $columns ) {
$columns['display_xyz'] = __( 'XYZ Posts' );
return $columns;
}
add_filter( 'manage_posts_columns', 'add_custom_columns' );
function set_sortable_columns( $columns ) {
$columns['display_xyz'] = 'display_xyz';
return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'set_sortable_columns' );
function sort_all_posts( $query ) {
global $current_screen;
// If on 'All Posts' page.
if ( isset( $current_screen ) && is_admin() && $current_screen->id === 'edit-post' && $query->query_vars['post_type'] === 'post' ) {
$show_xyz_posts = get_user_meta( get_current_user_id(), 'display_xyz' );
if ( ! $show_xyz_posts ) {
// Edit $query to not retrieve posts that have a specific category 'xyz'
}
}
}
add_filter( 'parse_query', 'sort_all_posts' );
// How to detect change of my custom screen option so I can hook the below option??
function on_change_screen_option($option, $value) {
if ( $option === 'display_xyz' ) {
update_user_meta( get_current_user_id(), 'display_xyz', $value );
}
}
add_action( '???', 'on_change_screen_option' );

How to block certain tags from creating in WordPress

Is there any WordPress theme function code that blocks certain wordpress tags from being created? I'd want to exclude some tags from the keyword list, for example, I don't want WordPress to create the following stop-words tags:
adult, bikini, enjoyment, fun, block, admin
You can use the pre_insert_term filter hook. that will help you to prevent tags before inserting. try the below code.
function prevent_some_tags_from_being_add( $term, $taxonomy ){
if( $taxonomy == 'post_tag' ){
$prevent_tags = array( 'adult', 'bikini', 'enjoyment', 'fun', 'block', 'admin' );
if( in_array( $term, $prevent_tags ) ){
return new WP_Error( 'invalid_term', __( 'Sorry this tag is not allowed.' ) );
}
}
return $term;
}
add_filter( 'pre_insert_term', 'prevent_some_tags_from_being_add', 10, 2 );

Breadcrumbs on product page don't reflect current url/slug when using multiple categories for product

My Wordpress site uses Woocommerce for products to sell. But when you assign multiple product categories to a product and visit the product page (with Product Permalinks set to Shop base with category), something unexpected is happening. The breadcrumb link to the (parent) category is always the same one and does not reflect the navigated url. (This is the same problem as described here.)
Problem in summary:
Navigate to
Breadcrumb link
Expected breadcrumb link
/shop/category-1/sample-product/
category-2
category-1
/shop/category-2/sample-product/
category-2
category-2
I found no real answer on any source that fixes this. (I think it should be fixed, but some might say it's for SEO reasons, preventing duplicate site contents.) So to help anyone searching for the answer, here it is:
What's going (wr)on(g)?
The breadcrumbs on the product page are produced by the woocommerce_breadcrumb() function. Which in turn gets it through WC_Breadcrumb->generate().
Traversing further in the code, you end up inside the add_crumbs_single() function where the woocommerce_breadcrumb_product_terms_args filter is applied to the ordering of the product terms fetch. (See wp_get_post_terms and WP_Term_Query::__construct() for more info on this.)
It's clear from the code, that they prioritize the term with the highest parent value, in other words, the term with the latest added parent in the database.
Solution 1
Since this will always be the same for this product, you might want to add a filter to your theme's function.php (or using a custom plugin) that will overwrite this behavior. I got mine working using this:
function prioritize_current_url_cat_in_breadcrumbs( $args ) {
// Only if we're on a product page..
if ( is_product() ) {
// ..and there's a product category slug in the navigated url..
$product_cat_slug = get_query_var( 'product_cat', '' );
if ( ! empty($product_cat_slug) ) {
// ..which we can find
$product_cat = get_term_by( 'slug', $product_cat_slug, 'product_cat', ARRAY_A );
if ( ! empty($product_cat) ) {
// Then only get that current product category to start the breadcrumb trail
$args['term_taxonomy_id'] = $product_cat['term_taxonomy_id'];
}
}
}
return $args;
}
add_filter( 'woocommerce_breadcrumb_product_terms_args', 'prioritize_current_url_cat_in_breadcrumbs' );
The rest of the add_crumbs_single() function will take care of traversing the category's parents etc up 'till Home.
Solution 2
Alternatively, you could use the woocommerce_breadcrumb_main_term filter to change the 'main term' used for the breadcrumb trail. The filter function receives 2 arguments: the main term (as WP_Term) and an array of WP_Terms with all product categories found. And it returns one term, so you can search through the array and pick the right one you want to start the breadcrumbs with.
function prioritize_current_url_cat( $first_term, $all_terms ) {
if ( is_product() ) {
$product_cat_slug = get_query_var( 'product_cat', '' );
if ( ! empty($product_cat_slug) ) {
// Get the WP_Term with the current slug
$filtered_terms = array_values( array_filter($all_terms, function($v) use ($product_cat_slug) {
return $v->slug === $product_cat_slug;
}) );
if ( ! empty($filtered_terms) ) {
return $filtered_terms[0];
}
}
}
// Fallback to default
return $first_term;
}
add_filter( 'woocommerce_breadcrumb_main_term', 'prioritize_current_url_cat', 10, 2 );
Hope this helps anyone searching for hours and working through lines of source code..!
Extra: Fix permalinks on archive pages as well
For tackling the same problem on the product archive pages, you can hook into the post_type_link filter and pre-emptively replace the %product_cat% part of the permalink with the correct slug like so:
/**
* Set product link slugs to match the page context,
* for products with multiple associated categories.
*
* For example:
* when viewing Category A, use '/category-a/this-product/'
* when viewing Category B, use '/category-b/this-product/'
*/
function my_plugin_product_url_use_current_cat( $post_link, $post, $leavename, $sample ) {
// Get current term slug (used in page url)
$current_product_term_slug = get_query_var( 'product_cat', '' );
if ( empty ( $current_product_term_slug ) ) {
return $post_link;
}
if ( is_product_category() ) {
// Get current term object
$current_product_term = get_term_by( 'slug', $current_product_term_slug, 'product_cat' );
if ( FALSE === $current_product_term ) {
return $post_link;
}
// Get all terms associated with product
$all_product_terms = get_the_terms( $post->ID, 'product_cat' );
// Filter terms, taking only relevant terms for current term
$matching_or_descendant_terms = array_filter( array_map( function( $term ) use ( $current_product_term ) {
// Return term if it is the current term
if ( $term->term_id === $current_product_term->term_id ) {
return [ TRUE, $term ];
}
// Return term if one of its ancestors is the current term (highest hierarchy first)
$parent_terms = array_reverse( get_ancestors( $term->term_id, 'product_cat' ) );
foreach ( $parent_terms as $parent_term_id ) {
if ( $parent_term_id === $current_product_term->term_id ) {
return [ FALSE, $term ];
}
}
// Leave out all others
return NULL;
}, $all_product_terms ) );
if ( count( $matching_or_descendant_terms ) > 0 ) {
// Sort terms (directly associated first, descendants next)
usort( $matching_or_descendant_terms, function( $a, $b ) {
if ( $a[0] === $b[0] ) {
return 0;
} else if ( TRUE === $a[0] ) {
return -1;
} else {
return 1;
}
} );
// Get entire slug (including ancestors)
$slug = get_term_parents_list( $matching_or_descendant_terms[0][1]->term_id, 'product_cat', [
'format' => 'slug',
'separator' => '/',
'link' => false,
'inclusive' => true,
] );
// Set url slug to closest child term of current term
// or current term (if directly associated)
$post_link = str_replace('%product_cat%/', $slug, $post_link);
}
} else if ( is_product() ) {
$post_link = str_replace('%product_cat%', $current_product_term_slug, $post_link);
}
return $post_link;
}
add_filter( 'post_type_link', 'my_plugin_product_url_use_current_cat', 10, 4 );
Explanation
As #Markos mentioned, we tried to figure out the most logical and intuitive way to do this.
Basically, it checks if the current product category (slug from the url) is directly associated with the displayed product. If so, use that slug for the link to the product page. (Using one of the solutions above, the breadcrumbs reflect the url path for intuitive navigation.)
If the current viewed product category is not directly associated with the product, it looks for the first matching descendant (sub) category and uses that slug for the link to the product page.
This way, the user always sees how they came on the current product page and can easily navigate back to the category.
Note: If this snippet doesn't change anything, try an earlier hook priority and/or check whether the $post_link variable contains the %product_cat% template part.
After a lengthy chat with Philip and troubleshooting scenarios when a shop has multiple layers of categories (thank you Philip) I found the following approach for the permalinks working for me.
function my_plugin_product_url_use_current_cat( $post_link, $post, $leavename, $sample ) {
if ( is_product_category() || is_product() ) {
$product_cat_slug = get_query_var( 'product_cat', '' );
// get all the category of products
$terms = get_the_terms ( $post->ID, 'product_cat' );
foreach($terms as $term){
$term_slug = $term->slug;
// check if category page belongs to the category of the product
if($term_slug == $product_cat_slug){
$post_link = str_replace('%product_cat%', $term_slug, $post_link);
return $post_link;
}
// or category page is a parent category of the product
else{
$all_parents = get_ancestors($term->term_id, 'product_cat');
foreach($all_parents as $cat_id){
$cat_term = get_term( $cat_id, 'product_cat' );
if($cat_term->slug == $product_cat_slug){
$post_link = str_replace('%product_cat%', $term_slug, $post_link);
return $post_link;
}
}
}
}
}
return $post_link;
}
add_filter( 'post_type_link', 'my_plugin_product_url_use_current_cat', 9, 4 );

WordPress SEO plugin only visible to specific user

I am using the Yoast SEO plugin in WordPress and wanted to know if there was a way to make it only visible to one specific user in the db or in the functions.php file? Not a role, an actual user.
I tried an universal solution to simply add "plugin-name" and disable it, but failed.
But, to show WPSEO only to a specific user (ID equals 2), the following works:
add_action( 'plugins_loaded', 'seo_so_25654837' );
function seo_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
remove_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
}
Don't add the code to functions.php, use it as a normal plugin.
The following is also needed to remove the SEO menu from the admin bar:
add_action( 'wp_before_admin_bar_render', 'bar_so_25654837' );
function bar_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
global $wp_admin_bar;
$nodes = $wp_admin_bar->get_nodes();
foreach( $nodes as $node )
{
if( !$node->parent )
{
if( 'wpseo-menu' === $node->id )
$wp_admin_bar->remove_menu( $node->id );
}
}
}
You can hook to pre_current_active_plugins to remove elements from the table before it is displayed. Using get_current_user_id() within the function will let you selectively hide a plugin.
function hide_plugins_by_user( $all_plugins=false ) {
global $wp_list_table;
// if the current user ID is not 1, hide it.
if ( 1 != get_current_user_id() ){
// the active plugins from the table
$plugins = $wp_list_table->items;
// loop through them
foreach ( $plugins as $key => $val ) {
// use the dir + filename of the plugin to hide
if ( $key == 'plugindir/plugin.php' ) {
unset( $wp_list_table->items[$key] );
}
}
}
}
add_action( 'pre_current_active_plugins', 'hide_plugins_by_user' );
This code is working fine (Credits goes to Hislop):
// Returns true if user has specific role
function check_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) )
$user = get_userdata( $user_id );
else
$user = wp_get_current_user();
if ( empty( $user ) )
return false;
return in_array( $role, (array) $user->roles );
}
// Disable WordPress SEO meta box for all roles other than administrator and seo
function wpse_init(){
if( !(check_user_role('seo') || check_user_role('administrator')) ){
// Remove page analysis columns from post lists, also SEO status on post editor
add_filter('wpseo_use_page_analysis', '__return_false');
// Remove Yoast meta boxes
add_action('add_meta_boxes', 'disable_seo_metabox', 100000);
}
}
add_action('init', 'wpse_init');
function disable_seo_metabox(){
remove_meta_box('wpseo_meta', 'post', 'normal');
remove_meta_box('wpseo_meta', 'page', 'normal');
}
Just place it in the functions.php file.
To disable the Yoast for all the users and enable it for just for few or specific, just add the following piece of code to your function.php file.
function remove_wpseo(){
/* if you want to keep it enabled for user with id 2 */
if ( '2' == get_current_user_id() ) {
return;
}
global $wpseo_front;
if(defined($wpseo_front)){
remove_action('wp_head',array($wpseo_front,'head'),1);
}
else {
$wp_thing = WPSEO_Frontend::get_instance();
remove_action('wp_head',array($wp_thing,'head'),1);
}
}
add_action('template_redirect','remove_wpseo');
Reference: https://makersbyte.com/disable-yoast-seo-plugin-specific-page/

Resources