[wp-trac] [WordPress Trac] #12240: Let plugins filter post permalink before core replacements

WordPress Trac wp-trac at lists.automattic.com
Mon Mar 1 08:04:11 UTC 2010


#12240: Let plugins filter post permalink before core replacements
--------------------------+-------------------------------------------------
 Reporter:  shidouhikari  |       Owner:  ryan                       
     Type:  enhancement   |      Status:  new                        
 Priority:  normal        |   Milestone:  3.0                        
Component:  Permalinks    |     Version:  2.9.1                      
 Severity:  normal        |    Keywords:  has-patch reporter-feedback
--------------------------+-------------------------------------------------
Changes (by shidouhikari):

 * cc: shidouhikari (added)


Comment:

 Ok, I think I'm better talking ih PHP than in english :P

 This code is only didactic, a simplified version of a plugin, I didn't
 test it but it's enough to explain my idea. The function would be hooked
 to this new filter.


 {{{
 // this filter only works if it is called before Wordpress core starts
 converting posts permastruct into a permalink
 // it searchs for the rewrite tag '%category%' and replaces it with a
 custom category slug
 function parseLink($permalink, $post) {
 $category = null;

         if (strpos($permalink, '%category%') !== false) {
                 $cat = null;

                 // retrieves a post metadata, which has a category ID
                 $category_permalink = get_post_meta($post->ID,
 '_category_permalink', true);

                 // if metadata is found, get that permalink from the
 retrieved ID
                 if ($category_permalink) $cat =
 get_category($category_permalink);

                 // if the category is found, get its slug and do the
 replacement
                 if ($cat) {
                         $category = $cat->slug;
                         if ($parent = $cat->parent){
                                 $category = get_category_parents($parent,
 false, '/', true) . $category;
                         }

                         $permalink = str_replace('%category%', $category,
 $permalink);
                 }

         }

         // if '%category%' is not part of posts permastruct, nothing had
 to be customly changed
         //if this post doesn't have the metadata, return the $permalink
 without removing the '%category%' rewrite tag, and let default Wordpress
 core deal with it
         return $permalink;
 }
 }}}


 Without the filter hook I'm suggesting, it would require a new rewrite tag
 to be done.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/12240#comment:7>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list