[wp-hackers] url_to_postid() improvements

Mark Jaquith mark.wordpress at txfx.net
Mon Jun 27 11:03:24 GMT 2005


As many people have noticed, url_to_postid() fails in many cases.  It 
creates false positives (suggesting a post ID for URIs that are not 
posts) and false negatives (failing on legit post URIs).

I spent some time identifying the cases that fail and patching the 
url_to_postid() function to handle them.

Some significant changes have been made in 1.6 SVN to the 
url_to_postid() function, but it still fails in many cases.  I also 
patched in my changes to that version of the function, but still, it 
fails many tests.

I've set up a page for testing of the 4 functions (1.5.1.2 version, 
1.5.1.2 patched by me, 1.6 SVN version, and 1.6 SVN patched by me).

http://txfx.net/wordpress/url-to-postid.php

You can enter in URIs to test, but I've put down 6 examples of common 
URIs that fail.  See the results for yourself at the link above, but 
here's the summary:

1. http://txfx.net/code/?foo=bar is a valid link to a page but with some 
junk thrown at the end.  Both patched versions get this right.
2. http://txfx.net/code/#random-anchor is a valid link to a page but 
with a junk anchor at the end.  Both patched versions get this right.
3. http://txfx.net/page/23 is a link to the 23rd page of the front-page 
view, not a post.  It should return 0.  Only my patched 1.5.1.2 version 
gets this right.
4. http://www.txfx.net/code/ is a valid link to a page, but with "www." 
inserted.  Only my patched 1.5.1.2 version gets this right.
5. http://txfx.net/2005/02/16/ is a link to a day view, not a post.  It 
should return 0.  Only my patched 1.5.1.2 version gets this right.
6. http://txfx.net/category/internet/ is a link to a category.  It 
should return 0.  Only my patched 1.5.1.2 version gets this right.

The patched version of the 1.5.1.2 version of the function is the only 
one to get them all right.  Included is the PHP file I used for 
testing.  Inside are my patched versions, my_url_to_postid() (the 
1.5.1.2 patched version) and my_wp16_url_to_postid() (1.6 SVN patched 
version).

I don't know if the 1.6 version can be modified to handle all 6 cases, 
and if not, it might be best to use my patched version of the 1.5.1.2 
function.

I'd appreciate feedback on this, especially from Ryan Boren, who checked 
in the changes for 1.6

- Mark Jaquith
MCincubus on #wordpress
-------------- next part --------------
A non-text attachment was scrubbed...
Name: url-to-postid.php
Type: application/atom
Size: 9705 bytes
Desc: not available
Url : http://comox.textdrive.com/pipermail/wp-hackers/attachments/20050627/01d6f8a6/url-to-postid.bin


More information about the wp-hackers mailing list