[wp-hackers] post_name_check

Alex King lists at alexking.org
Tue Apr 19 14:15:04 GMT 2005


Unfortunately, it isn't that simple. The post_name value gets the same 
"uniquifying" treatment even if you specify it manually. We need to go 
ahead and use the value in $_POST['post_name'] if it exists.

function singular_post($id) {
	global $wpdb;
	if(!isset($id)) $id = $_REQUEST['post_ID'];
	if (!empty($_POST['post_name'])) {
		$slug = sanitize_title($_POST['post_name'], $id);	}
	else {
		$post_title = $wpdb->get_var("
			SELECT post_title
			FROM {$wpdb->posts}
			WHERE ID = '{$id}'
			LIMIT 1
		");
		$slug = sanitize_title($post_title, $id);	
	}
	$wpdb->query("
		UPDATE {$wpdb->posts}
		SET post_name = '$slug'
		WHERE ID = '{$id}'
		LIMIT 1
	");
	return $id;
}

Also, I added a call to the plugin on edit as well:

add_action('publish_post', 'singular_post', 8);
add_action('edit_post', 'singular_post', 8);

Cheers,
--Alex

http://www.alexking.org/


On Apr 19, 2005, at 4:05 AM, Jamie Talbot wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Alex,
>
> Yep, you're right, there was no checking to see if a post-name had been
> specified.  Your solution is best, and only adds one line before the
> post-name replacement replacement:
>
> if ($specified = $_REQUEST['post_name']) return $id;
>
> or somesuch.  Probably don't even need the assignment statement really.
>  I think this should cover it:  Do nothing when the user has specified
> the post-name themselves and assume they know what they're doing,
> otherwise just sanitise the post title as usual.  Like you say, the
> other two solutions seem a bit of an overkill.
>
> Cheers for the info!
>
> Jamie.
>
>
> Alex King wrote:
>> Hi Jamie--
>>
>> I've run into a problem with Singular: it overwrites any selected post
>> slug with the generated slug from the post title.
>>
>> I'm looking at 3 possible solutions:
>>
>> 1. Check for $_POST['post_name'] which should still be around. (I 
>> think
>> this is best)



More information about the wp-hackers mailing list