How to Add .html to the End of a WordPress URL and hide /product-category/ - wordpress

I use the Remove Taxonomy Base Slug plugin to hide in the url /product-category/
For example site.ru/product-category/cars/sport/bmw make site.ru/cars/sport/bmw
the plugin does a good job with it, but it's impossible to add everywhere to the end of the url .html to have site.ru/cars/sport/bmw.html instead of site.ru/product-category/cars/sport/bmw
site.ru/cars.html instead of site.ru/product-category/cars
In the settings of permanent links installed /%postname%.html
but it still does not work, it seems because of the rules of the plugin

Yes, We can hide and show category into URL with .html extension into a page. I was developing one plugin for .html URL for WordPress.
check plugin and add your comment. also, we provide support.
Plugin linn Wordpress add .htmli into custom post and category list page
Sample Code :
function jn_htmlInUrl_page_permalink() {
global $wp_rewrite;
if ( in_array( 'page', $this->selected_post_type ) ) {
if ( ! strpos( $wp_rewrite->get_page_permastruct(), '.html' ) ) {
$wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
}
}
$wp_rewrite->flush_rules();
}
if you fetching issues and comment here or plugin details page.
i will try to help resolve issues

Related

How to remove wordpress date archive pages?

I have developed my own wordpress theme, and learning all kinds of programming stuff, but my priority is the content, not the programming knownledge. I need to know how to remove archive date listing from wordpress?
Question 1: The google search results displayed like this: virmodrosti.com/2017/05/
I don't want any kind of date archive option, how do you disable that?
I also don't use any kind of plugin, and always like to do it on my own.
Question 2: I don't know why older entries doesn't work anymore
virmodrosti.com/zdravje/ this page works fine
virmodrosti.com/zdravje/page/2/ it redirects to 404 error page
I only choose option in wordpress to hide that annoying /category/ with dash . inside editor at permanlinks, Category base. Maybe somehow these stuff is kinda fighting with each other and doesn't work properly.
Thank you.
This is code from Digital Nomad theme I maintain:
function digitalnomad_remove_date_archives() {
//if we are on date archive page
if ( is_date() ) {
// theme sets alternatine archive page with table like list of all posts
$archive_page = get_option( 'digitalnomad_archive_page' );
if ( $archive_page ) {
// redirs to alternatine archive page if configured (good for SEO)
wp_redirect( esc_url( get_page_link( $archive_page ) ) );
die();
} else {
// otherwise error 404 is displayed
global $wp_query;
$wp_query->set_404();
}
}
}
add_action( 'template_redirect', 'digitalnomad_remove_date_archives' );
Use the smart Archive Page Remover wordpress plugin
or visit your theme's functions.php file
then insert this code
/* Register template redirect action callback */
add_action('template_redirect','makes_remove_wp_archives');
/* Remove archive*/
function makes_remove_wp_archives(){
// if we are on category or tag or date or author archive
if(is_category()|| is_tag()||is_author()){
global $wp_query;
$wp_query->set_404();
}
}

How to create Custom Pagination Permalinks in wordpress

I have an article with several pages in my wordpress blog. if for example i have the following link in my blog :
http://example.com/heartbreaking-photos
any idea how can i change the link of the second page from
http://example.com/heartbreaking-photos/2
to http://example.com/heartbreaking-photos/CUSTOM-STRING
CUSTOM-STRING aimed to be a custom title inside the page
To achieve this, you will need to do 2 things:
Disable the default WordPress canonical redirect - this is necessary, because WordPress will always redirect to the /2/ page when it encounters the page parameter in the URL or query args.
Add a custom rewrite rule to direct your custom title to the second page of your page - this is essentially necessary to allow the link format that you want.
In terms of code, this is what you need (this is a working solution, I've just tested it locally):
// Removes the canonical redirection
remove_filter( 'template_redirect', 'redirect_canonical' );
// Add custom rewrite rules
add_action( 'init', 'my_add_custom_rewrite_rules' );
function my_add_custom_rewrite_rules() {
// Slug of the target page
$page_slug = 'heartbreaking-photos';
// Page number to replace
$page_num = 2;
// Title you wish to replace the page number with
$title = 'custom-string';
// Add the custom rewrite rule
add_rewrite_rule(
'^' . $page_slug . '/' . $title . '/?$',
'index.php?pagename=' . $page_slug . '&page=' . $page_num, 'top'
);
}
There are three things you might want to configure or change here:
$page_slug - this is the slug of your page, in your case this is heartbreaking-photos
$page_num - the number of your pagination page, in your case this is 2
$title - the title you wish to use instead of your page number 2.
Feel free to alter the code as you wish, or copy it to cover more additional cases, similar to this one.
EDIT
Important: Once you use the code, go to Settings > Permalinks and click the "Save Changes" button. This will rebuild your rewrite rules, and is necessary for the solution to work.
Hope that helps. Let me know if you have any questions.
You can try this codex. Pass the arg and you will get page id, page title and use those
https://codex.wordpress.org/Function_Reference/get_pages
Or you can call page title by page id
$pagetitle= get_post_field( 'post_title', $page_id );
Ok, so basically you don't want to display the navigation link under the page (use css or modify the post template in the child theme) and add your custom link. If I understand it well:
Remove navigation links (depends on your theme, but basically):
.nav-links { display: none; }
You can add the custom link through function + custom fileds:
create a custom field, for example "my-url" in your post, see codex: https://codex.wordpress.org/Custom_Fields
add to your functions.php (in the child theme or in a custom site plugin):
function my_page_add_to_content( $content ) {
if ( ! empty(get_post_meta( get_the_ID(), 'my-url', true ) ) {
$content .= 'URL TEXT HERE'
}
return $content;
}
add_filter( 'the_content', 'my_page_add_to_content' );

Change Wordpress feed <link> for one specific tag

I have an external page that reads a RSS feed for a tag. I can't change anything on the external page, so the challenge is to change the RSS feed on my side to match the requirements.
On the external page the 3 latest posts for a tag are shown, and at the end of the section (note: not after each post but after all 3 posts) there is a "View all" link. This link receives its value from the element in the feed, which is by default set to my blog homepage, e.g. http://myblog.com). For this specific tag the link should be http://myblog.com/tag/myspecialtag.
The requirement is that the "View all" link links to the tag page instead of the homepage.
My idea was to add a condition to the element to change the URL for this specific category. I tried to change the feed template as recommended here: Customizing feeds, but for some reason it doesn't change the template at all. The code I tried is the following:
remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', 'change_feed_rss2', 10, 1 );
function change_feed_rss2( $for_comments ) {
$rss_template = get_template_directory() . '/feeds/feed-custom_rss2.php';
if( file_exists( $rss_template ) )
load_template( $rss_template );
else
do_feed_rss2( $for_comments ); // Call default function
}
Of course I created the custom feed template and stored it in the feeds directory in my theme.
As this didn't work I tried looking into filters/hooks but I couldn't find anything helpful regarding my issue. Any ideas on how to best solve this issue?
I came up with the following solution:
I created a new custom page template custom_rss-feed.php and copied the code from wp-includes/feed-rss.php into it. I assigned this template to a new page. Additionally I added a filter to get_bloginfo_rss like the following:
function alter_bloginfo_rss($value, $key) {
if ( $key === "url" &&
is_page_template("custom_rss-feed.php")
) {
return $value . "/my/custom/url";
}
return $value;
}
add_filter("get_bloginfo_rss", "alter_bloginfo_rss", 10, 2);

How to add page URL to list of pages in WordPress admin panel?

Currently, by default, the following columns appear in the list of pages in the WordPress admin panel:
Title
Author
Comments
Date
and because I have AIO SEO installed:
SEO Title
SEO Description
SEO Keywords
Is there a way to have WordPress also display the URL to the page (at least the part of the URL that is created when the page itself is created)?
The page url is actually already there by default, it's just hiding. When you hover over a page title, several links appear below the title -- edit, quick edit, trash, view. View is the hyperlink to the page, which you can click to view the page, or right click and copy the link address to use elsewhere.
Otherwise, if you are using a custom/child theme, you could add the following to your functions.php file:
add_filter('manage_page_posts_columns', 'my_custom_column', 10);
add_action('manage_page_posts_custom_column', 'add_my_custom_column', 10, 2);
function my_custom_column($defaults) {
$defaults['url'] = 'URL';
return $defaults;
}
function add_my_custom_column($column_name, $post_id) {
if ($column_name == 'url') {
echo get_permalink( $post_id );
}
}
Note: This just creates a text url to your page.
Also note, you do not want to edit your functions.php file directly if you are using a theme you did not create, as it will be overwritten when you update. If you want to add this to an existing theme, I'd suggest looking into child themes.
This is helpful. I would only improve the output slightly by removing the site url and just showing the page. Takes up less space and less to weed through visually.
if ($column_name == 'url') {
$siteURL=get_site_url($post_id);
$link= get_permalink( $post_id );
echo str_replace($siteURL,"",$link);
}

Remove category & tag base from WordPress url - without a plugin

I would like to remove the category & tag base from WordPress URL. I have come across other posts and solutions which used plugins. I would like to stay away from plugins and have a solution from within functions.php. This would prevent any future plugin updates or WordPress default files from being changed.
Any help would be appreciated. Thanks!
I have tried these solutions so far:
This htaccess solution did not work: http://mikepayne.co/2011/remove-category-base-from-url/
These methods also failed: http://www.askapache.com/wordpress/remove-category-wordpress-urls.html
If you want to remove /category/ from the url, follow these two steps:
Go to Settings >> Permalinks and select Custom and enter: /%category%/%postname%/
Next set your Category Base to .
Save it and you’ll see your URL changed to this format:
http://yourblog.com/quotes/
(Source: http://premium.wpmudev.org/blog/daily-tip-quick-trick-to-remove-category-from-wordpress-url/)
If you use Yoast SEO plugin just go to:
Search Appearance > Taxonomies > Category URLs.
And select remove from Strip the category base (usually /category/) from the category URL.
Regarding the tag removal I did not found any solution yet.
Whilst you dismiss it as a solution, the plugin is by far the easiest and most consistent method and they don't change any WordPress default files.
http://wordpress.org/plugins/wp-no-category-base/
It hasn't needed to be updated for a year, so it is not exactly creating any problems with updates.
There is no simple hand rolled solution that will do all of this that does not just replicate what the plugin does from within your own functions.php
Better and logical permalinks like myblog.com/my-category/ and myblog.com/my-category/my-post/.
Simple plugin - barely adds any overhead.
Works out of the box - no setup needed. No need to modify
WordPress files.
Doesn't require other plugins to work.
Compatible with sitemap plugins.
Works with multiple sub-categories.
Works with WordPress Multisite.
Redirects old category permalinks to the new ones (301 redirect, good for SEO).
Plus you get the benefit that if WordPress does change, then the plugin will be updated to work whilst you would then have to figure out how to fix your own code on your own.
Set Custom Structure: /%postname%/
Set Category base: . (dot not /)
Save. 100% work correctly.
instead put this in your functions.php
works fine, no redirect problems.
function fix_slash( $string, $type )
{
global $wp_rewrite;
if ( $wp_rewrite->use_trailing_slashes == false )
{
if ( $type != 'single' && $type != 'category' )
return trailingslashit( $string );
if ( $type == 'single' && ( strpos( $string, '.html/' ) !== false ) )
return trailingslashit( $string );
if ( $type == 'category' && ( strpos( $string, 'category' ) !== false ) )
{
$aa_g = str_replace( "/category/", "/", $string );
return trailingslashit( $aa_g );
}
if ( $type == 'category' )
return trailingslashit( $string );
}
return $string;
}
add_filter( 'user_trailingslashit', 'fix_slash', 55, 2 );
The dot trick will likely ruin your rss feeds and/or pagination. These work, though:
add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
function no_category_base_rewrite_rules($category_rewrite) {
$category_rewrite=array();
$categories=get_categories(array('hide_empty'=>false));
foreach($categories as $category) {
$category_nicename = $category->slug;
if ( $category->parent == $category->cat_ID )
$category->parent = 0;
elseif ($category->parent != 0 )
$category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
$category_rewrite['('.$category_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$category_rewrite['('.$category_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
$category_rewrite['('.$category_nicename.')/?$'] = 'index.php?category_name=$matches[1]';
}
global $wp_rewrite;
$old_base = $wp_rewrite->get_category_permastruct();
$old_base = str_replace( '%category%', '(.+)', $old_base );
$old_base = trim($old_base, '/');
$category_rewrite[$old_base.'$'] = 'index.php?category_redirect=$matches[1]';
return $category_rewrite;
}
// remove tag base
add_filter('tag_rewrite_rules', 'no_tag_base_rewrite_rules');
function no_tag_base_rewrite_rules($tag_rewrite) {
$tag_rewrite=array();
$tags=get_tags(array('hide_empty'=>false));
foreach($tags as $tag) {
$tag_nicename = $tag->slug;
if ( $tag->parent == $tag->tag_ID )
$tag->parent = 0;
elseif ($tag->parent != 0 )
$tag_nicename = get_tag_parents( $tag->parent, false, '/', true ) . $tag_nicename;
$tag_rewrite['('.$tag_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?tag=$matches[1]&feed=$matches[2]';
$tag_rewrite['('.$tag_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?tag=$matches[1]&paged=$matches[2]';
$tag_rewrite['('.$tag_nicename.')/?$'] = 'index.php?tag=$matches[1]';
}
global $wp_rewrite;
$old_base = $wp_rewrite->get_tag_permastruct();
$old_base = str_replace( '%tag%', '(.+)', $old_base );
$old_base = trim($old_base, '/');
$tag_rewrite[$old_base.'$'] = 'index.php?tag_redirect=$matches[1]';
return $tag_rewrite;
}
// remove author base
add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules');
function no_author_base_rewrite_rules($author_rewrite) {
global $wpdb;
$author_rewrite = array();
$authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users");
foreach($authors as $author) {
$author_rewrite["({$author->nicename})/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$"] = 'index.php?author_name=$matches[1]&feed=$matches[2]';
$author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
$author_rewrite["({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';
}
return $author_rewrite;}
add_filter('author_link', 'no_author_base', 1000, 2);
function no_author_base($link, $author_id) {
$link_base = trailingslashit(get_option('home'));
$link = preg_replace("|^{$link_base}author/|", '', $link);
return $link_base . $link;
}
The non-category plugin did not work for me.
For Multisite WordPress the following works:
Go to network admin sites;
Open site under \;
Go to settings;
Under permalinks structure type /%category%/%postname%/.
This will display your url as www.domainname.com/categoryname/postname;
Now go to your site dashboard (not network dashboard);
Open settings;
Open permalink. Do not save (the permalink will show uneditable field as yourdoamainname/blog/. Ignore it. If you save now the work you did in step 4 will be overwritten. This step of opening permalink page but not saving in needed to update the database.
If you're still searching for the combination (tags, categories and pages on the url-base), you can do it like I did.
Open the settings for permalinks and set a dot (.) for the category- and tag-base (https://premium.wpmudev.org/blog/removing-category-base-urls-wordpress/)
Install the plugin wp-no-tag-base
Tested using Wordpress 3.9.1
If you have pages, categories or tags having the same name, the system will take:
tag
page
category
https://wordpress.org/plugins/remove-category-url/
Use this plugin it does the job perfectly of hiding the category-base
It does not require any setting just install and activate.
Select Custom Structure in permalinks and add /%category%/%postname%/ after your domain. Adding "/" to the category base doesn't work, you have to add a period/dot. I wrote a tutorial for this here: remove category from URL tutorial
I don´t know how to do it using code, but for those who don't mind using a plugin. This is a great one that works for me:
https://es.wordpress.org/plugins/permalink-manager/
Modifying WP core files doesn't seems to be a solution for removing the category prefix. Also the "." fix via the Permalinks doesn't seems to work.
I believe its better to have this set via Yoast SEO plugin or Rank Math SEO Plugin, hoping almost all WordPress sites have either one of this plugin for SEO purpose.
No complicated steps, just a few mouse clicks and eventually forget about it.
If you are using Yoast SEO Plugin,
Yoast SEO > Search Appearance > Taxonomies
If you are using Rankmath SEO Plugin,
Rankmath > General Settings > Strip Category Base
And here is a dedicated plugin that meets the purpose: https://wordpress.org/plugins/remove-category-url/ , if that helps someone.
updated answer:
other solution:
In wp-includes/rewrite.php file, you'll see the code:
$this->category_structure = $this->front . 'category/';
just copy whole function, put in your functions.php and hook it. just change the above line with:
$this->category_structure = $this->front . '/';
add_action( 'init', 'remove_category_perma' );
function remove_category_perma() {
unset($GLOBALS['wp_rewrite']->extra_permastructs['category']);
}
WordPress 5.0.2:
To remove category slug from existing posts, do this :
Navigate to Settings > Permalinks and change Custom Structure from /%category%/%postname%/ to: /%postname%/
Keep Category and Tag bases empty (which is the default also)
Save
All posts can now be directly accessed via domain.com/%postname%/ and all categories can be accessed via domain.com/category/xyz/. WordPress will automatically add all the 301 redirects for the old urls. So, if someone accesses domain.com/%category%/%postname%/, they will automatically get redirected to domain.com/%postname%/.
Adding "." or "/" won't work if you want a consolidated blog view. Also, I have know idea what that solutions would do for the RSS or XML feeds. I feel better sticking with the WP convention. However, I did come up with a more elegant approach.
First, I name the base category url "blog"
Then I created a category called "all". Finally, I but all my subcategories under "all". So I get a url structure like this.
/blog - 404 - recommend 301 redirect to /blog/all/
/blog/all/ - all posts combined.
/blog/all/category1/ - posts filtered by category1
/blog/all/category2/ - posts filterer by category2
I put a custom label on the menu item called "Blog", but it goes to blog/all. It would be a good idea to 301 redirect /blog to /blog/all in the .htaccess file to avoid the 404 on /blog.

Resources