Remove Category Base from WordPress Category URL - php

I fished around the internet for a solution to this, tried a plugin or two to remove the /category/ from wordpress url's.
While some of these plugins are good, the category link still display's /category/.
Also I've tried putting in ./ in the category base options in permalinks settings.
Does anyone know how I could do like a php search and replace or something like that?

A cleaner solution:
add_filter('user_trailingslashit', 'remcat_function');
function remcat_function($link) {
return str_replace("/category/", "/", $link);
}
add_action('init', 'remcat_flush_rules');
function remcat_flush_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_filter('generate_rewrite_rules', 'remcat_rewrite');
function remcat_rewrite($wp_rewrite) {
$new_rules = array('(.+)/page/(.+)/?' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2));
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

Using WordPress 3.9.1 (latest version as of this post) I simply added a single line to my theme's functions.php...
$wp_rewrite->add_permastruct('category_base', '%category%');
Then I opened Settings > Permalinks and hit Save. This appears to flush the permalink cache and makes it work.

http://wordpress.org/extend/plugins/wp-no-category-base/ and it doesn't alter permalinks, so removing it reverts the structure with no problem. And you don't have to alter core files.

https://wordpress.org/plugins/no-category-base-wpml/ is a plugin that solves the problem and works with current versions of WordPress.

Related

Removing /shop/ from WooCommerce URLs

Currently I have a WooCommerce installation where product URLs are produced as such:
domain.com/shop/product-category/product-name/
Due to SEO and current site structure, I would like to change it to:
domain.com/product-category/product-name/
I know how to remove shop from the breadcrumb using the woocommerce_get_breadcrumb filter, but I am unsure how to proceed for the URLs themselves.
All I've found on Stack Overflow are people recommending to install a plugin called Premmerce. This plugin has a premium version and constantly hassles you to upgrade: Removing /product-category/ and /shop/ from URL in WooCommerce
I would like to do this programmatically from my own plugin or just functions.php
WooCommerce forces /product/ on you if you try only inputting /%product_cat%/ in the permalinks panel through class-wc-admin-permalink-settings.php. The wc permalinks is an option called "woocommerce_permalinks". Since I only want to have /%product_cat%/, I can just force it even though it's not the most elegant solution:
add_action( "update_option_woocommerce_permalinks", "apply_product_cat", 10, 3 );
function apply_product_cat( $old_val, $new_val, $option_name ) {
if ($option_name == "woocommerce_permalinks") {
$new_val['product_base'] = "/%product_cat%/";
update_option( "woocommerce_permalinks", $new_val );
}
}
What Premmerce does is effectively offer you another admin panel, where they just save to this option on their own.

Removing the WooCommerce sidebar the correct way - TwentySeventeen?

I'm working on a project that is running of a child theme of TwentySeventeen and whilst the rest of the site doesn't have a sidebar, WooCommerce seems to have it.
For example, the shop page has it - I have tried a few things already and none work without caveats or didn't work at all:
I tried copying archive-product.php to my theme dir in woocommerce/archive-product.php and removing the below:
do_action( 'woocommerce_after_main_content' );
This didn't work.
I then tried doing:
remove_action('woocommerce_sidebar','woocommerce_get_sidebar',10);
...this didn't work either.
I found this answer and it worked, but didn't make the page full width (still had space for the sidebar) and a comment on the answer noted using that method isn't a great idea.
I also found this answer but it involves adding CSS, something I'd like to avoid as it isn't the most robust method in-case class names change in the future etc...
Isn't there a proper way of doing this without potential side affects?
Please, add this code to your functions.php
For remove only woocommerce side bar
function disable_woo_commerce_sidebar_mms() {
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10);
}
add_action('init', 'disable_woo_commerce_sidebar_mms')
for remove all side bars
function remove_sidebar_mms() {
return false;
}
add_filter( 'is_active_sidebar', 'remove_sidebar_mms', 10, 2 );
OR
You can try this with to increase the priority hope fully its work
remove_action('woocommerce_sidebar','woocommerce_get_sidebar',25);
With the help of Mannu saraswat's answer and some fiddling around I came up with a solution:
// Remove the sidebar
add_action('get_header', 'blm_wc_remove_sidebar_check', 10);
// Removes the sidebar
function blm_wc_remove_sidebar($index) {
return false;
}
// Check to see if we're on a WooCommerce page and if so, remove the sidebar
function blm_wc_remove_sidebar_check() {
if ( is_woocommerce() ) {
add_filter('is_active_sidebar', 'blm_wc_remove_sidebar', 10, 1);
}
}
This avoids having to do the is_active_sidebar check / filter addition on non-WooCommerce pages.
Maybe there is a cleaner way to do this, but this worked for me.

Add custom htaccess record for custom url path rewrite within wordpress

I've integrated a custom PHP script within my wordpress instalation (with theme page-template).
The page is /islands/island/ and I get the islandId with GET paramater. So the URL looks like https://website.com/islands/island/?iid=1
But now I want to rewrite the url to be like this: https://website.com/islands/island/1
I already tried edditing the .htaccess but with no luck.
After some research I found editing .htaccess is not the right way to do this. So I used the followingn code and added it to my theme's function.php.
function add_directory_rewrite() {
add_rewrite_tag("%iid%", '([^/]*)');
add_rewrite_rule('^islands/island/([^/]*)', 'index.php?pagename=islands/island&iid=$matches[1]', 'top');
}
add_action( 'init', 'add_directory_rewrite' );
But unfortunately it is not working. When I browse to the page http://website.com/islands/island/1 it redirects to http://website.com/islands/island/.
Am I missing something?
Got it working!
Working code:
function add_directory_rewrite() {
add_rewrite_tag("%iid%", '[\d+]');
add_rewrite_rule('islands/island/([^/]*)', 'index.php?pagename=islands/island&iid=$matches[1]', 'top');
}
add_action( 'init', 'add_directory_rewrite' );
Changed the placeholder tags as #Mario suggested.

Disabled caching for a WordPress shortcode?

Preface: I have developed a plugin, so editing theme files is out of the question.
My shortcode function:
/*** [leaderboard] shortcode with tournament_id input value ***/
function leaderboard_shortcode( $tournamentid ) {
ob_start();
include_once dirname( __FILE__ ) . '/leaderboard.php';
$output = ob_get_clean();
return $output;
}
add_shortcode( 'golf-deputy-leaderboard', 'leaderboard_shortcode' );
I tried adding in to relevant pages, however, if the shortcode is added to a page not controlled by the plugin - e.g. the home page - , the shortcode caches.
I guess my question is this: what options do I have to NOT cache the content of the shortcode, even if the page it is placed on, has caching enabled.
I have been racking my brain, the internetz, the WordPress Codex... all to no avail. I am open to any and all suggestions. Help me Stackoverflow, you're my only hope.
Cache functions start working before all possible filers&actions, and as shortcodes run inside the_content filter, it is impossible not to cache a shortcode only. I mean, when WP returns cached data, it returns static HTML data, no any theme&plugin's PHP runs in that case.
You need add that page (which has a shortcode in its content) to exclude list of your plugin. All popular cache plugins have such exclude field inside their settings.
Or another way is building custom cache solution which simply ignores caching then the_content contains any shortcode.

Wordpress conditional permalink

I have a specific page on my wordpress website example.com/download/ that I already have a page for. I would like to add some sort of php code to enable the following functionality.
If there is a subpath after the /download/<name>, I would like to fetch a file from some directory based on <name>
If no subpath is specified (/download/) I want to display the page that is already written
Is this possible with Wordpress hooks/filters?
I have a feeling it has something to do with the WP_Rewrite class, but I am not sure exactly what to write, nor where to put it without WP or theme updates wiping the code
Yes, you have to add a custom rewrite rule. You can use your theme's functions.php to add the new functionality.
function custom_rewrite_rule() {
add_rewrite_rule('^download\/([^/]+)\/?','your_url_to_the_downloader_file.php?download=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
To make it work, first you need to go to Admin -> Settings -> Permalinks and click the save button so the new rule is added.

Categories