Custom product attribute URL - wordpress

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!

Related

Filter by Attribute not filtering on shop page

I’ve developed a new theme for a website, but i seem to have a problem related to widget Filter By Attribute.
When i use the filter, that comes with the gutenberg block editor for widgets, the filter attribute widget doesn’t filter anything. (i guess it uses ajax to filter, but its not filtering anything)
When i use the add_filter('use_widgets_block_editor', '__return_false'); to remove the gutenberg and use a new filter by attribute (which is a different widget, than the one that comes with Gutenberg), the widget works, because it filters the query by passing some params on the url, but has a problem. It shows variations that are out of stock, which is something that have been fixed with the new widget, using the product lookup tables, and it's exactly what i'm trying to achieve here.
I strongly believe this is related to the AJAX call it uses to filter the shop page, so i guess i'm missing an ID or Class, to the product wrapper or something.
Does anyone also had this problem?
If you want to see the page in question, is this one https://www.hiima-store.com/bch/shop/
When you go into Filters, you can see both widgets there. (there are two for sizes. the first one uses query params and the second one, is the one that is not filtering).
I did a custom script regarding that.
It might help someone with the same problem.
It will hide the product if the variation is out of stock.
add_action('woocommerce_before_shop_loop_item_title', 'out_of_stock_variations_loop');
function out_of_stock_variations_loop()
{
global $product;
if (isset($_GET["filter_size"])) { // check if the attribute is in the url
if ($product->product_type == 'variable') {
$available = $product->get_available_variations();
if ($available) foreach ($available as $instockvar) {
if (isset($instockvar['attributes']['attribute_pa_' . __('size', 'hiima')])) {
var_dump('entrei');
if (($instockvar['attributes']['attribute_pa_.' . __('size', 'hiima')] == $_GET['filter_size']) && (!$instockvar['max_qty'] > 0)) {
global $product;
$id = $product->get_id();
echo "<style>.post-$id{display: none}</style>";
}
}
}
}
if (!$product->is_in_stock()) {
global $product;
$id = $product->get_id();
echo "<style>.post-$id{display: none}</style>";
}
}
}

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

ACF default field group settings for "hide on screen"

I would like to set default settings for style and hide on screen settings within the field group settings.
I've found posts talking about modifying settings for individual ACF fields like the WYSIWYG or image fields like so.
add_filter( 'acf/get_valid_field', 'change_post_content_type');
function change_post_content_type( $field ) {
if($field['type'] == 'wysiwyg') {
$field['tabs'] = 'visual';
$field['media_upload'] = 0;
}
if($field['type'] == 'image') {
$field['preview_size'] = 'small';
}
if($field['type'] == 'style') {
$field['style'] = 'seamless';
}
return $field;
}
Using this overrides whatever is selected rather than setting the value as default but it's good enough for what I need.
The image and WYSIWG field work fine but I can't get it working on the field group setting fields. I don't think the $field['type'] == 'style' is correct but as it doesn't follow the same structure as the other fields I don't know what I should be using.
Any ideas?
update
I've found this but I can't figure out how to use it. The following doesn't work
add_action('acf/render_field_group_settings', 'change_field_group_settings', 10, 1);
function change_field_group_settings( $field_group ) {
$field_group['style'] = 'seemless';
$field_group['hide_on_screen'] = array('the_content');
return $field_group;
}
You may need change your syntax to use proper formatting for style and hide_on_screen, for example:
'hide_on_screen' => array(
0 => 'the_content',
),
Best practice would be to create a new Field Group in your WP CMS, publish it, and navigate to:
Custom Fields > Tools > (check the box for your Field Group) > Generate PHP
That way you can view the proper output.

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

make specific tag visible only to logged in users in frontend

In my wordpress cms, I display a regular list of all existing tags on my single template. Now I need to exclude one specific tag and show it only to the logged in people. What wordpress function/hook should I be looking into ?
EFFORTS - I know that I could use is_user_logged_in() and am also looking into is_tag() but how do I put this together. Currently I am having it displayed on my single template with the_tags() but it does not seem to offer such parameter to exclude a particular tag.
Psuedo code would be : If user is logged in show all tags except this specific tag.
add_filter('get_the_terms', 'tag_filter', 100, 1);
function tag_filter($tags) {
$exclude = array('hello', 'demo', 'tag_one', 'tag_two');
foreach($tags AS $index => $tag) {
$name = $tag->name;
#$name = $tag->slug;
if(in_array(strtolower($name), $exclude)) {
unset($tags[$index]);
}
}
return $tags;
}

Resources