Remove woocommerce duplicate taxonomy page links? - woocommerce

I am having an issue with duplicate content, which seems to be an out of the box issue with woocommerce. For example, our SEO company is complaining because of duplicate content because you can go to:
http://localhost/wp7/product-category/clothing/hoodies/
but you can also go to
http://localhost/wp7/product-category/hoodies/
So clothing is the parent taxonomy of hoodies. Is there a filter/code to make it so that you won't be able to navigate to just the child taxonomy link, but only allow http://www.example.com/product-category/parent/child?
Any help on this would be greatly appreciated.
Kind regards, and thanks!

I was able to work through this, with this solution. Which seems to work beautifully. Not sure of the repercussions in the woocommerce eco system, but it definitely works on the frontend.
/**
* Disallow duplicate Woocommerce taxonomy links
* |- Redirects http://www.example.com/product_cat/child to http://www.example.com/product_cat/parent/child
*/
add_action('template_redirect', function ($query) {
global $wp;
$productCatTaxonomy = 'product_cat';
if (is_tax($productCatTaxonomy)) {
$current_url = add_query_arg($wp->query_string, '', home_url($wp->request));
$current_args = parse_url($current_url);
$currentTerm = get_term_by('slug', get_query_var('term'), $productCatTaxonomy);
if (isset($current_args['query']) && !stristr($current_args['query'], '%2f')) {
$currentParentTerm = (isset($currentTerm->parent) && $currentTerm->term_id) ? get_term_by('id', $currentTerm->parent, $productCatTaxonomy) : 0;
if ($currentParentTerm) {
$childTermLink = (isset($currentTerm->term_id) && $currentTerm->term_id) ? get_term_link($currentTerm->term_id, $productCatTaxonomy) : 0;
if ($childTermLink) {
wp_redirect($childTermLink);
die();
}
}
}
}
return $query;
});

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 );

Hide Wordpress Categories from Users by Role

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).

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.

Manually highlight Wordpress admin menu item

Under Pages menu in Wordpress Admin page, I got this layout:
Pages
Edit (url: edit-pages.php)
Add New (url: page-new.php)
Special Pages (url: edit-pages.php?special-pages=true)
as you can see, I've added a new submenu item called Special Pages which is pretty much a link to to Edit page with custom filter. Because Wordpress use file name to identify and highlight the submenu item, so whenever I click on Special Pages, the Edit submenu item is selected. Is there anyway to force Wordpress to select Special Pages menu item instead?
Cheers
better solution:
add_filter('parent_file', 'my_plugin_select_submenu');
function my_plugin_select_submenu($file) {
global $plugin_page;
if ('__my-current-submenu-slug__' == $plugin_page) {
$plugin_page = '__my-submenu-slug-to-select__';
}
return $file;
}
To further clarify Ken Vu's answer, edit the global variables $submenu_file and $parent_file. E.g., to highlight your page:
global $submenu_file;
$submenu_file = "edit-pages.php?special-pages=true";
If you need to change the top-level item highlighted, use $parent_file. E.g., highlight the "Writing" setting page:
global $parent_file;
global $submenu_file;
$parent_file = 'options-general.php';
$submenu_file = 'options-writing.php';
Solution: use $submenu_file variable
$submenu_file = "edit-pages.php?special-pages=true"
Thanks Ken Vu and Jonathan Brinley. Using your answers, I finally got the highlighting of my admin menu to work properly. As I struggled a bit to get it to work, I though I would post the entire result here, so other people can find it more easily :
The idea is to call the parent_file filter (undocumented, as many Wordpress parts unfornatunately). In my case, I was adding a custom menu instead of the default generated when creating a custom post type.
In my custom post code, I call the add_meta_boxes action. Within this hook, I issue my call to the parent_file filter :
add_filter('parent_file', array(&$this, 'highlight_admin_menu'));
_
Then this is how my hightlight_admin_menu function looks like :
function highlight_admin_menu($some_slug){
global $parent_file;
$parent_file = 'post.php?post=149&action=edit';
return $parent_file;
}
_
This got my menu to highlight properly. Try playing around with you own code to know where to issue the add_filter('parent_file', ...) code. Find a bit of code executed only on that particular page load, but soon enough that it is still possible to modify the $parent_file variable.
I hope this helps!
For changing the highlighted menu item for a submenu item, the proper filter is submenu_file.
add_filter('submenu_file', 'menuBold');
static function menuBold($submenu_file)
{
if ( checkProperPage($_GET) ) {
// The address of the link to be highlighted
return 'post-new?post_type=foobar&foo=bar';
}
// Don't change anything
return $submenu_file;
}
The check happens in WP's ~/wp-admin/menu-header.php file on line 194 (Wordpress 4.5.3):
if ( isset( $submenu_file ) ) {
if ( $submenu_file == $sub_item[2] )
$class[] = 'current';
...
}
You can modify this code to work for you. You can change both parent and submenu with that. Tested code.
function change_active_parent($submenu_file)
{
global $parent_file;
$zone = 'edit-tags.php?taxonomy=zone&post_type=product';
$storefront = 'edit-tags.php?taxonomy=storefront&post_type=product';
$container = 'edit-tags.php?taxonomy=container&post_type=product';
if (esc_html($zone) == $submenu_file) {
$parent_file = 'parent';
$submenu_file = $zone;
}
elseif (esc_html($storefront) == $submenu_file) {
$parent_file = 'parent';
$submenu_file = $storefront;
}
elseif (esc_html($container) == $submenu_file) {
$parent_file = 'parent';
$submenu_file = $container;
}
return $submenu_file;
}
add_filter( 'submenu_file', 'change_active_parent' );
Use the load-{$page_hook} action hook and modify the necessary globals:
/**
* For giggles, lets make an admin page that is not "in the menu" to play with.
*/
add_action('admin_menu', 'mort1305_admin_menu');
function mort1305_admin_menu() {
add_submenu_page(
NULL,
'Page Title',
'',
'administrator',
'my_slug',
'mort1305_page_content'
);
}
/**
* The menu item to highlight and the submenu item to embolden.
*/
add_action('load-admin_page_my_slug', 'mort1305_on_page_load');
function mort1305_on_page_load(){
global $plugin_file, $submenu_file, $title;
$plugin_page = 'slug-of-menu-item-to-be-highlighted';
$submenu_file = 'slug-of-submenu-item-to-be-bold';
foreach($submenu[NULL] as $submenu_arr) {
if($submenu_arr[2] === 'test_page_slug') {
$title = $submenu_arr[3];
break;
}
}
}
/**
* Page content to display.
*/
function mort_1305_page_content() {
echo This is the '. get_admin_page_title() .' page. The slug of my parent is '. get_admin_page_parent() .'.';
}

Resources