You shouldn't have to be an expert to be safe on a computer.

Home

Guides

Seminars

Tips and Tricks

Log in

Hack WordPress to Make Postnames Always Editable

Saturday, November 7th, 2009 (No comments yet)
We want to have this postname control no matter what kind of permalink style we actually have.
We want to have this postname control no matter what kind of permalink style we actually have.

There's probably a good reason that the WordPress development team took away our ability to modify the page postname (aka slug) when we don't have pretty permalinks active… Yes, a very good reason…

Well forget them! I want to be able to modify a postname no matter what. My development machine doesn't work with pretty permalinks so if I can't edit the postname directly in the edit screen, I have to go straight to the database to do it.

Why do I care? Well a lot of my custom code and heirarchy depends on the postnames. For example, how am I supposed to find all pages that are the child of the page named "Parent" if I can't name the damned page!?

Anyway… enough ranting. Here's the simple hack I found for fixing this vexing issue:

WARNING! I am not a WordPress developer and I have no idea at all if this hack will affect anything else negatively. For all I know, it could trigger nuclear war. Use at your own risk.

  1. Open the "post.php" file in wp-admin\includes
  2. Find these lines:
    1
    2
    3
    4
    5
    6
    
    function get_sample_permalink_html($id, $new_title=null, $new_slug=null) {
    	$post = &get_post($id);
    	list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
    	if (false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%')) {
    		return '';
    	}

    Can you see the problem yet? It tests for your permalink style and if it's not "pretty", it dumps out of the function. Easy to fix:

  3. Change it by adding one line as shown here:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    function get_sample_permalink_html($id, $new_title=null, $new_slug=null) {
    	$post = &get_post($id);
    	list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
     
    	// Add this line right here!
    	$permalink = get_bloginfo("url")."/%postname%";
     
    	if (false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%')) {
    		return '';
    	}
  4. That's it! What we're doing is tricking wordpress into thinking that the page ALWAYS uses pretty permalinks. The rest of the code for editing the postname triggers and viola!

Support the Geek

If you hate ads as much as I do, please consider supporting us by donating or browsing our recommended products


Recommended Products and Services
Quick Tips:

Jsearch - Wordpress Multiple Category, Tag, and Term Search

Jsearch, the most complete search plugin available for Wordpress. Handles multiple categories, tags, and keywords all at the same time. Widget enabled!

[Click for full description]

Get File Path to Wordpress Theme

Here's a tip to get the file path to your current Wordpress theme.

[Click for full description]

Hack Wordpress to Make Postnames Always Editable

When using ordinary permalinks, you're blocked from editing the postname when doing posts and pages. This is senseless and must be stopped!

[Click for full description]