Rewrite rules for EDD product page - wordpress

I want my url changes depending on the term of taxonomy category_download custom post types download.
Example:
I have 2 categories:
– plugins
– themes
I wish I had a url for my single download:
/plugins/name_of_download/
AND
/themes/name_of_download/

Try adding this code to your themes functions.php file,
function __custom_messagetypes_link( $link, $term, $taxonomy )
{
if ( $taxonomy == 'plugins' ){
return str_replace( 'plugins/', '', $link );
}
else if ( $taxonomy == 'themes' ){
return str_replace( 'themes/', '', $link );
}
else{
return $link;
}
}
add_filter( 'term_link', '__custom_messagetypes_link', 10, 3 );
I am assuming the used slug, May be you have to change the your taxonomy slug if these are different.
If have any problem you can freely ask, Thanks,

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?

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

is_feed() not working for Custom Post Types

I have a CPT called quotes with ACFs that I want to add to my RSS feed.
My rss url is example.com/feed/?post_type=quotes
When I use is_feed() this code works. But it doesn't work when I try to limit it to only the quotes CPT using: is_feed('quotes')
function add_fields_to_rss ($content) {
if(is_feed('quotes')) {
$post_id = get_the_ID();
$output = '<div><p>' . get_field('the_quote', $post_id) . '</p>';
$output .= '<h3 style="text-align: right;">' . get_field('quoted', $post_id) . '</h3>';
$output .= '</div>';
$content = $content.$output;
}
return $content;
}
add_filter('the_content','add_fields_to_rss');
Is there anything else I need to do so that this function will work with my quotes CPT only?
if I understand currently you want to add all custom posts types to the rss feed?
If so the following should work:
add_filter( 'request', 'myfeed_request' );
function myfeed_request( $qv ) {
if ( isset( $qv['feed'] ) ) {
// gett all custom posts types
$qv['post_type'] = get_post_types();
}
return $qv;
}
Tell me if this is what you're looking for
EDIT 1: Aswering your updated question and comment
The following is for adding specific customs posts types to the rss feed
<?php add_filter( 'request', 'multiple_CPT_to_rss' );
function multiple_CPT_to_rss( $qv ) {
if ( isset( $qv['feed'] ) && !isset( $qv['post_type'] ) ) {
$qv['post_type'] = array( 'post', 'my_CPT_1', 'my_CPT_2' );
}
return $qv;
}; ?>
CPT creates default rss links in the form of My rss url is example.com/feed/?post_type=my_post_type
If seems that doesn't work with is_feed('my_post_type')
I'm guessing to make it work you need to create the feed with add_feed()
But I didn't test that because I went another way.

Woocommerce product ID in url

I'm looking for a way to put a Woocommerce product ID in a URL, e.g.:
domain.com/product-category/id/product-title
The ID is the product's post ID, e.g. "12092". Automatically created by WooCommerce in Wordpress.
Can this be done in a way? Either easily through the Wordpress permalinks settings or in a different way through hacking my way into the files.
This nearly works:
<?php
add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3);
function wpse33551_post_type_link( $link, $post = 0 ){
if ( $post->post_type == 'product' ){
return home_url( 'p/' . $post->ID );
} else {
return $link;
}
}
add_action( 'init', 'wpse33551_rewrites_init' );
function wpse33551_rewrites_init(){
add_rewrite_rule(
'product/([0-9]+)?$',
'index.php?post_type=product&p=$matches[1]',
'top' );
}
?>
But the output is:
domain.com/p/45
I'd like it to be:
domain.com/p/45/title-of-the-post
Thanks!
Why so complicated? Go to settings -> permalinks and click "Shop based URL" and save. Now you can see that the permalink structure in the "Custom structure". It should looks like this now:
/shop/%product_cat%/
Now add the post_id to the permalink setting like this:
/shop/%product_cat%/%post_id%/
Save it and go ahead.
You can use $post->post_name to get the slug.
Use a more permissive regular expression to accept any url that ends with /p/[id][anything]
<?php
add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3);
function wpse33551_post_type_link( $link, $post = 0 ){
if ( $post->post_type != 'product' )
return $link;
return home_url( 'p/' . $post->ID . '/' . $post->post_name );
}
add_action( 'init', 'wpse33551_rewrites_init' );
function wpse33551_rewrites_init(){
add_rewrite_rule(
'p/([0-9]+)?.*?$',
'index.php?post_type=product&p=$matches[1]',
'top'
);
}
?>
You might need to 'reset' your permalinks in the settings menu for WordPress to accept the new rule.

How can I use category IDs in WordPress permalinks?

I want to use something like:
http://example.com/%category_id%/%postname%/
for the permalink structure.
For example, if a post has a category with an ID of 3, then the URL for the post will be
http://example.com/3/post-name/
Does anyone know how this can be done? I don't mind modifying WordPress core.
This code adds the %category_id% rewrite tag, and filters post permalinks to replace them with the actual category ID (lowest if there are multiple categories). You can place this in a plugin or in your theme file.
add_action( 'init', 'so6159452_init' );
function so6159452_init()
{
add_rewrite_tag( '%category_id%', '([0-9]+)' );
}
add_filter( 'post_link', 'so6159452_post_link', 10, 2 );
function so6159452_post_link( $permalink, $post )
{
if ( false !== strpos( $permalink, '%category_id%' ) ) {
$cats = get_the_category( $post->ID );
if ( $cats ) {
usort( $cats, '_usort_terms_by_ID' ); // order by ID
$category_id = $cats[0]->cat_ID;
} else {
// Error: no category assigned to this post
// Just use a dummy variable
$category_id = '0';
}
$permalink = str_replace( '%category_id%', $category_id, $permalink );
}
return $permalink;
}

Resources