Plugin Directory

Changeset 469102


Ignore:
Timestamp:
11/29/2011 02:34:33 PM (14 years ago)
Author:
michaeltyson
Message:

Added support for custom post types, thanks to Balázs Németh
Tagged v0.7.9

Location:
custom-permalinks
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • custom-permalinks/trunk/custom-permalinks.php

    r462757 r469102  
    55Donate link: http://atastypixel.com/blog/wordpress/plugins/custom-permalinks/
    66Description: Set custom permalinks on a per-post basis
    7 Version: 0.7.8
     7Version: 0.7.9
    88Author: Michael Tyson
    99Author URI: http://atastypixel.com/blog
     
    107107        $post = $wp_query->post;
    108108        $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
    109         $original_permalink = ( $post->post_type == 'post' ? custom_permalinks_original_post_link( $post->ID ) : custom_permalinks_original_page_link( $post->ID ) );
     109        $original_permalink = ( $post->post_type == 'page' ? custom_permalinks_original_page_link( $post->ID ) : custom_permalinks_original_post_link( $post->ID ) );
    110110    } else if ( is_tag() || is_category() ) {
    111111        $theTerm = $wp_query->get_queried_object();
     
    159159    if ( !$request ) return $query;
    160160   
    161     $sql = "SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts ".
    162                 " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
    163                 "   meta_key = 'custom_permalink' AND ".
    164                 "   meta_value != '' AND ".
    165                 "   ( meta_value = LEFT('".mysql_escape_string($request_noslash)."', LENGTH(meta_value)) OR ".
    166                 "     meta_value = LEFT('".mysql_escape_string($request_noslash."/")."', LENGTH(meta_value)) ) ".
    167                 " ORDER BY LENGTH(meta_value) DESC";
    168    
     161    $sql = "SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts  ".
     162                "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
     163                "  meta_key = 'custom_permalink' AND ".
     164                "  meta_value != '' AND ".
     165                "  wp_posts.post_status = 'publish' AND ".
     166                "  ( meta_value = LEFT('".mysql_escape_string($request_noslash)."', LENGTH(meta_value)) OR ".
     167                "    meta_value = LEFT('".mysql_escape_string($request_noslash."/")."', LENGTH(meta_value)) ) ".
     168                "ORDER BY LENGTH(meta_value) DESC";
     169
    169170    $posts = $wpdb->get_results($sql);
    170171   
     
    176177            $_CPRegisteredURL = $request;
    177178               
    178         $originalUrl =  preg_replace('@/+@', '/', str_replace( trim($posts[0]->meta_value,'/'),
    179                                    ( $posts[0]->post_type == 'post' ?
    180                                             custom_permalinks_original_post_link($posts[0]->ID)
    181                                             : custom_permalinks_original_page_link($posts[0]->ID) ),
    182                                    $request_noslash ));
     179        $originalUrl =  preg_replace( '@/+@', '/', str_replace( trim( $posts[0]->meta_value,'/' ),
     180                                    ( $posts[0]->post_type == 'page' ?
     181                                            custom_permalinks_original_page_link($posts[0]->ID)
     182                                            : custom_permalinks_original_post_link($posts[0]->ID) ),
     183                                   $request_noslash ) );
    183184    }
    184185   
     
    280281    ob_start();
    281282    ?>
    282     <?php custom_permalinks_form($permalink, ($post->post_type == "post" ? custom_permalinks_original_post_link($id) : custom_permalinks_original_page_link($id)), false); ?>
     283    <?php custom_permalinks_form($permalink, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false); ?>
    283284    <?php
    284285    $content = ob_get_contents();
     
    286287   
    287288    if ( 'publish' == $post->post_status ) {
    288         $view_post = 'post' == $post->post_type ? __('View Post') : __('View Page');
     289        $view_post = 'page' == $post->post_type ? __('View Page') : __('View Post');
    289290    }
    290291   
     
    619620    // List posts/pages
    620621    global $wpdb;
    621     $query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
    622                 "$wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != ''";
     622    $query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE
     623            $wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != '';";
    623624    $posts = $wpdb->get_results($query);
    624625    foreach ( $posts as $post ) {
     
    643644 */
    644645function custom_permalinks_original_post_link($post_id) {
    645     remove_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
    646     $originalPermalink = ltrim(str_replace(get_home_url(), '', get_permalink( $post_id )), '/');
    647     add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
     646    remove_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); // original hook
     647    remove_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
     648    $originalPermalink = ltrim(str_replace(get_option('home'), '', get_permalink( $post_id )), '/');
     649    add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); // original hook
     650    add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
    648651    return $originalPermalink;
    649652}
     
    728731add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
    729732add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
     733add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
    730734add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
    731735add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
  • custom-permalinks/trunk/readme.txt

    r463703 r469102  
    2929
    3030== Changelog ==
     31
     32= 0.7.9 =
     33
     34 * Support for custom post types, by Balázs Németh
    3135
    3236= 0.7.8 =
Note: See TracChangeset for help on using the changeset viewer.