change wordpress site slug - wordpress

I need to adding something in my child functions.php to change slug from "campaigns" to something else.
Right now it depends on
define( 'EDD_SLUG', apply_filters( 'atcf_edd_slug', 'campaigns' ) );
from a plugin. I can't edit that plugin (should be update often) and I should resolve it in child theme

Inside the function replace NEWSLUG with whatever you want that slug to be.
function wpse_update_slug( $slug ) {
return 'NEWSLUG';
}
add_filter( 'atcf_edd_slug', 'wpse_update_slug' );

Related

How to remove the original link of a post in Wordpress after using post_link and add_rewrite_rule to change the link?

I changed the permalinks of my posts using post_link, adding "/test/" to it.
So for example www.website.com/this-is-fun/ is now having the permalink www.website.com/test/this-is-fun/
This permalink is correctly showing in the admin, when editing a post, and also on the website the link to the post is the new one.
However, when I open this link it is not working, I get a 404. I was able to fix that with a add_rewrite_rule, but now my problem is that BOTH urls exist. I want only the new one to exist.
Am I making a mistake in the post_link part, is there a way you can automatically disable the original link and make the new one work? Or should I use add_rewrite_rule to disable the original? Or is there another way?
This is my code:
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
$permalink = home_url('/test/' . $post->post_name .'/' );
return $permalink;
}
add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
'test/([^/]+)(?:/([0-9]+))?/?$',
'index.php?name=$matches[1]',
'top'
);
}
add_action( 'init','flush_rewrite_rules' );
I know I can change the permalink in the settings btw, but this new permalink will not be for all posts, so I need to do it this way.

Custom post template in a plugin erase theme's one

i work with sportpress on Wordpress. I have a custom plugin that add a lot of function to it.
I created a template for the custom post-type of sportpress. They are in my plugin but i don't find how to make them prioritary on theme. The theme isn't a custom'one, so i don't want to delete or modify anything in it.
I have tried several things but theme's file is everytime superior.
Things tried :
naming the file 'single-....php',
filter : add_filter( 'template_include', 'toornament_template' ),
shortcode in theme file (works but not what i want...),
Thanks a lot...
EDIT :
here is what i tried exaclty, it works with another type of post that has no theme template, but not for the one that has single-post template in the theme...
add_filter( 'template_include', 'player_template', 1 );
function player_template( $template ) {
if ( is_singular( 'sp_player' ) && file_exists( plugin_dir_path(__FILE__) . 'templates/player-tpl.php' ) ){
$template = plugin_dir_path(__FILE__) . 'templates/player-tpl.php';
}
return $template;
};

Remove category base from WordPress url only for specific category

I would like to remove the category base from Wordpress URL only for specific category.
For example, i need to change:
mysite.com/category/blog
to
mysite.com/blog
but i want to keep other categories unchanged:
mysite.com/category/songs
I think that it could be achieved with some .htaccess rule, but I found some generic rules that remove the basic category in all the url.
you can easily achieve this by using Enhanced Custom Permalinks Wp plugin. you just need to go edit the category, yo will see a field to add your custom url.
https://wordpress.org/plugins/enhanced-custom-permalinks/
This can be accomplished with some custom filters & actions.
Try placing this code in your theme's functions.php file:
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the categories for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "News" ) {
$permalink = trailingslashit( home_url('/'. $post->post_name .'-'. $post->ID .'/' ) );
}
return $permalink;
}
add_action('generate_rewrite_rules', 'custom_rewrite_rules');
function custom_rewrite_rules( $wp_rewrite ) {
// This rule will will match the post id in %postname%-%post_id% struture
$new_rules['^([^/]*)-([0-9]+)/?'] = 'index.php?p=$matches[2]';
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite;
}
This will set up the permalink structure that you want for posts:
You can easily do that without using plugins.
Through Admin panel
go to settings->permalinks and select default to custom
here you know more..
https://codex.wordpress.org/Using_Permalinks

Woocommerce change product style and display it on a page

Sorry in advance for my approximative english. I would like to change product page style on woocommerce for a specific product category (don't display pictures and some other important layout changes). Already made it and it works, the problem is that I would like to display these products on a single page. I tried using woocommerce integrated shortcode [product_page id=$id], the problem is that the custom style and layout I created on content-single-product.php (in woocommerce folder) is not applied using this shortcode. I tried to copy my script on class-ws-shortcodes.php (woocommerce folder) but it didn't worked. What should I do to display the products on a specific page, with the real style of these products page (the custom one I created), and not the woocommerce shortcode one?
Thanks in advance for your answers
There are two ways to achieve this, it just depends on whether you want to edit single-product.php or content-single-product.php. Both will get you to the same end result.
To do the former, you can use default WordPress functionality and filter the template under given conditions via template_include
add_filter( 'template_include', 'so_29984159_custom_category_template' );
function so_29984159_custom_category_template( $template ){
if ( is_single() && get_post_type() == 'product' && has_term( 'custom_category', 'product_cat' ) ) {
$template = locate_template( 'single-product-custom.php' );
}
return $template;
}
Or if you'd rather create a custom content-product.php template part, you can use WooCommerce's wc_get_template_part which works in pretty much the same way.
Technically I think you can use the same conditional logic, but I tweaked it here to point out the variables WooCommerce makes available at the filter.
add_filter( 'wc_get_template_part', 'so_29984159_custom_content_template_part', 10, 3 );
function so_29984159_custom_content_template_part( $template, $slug, $name ){
if ( $slug == 'content' && $name == 'product' && has_term( 'custom_category', 'product_cat' ) ) {
$template = locate_template( 'content-product-custom.php' );
}
return $template;
}
In either case the single-product-custom.php or content-product-custom.php would be in your theme's root folder. To put them somewhere else, just change the path in the locate_template() argument.
You can not do coding in wordpress's file i.e. page.php to modify woocommerce pages. Its wrong way. You simply override WooCommerce pages into your theme folder and then modify any pages which you want to do. All templates/pages are located in a woocommerce/templates folder.
Here you can find documentation.
http://docs.woothemes.com/document/template-structure/
edit woocommerce/templates/content-single-product.php for single product view

Hook taxonomy template to category template

I've created a plugin which registers a custom post type and a custom taxonomy for me.
Now, whenever I visit: www.mysite.com/events_categories/category I should get a list of all the posts under that category. I want to use my own template and not Wordpress' default category template. Also, I want this template to be used for ALL categories, not just one category.
I used this code:
function taxonomy_template() {
global $post;
if( is_tax( 'event_categories' ) ) {
$tax_tpl = dirname( __FILE__ ) . '\taxonomy-event_categories.php';
}
return $tax_tpl;
}
add_filter( 'template_include', 'taxonomy_template' );
And while it works for the taxonomy, it makes everything else on the site go blank. This is because the $tax_tpl is empty if the page I'm on isn't a taxonomy.
I've tried using template_redirect, but no luck either.
So I want to know how to hook my template (taxonomy-event_categories.php) to the Wordpress' category template.
Hope you guys have a solution, because I've looked everywhere and it's possible to find the solution, yet I see many plugins doing this effortlessly.
Okay, so I solved it using this code:
add_action('template_redirect', 'taxonomy_template');
function taxonomy_template( ){
$tax_tpl = dirname( __FILE__ ) . '\taxonomy-event_categories.php';
if( is_tax('event_categories') ) {
include $tax_tpl;
die();
}
}
I've no idea why this works because I think I already tried this specific code, but it didn't work. Let's just hope it will keep working.

Resources