Yeshuahin Alternative Post URLs

By yeshuahin

0+ active installationsVersion 1.0

Gives every post an alternative, shorter URL made of its main category and its post ID, for example /news/123/.

Description

Long post titles make long links. Yeshuahin Alternative Post URLs gives every post an alternative, shorter URL that is easy to copy, share and print, while the normal permalink keeps working. The short URL contains only the post’s main category and its post ID:

https://example.com/bangkok/20/

The URL stays valid when the post title or slug changes, because the ID never changes.

Features

  • Works alongside your existing permalinks, with no redirect: the short URL shows the post directly and the normal permalink is used as the canonical URL for search engines.
  • Uses the primary category set in Yoast SEO, Rank Math or SEOPress. Without one it uses the post’s first category.
  • Shows the alternative URL in a new “Alt URL” column in the Posts list, with a Copy button, and in a box in the post editor.
  • Shortcode [alt_post_url] prints the alternative URL of the current post, or of a given post with [alt_post_url id="123"].
  • No rewrite rules and no settings. It never needs a permalink flush and will not take over pages, date archives or other URLs: it only answers when the first part is an existing category and the second is the ID of an existing post.
  • If permalinks are set to Plain, the alternative URL has the form /?category_name=bangkok&p=20.

For developers

  • yhh_altpost_get_url( $post ) returns the alternative URL of a post.
  • yhh_altpost_main_category filter changes the category used in the URL.
  • yhh_altpost_redirect_to_permalink filter, return true to redirect the alternative URL to the normal permalink.

Installation

  1. Upload the yeshuahin-alternative-post-urls folder to /wp-content/plugins/, or install the zip through Plugins > Add New.
  2. Activate the plugin through the Plugins screen.
  3. Open Posts to copy each post’s alternative URL from the “Alt URL” column.

For the /category/id/ form, set Settings > Permalinks to any option other than Plain.

FAQ

What if a post has more than one category?

One category is shown: the primary category from Yoast SEO, Rank Math or SEOPress if set, otherwise the first category alphabetically (the default category is only used if it is the only one). The URL also works with any other existing category slug.

Does it create duplicate content?

No. The normal permalink is still the canonical URL of the post.

Does it work with pages or custom post types?

No, only standard posts.

Changelog

1.0

  • First release.