|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Plugin Name: Hashed Permalinks |
| 4 | + * Plugin URI: https://wolfdevs.com/plugins/hashed-permalinks/ |
| 5 | + * Description: This plugin will add a unique hash to the end of your post permalinks. Use %posthash% in your permalink structure to add the hash. |
| 6 | + * Version: 1.0 |
| 7 | + * Author: WolfDevs |
| 8 | + * Author URI: https://wolfdevs.com/ |
| 9 | + * License: GNU General Public License v3.0 |
| 10 | + * License URI: http://www.gnu.org/licenses/gpl-3.0.html |
| 11 | + */ |
| 12 | + |
| 13 | +function hashed_post_name($permalink, $post_id, $leavename) { |
| 14 | + if (strpos($permalink, '%posthash%') === FALSE) { |
| 15 | + return $permalink; |
| 16 | + } |
| 17 | + |
| 18 | + $post = get_post($post_id); |
| 19 | + if (!$post) { |
| 20 | + return $permalink; |
| 21 | + } |
| 22 | + |
| 23 | + // Hash the post title |
| 24 | + $hashed_post_title = hash('sha256', $post->post_title); |
| 25 | + |
| 26 | + // Shorten the hash to 6 characters |
| 27 | + $short_hash = substr($hashed_post_title, 0, 6); |
| 28 | + |
| 29 | + // Concatenate the post ID with the shortened hash |
| 30 | + $unique_hash = $post->ID . $short_hash; |
| 31 | + return str_replace('%posthash%', $unique_hash, $permalink); |
| 32 | +} |
| 33 | +add_filter('post_link', 'hashed_post_name', 10, 3); |
| 34 | +add_filter('post_type_link', 'hashed_post_name', 10, 3); |
| 35 | +add_filter('page_link', 'hashed_post_name', 10, 3); // Apply the function to pages as well |
| 36 | + |
| 37 | +function custom_rewrite_tag() { |
| 38 | + add_rewrite_tag('%posthash%', '([^&]+)'); |
| 39 | +} |
| 40 | +add_action('init', 'custom_rewrite_tag', 10, 0); |
| 41 | + |
| 42 | +function custom_rewrite_rules() { |
| 43 | + flush_rewrite_rules(); |
| 44 | +} |
| 45 | +register_activation_hook(__FILE__, 'custom_rewrite_rules'); |
0 commit comments