├── .gitignore ├── README.md ├── allow-numeric-stubs.php └── readme.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .svn 2 | .idea -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Allow Numeric Slugs 2 | 3 | A WordPress plugin that allows pages to have a slug (URL) that is only a number. 4 | 5 | Normally is not possible for a page to have a URL like `yoursite.com/about/2/` (a page called "2" that's a child of "About"). That URL conflicts with paged content feature where you can posts and pages with multiple pages of content by adding `` within your content. 6 | 7 | This plugin allows you to have pages with numbers as slugs by giving up the ability to have paged content pages which isn't a big deal as most people don't use paged content pages anyway. 8 | -------------------------------------------------------------------------------- /allow-numeric-stubs.php: -------------------------------------------------------------------------------- 1 | <!--nextpage--> ability in pages to accomplish it. 8 | Version: 3.0.0 9 | Author: Viper007Bond 10 | Author URI: http://www.viper007bond.com/ 11 | 12 | ************************************************************************** 13 | 14 | Copyright (C) 2008-2016 Viper007Bond 15 | 16 | This program is free software: you can redistribute it and/or modify 17 | it under the terms of the GNU General Public License as published by 18 | the Free Software Foundation, either version 3 of the License, or 19 | (at your option) any later version. 20 | 21 | This program is distributed in the hope that it will be useful, 22 | but WITHOUT ANY WARRANTY; without even the implied warranty of 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 | GNU General Public License for more details. 25 | 26 | You should have received a copy of the GNU General Public License 27 | along with this program. If not, see . 28 | 29 | **************************************************************************/ 30 | 31 | class Allow_Numeric_Stubs { 32 | 33 | /** 34 | * Allow_Numeric_Stubs constructor. Registers the plugin's hooks. 35 | */ 36 | function __construct() { 37 | // Flush rewrite rules on plugin activation 38 | register_activation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ); 39 | 40 | // Modify the rewrite rules for pages to swap in numeric slug support instead of paging 41 | add_filter( 'page_rewrite_rules', array( $this, 'page_rewrite_rules' ) ); 42 | 43 | // Filter the result of wp_unique_post_slug() to allow numeric slugs for pages 44 | add_filter( 'wp_unique_post_slug', array( $this, 'wp_unique_post_slug_allow_numeric_page_slugs' ), 10, 6 ); 45 | } 46 | 47 | /** 48 | * Flush out WordPress's cached rewrite rules so that the modifications this plugin makes take effect. 49 | */ 50 | function flush_rewrite_rules() { 51 | global $wp_rewrite; 52 | $wp_rewrite->flush_rules(); 53 | } 54 | 55 | /** 56 | * Remove the rewrite rule that prevents numeric slugs from working (paged content) 57 | * and replace it with a new rule that allows them to. 58 | * 59 | * @param array $rules The existing rewrite rules for pages. 60 | * 61 | * @return array The modified rewrite rules for pages. 62 | */ 63 | function page_rewrite_rules( $rules ) { 64 | unset( $rules['(.?.+?)(/[0-9]+)?/?$'] ); // Before WordPress 4.4 65 | unset( $rules['(.?.+?)(?:/([0-9]+))?/?$'] ); // After WordPress 4.4, see https://core.trac.wordpress.org/changeset/34492 66 | 67 | $rules['(.?.+?)?/?$'] = 'index.php?pagename=$matches[1]'; 68 | 69 | return $rules; 70 | } 71 | 72 | /** 73 | * Undoes the work of wp_unique_post_slug() for pages with a numeric slug, 74 | * but only if they don't conflict with any existing sibling pages. 75 | * 76 | * @since 3.0.0 77 | * 78 | * @param string $slug The slug that wp_unique_post_slug() suggests using. 79 | * @param int $post_ID The post (page) ID that the slug belongs to. 80 | * @param string $post_status The status of post (page) that the slug belongs to. 81 | * @param string $post_type The post_type of the post that we're currently filtering. Aborts for everything but "page". 82 | * @param int $post_parent Post parent ID. 83 | * @param string $original_slug The originally requested slug, which may or may not be unique. 84 | */ 85 | public function wp_unique_post_slug_allow_numeric_page_slugs( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) { 86 | global $wpdb; 87 | 88 | // We're only interested in pages with attempted numeric slugs that got changed 89 | if ( 'page' != $post_type || ! is_numeric( $original_slug ) || $slug === $original_slug ) { 90 | return $slug; 91 | } 92 | 93 | // Was there actually a conflict or was a suffix just added due to the preg_match() call in wp_unique_post_slug() ? 94 | $post_name_check = $wpdb->get_var( $wpdb->prepare( 95 | "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1", 96 | $original_slug, $post_type, $post_ID, $post_parent 97 | ) ); 98 | 99 | // There really is a conflict due to an existing page so keep the modified slug 100 | if ( $post_name_check ) { 101 | return $slug; 102 | } 103 | 104 | // Otherwise give us the slug we wanted 105 | return $original_slug; 106 | } 107 | } 108 | 109 | $GLOBALS['Allow_Numeric_Stubs'] = new Allow_Numeric_Stubs(); -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Allow Numeric Slugs === 2 | Contributors: Viper007Bond 3 | Donate link: http://www.viper007bond.com/donate/ 4 | Tags: page, pages, numeric, number 5 | Requires at least: 3.3 6 | Tested up to: 4.4 7 | Stable tag: trunk 8 | 9 | Allows Pages to have a slug (URL) that is only a number. Sacrifices the paged content ability in Pages to accomplish it. 10 | 11 | == Description == 12 | 13 | Normally it is not possible to have a page slug (the page's name in the URL) that is a number. For example this will not work: `yoursite.com/about/2/`. That URL conflicts with paged content feature where you can posts and pages with multiple pages of content by adding `` within your content. 14 | 15 | This plugin allows you to have pages with numbers as slugs by giving up the ability to have paged content pages which isn't a big deal as most people don't use paged content pages anyway. 16 | 17 | Code contributions and bug reports are welcome via [this plugin's GitHub repository](https://github.com/Viper007Bond/allow-numeric-stubs). 18 | 19 | == Installation == 20 | 21 | 1. Go to your admin area and select Plugins → Add New from the menu. 22 | 2. Search for "Allow Numeric Slugs". 23 | 3. Click install. 24 | 4. Click activate. 25 | 26 | == ChangeLog == 27 | 28 | = Version 3.0.0 = 29 | 30 | * Ditch all of the old fragile hackery of this plugin in favor of just hooking into the WordPress function that ensures that slugs are unique. It's what was adding the `-2` suffix in the first place. 31 | 32 | = Version 2.2.0 = 33 | 34 | * Update for WordPress 4.4's rewrite rules. 35 | * PHP 7 compatibility by renaming class constructor. Also drops unneeded references. 36 | 37 | = Version 2.1.0 = 38 | 39 | * Update for WordPress 3.3's rewrite rules. 40 | 41 | = Version 2.0.1 = 42 | 43 | * Re-add the `save_post` filter after fixing the slug incase multiple posts are updated in one pageload. 44 | 45 | = Version 2.0.0 = 46 | 47 | * Recoded for WordPress 3.0+. WordPress now won't let you manually enter a numeric slug -- it will prefix "-2" onto the end of it so that the page is viewable. This new plugin version works around it. 48 | 49 | = Version 1.0.0 = 50 | 51 | * Initial release. --------------------------------------------------------------------------------