This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ /** ** Actions and filters ** **/ /** * Filter to replace the post permalink with the custom one * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_post_link($permalink, $post) { $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true ); if ( $custom_permalink ) { return get_home_url()."/".$custom_permalink; } return $permalink; } /** * Filter to replace the page permalink with the custom one * * @package CustomPermalinks * @since 0.4 */ function custom_permalinks_page_link($permalink, $page) { $custom_permalink = get_post_meta( $page, 'custom_permalink', true ); if ( $custom_permalink ) { return get_home_url()."/".$custom_permalink; } return $permalink; } /** * Filter to replace the term permalink with the custom one * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_term_link($permalink, $term) { $table = get_option('custom_permalink_table'); if ( is_object($term) ) $term = $term->term_id; $custom_permalink = custom_permalinks_permalink_for_term($term); if ( $custom_permalink ) { return get_home_url()."/".$custom_permalink; } return $permalink; } /** * Action to redirect to the custom permalink * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_redirect() { // Get request URI, strip parameters $url = parse_url(get_bloginfo('url')); $url = isset($url['path']) ? $url['path'] : ''; $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/'); if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos); global $wp_query; $custom_permalink = ''; $original_permalink = ''; // If the post/tag/category we're on has a custom permalink, get it and check against the request if ( is_single() || is_page() ) { $post = $wp_query->post; $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true ); $original_permalink = ( $post->post_type == 'page' ? custom_permalinks_original_page_link( $post->ID ) : custom_permalinks_original_post_link( $post->ID ) ); } else if ( is_tag() || is_category() ) { $theTerm = $wp_query->get_queried_object(); $custom_permalink = custom_permalinks_permalink_for_term($theTerm->term_id); $original_permalink = (is_tag() ? custom_permalinks_original_tag_link($theTerm->term_id) : custom_permalinks_original_category_link($theTerm->term_id)); } if ( $custom_permalink && (substr($request, 0, strlen($custom_permalink)) != $custom_permalink || $request == $custom_permalink."/" ) ) { // Request doesn't match permalink - redirect $url = $custom_permalink; if ( substr($request, 0, strlen($original_permalink)) == $original_permalink && trim($request,'/') != trim($original_permalink,'/') ) { // This is the original link; we can use this url to derive the new one $url = preg_replace('@//*@', '/', str_replace(trim($original_permalink,'/'), trim($custom_permalink,'/'), $request)); $url = preg_replace('@([^?]*)&@', '\1?', $url); } // Append any query compenent $url .= strstr($_SERVER['REQUEST_URI'], "?"); wp_redirect( get_home_url()."/".$url, 301 ); exit(); } } /** * Filter to rewrite the query if we have a matching post * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_request($query) { global $wpdb; global $_CPRegisteredURL; // First, search for a matching custom permalink, and if found, generate the corresponding // original URL $originalUrl = NULL; // Get request URI, strip parameters and /'s $url = parse_url(get_bloginfo('url')); $url = isset($url['path']) ? $url['path'] : ''; $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/'); $request = (($pos=strpos($request, '?')) ? substr($request, 0, $pos) : $request); $request_noslash = preg_replace('@/+@','/', trim($request, '/')); if ( !$request ) return $query; $sql = "SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts ". "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ". " meta_key = 'custom_permalink' AND ". " meta_value != '' AND ". " ( LOWER(meta_value) = LEFT(LOWER('".mysql_escape_string($request_noslash)."'), LENGTH(meta_value)) OR ". " LOWER(meta_value) = LEFT(LOWER('".mysql_escape_string($request_noslash."/")."'), LENGTH(meta_value)) ) ". "ORDER BY LENGTH(meta_value) DESC LIMIT 1"; $posts = $wpdb->get_results($sql); if ( $posts ) { // A post matches our request // Preserve this url for later if it's the same as the permalink (no extra stuff) if ( $request_noslash == trim($posts[0]->meta_value,'/') ) $_CPRegisteredURL = $request; $originalUrl = preg_replace( '@/+@', '/', str_replace( trim( strtolower($posts[0]->meta_value),'/' ), ( $posts[0]->post_type == 'page' ? custom_permalinks_original_page_link($posts[0]->ID) : custom_permalinks_original_post_link($posts[0]->ID) ), strtolower($request_noslash) ) ); } if ( $originalUrl === NULL ) { // See if any terms have a matching permalink $table = get_option('custom_permalink_table'); if ( !$table ) return $query; foreach ( array_keys($table) as $permalink ) { if ( $permalink == substr($request_noslash, 0, strlen($permalink)) || $permalink == substr($request_noslash."/", 0, strlen($permalink)) ) { $term = $table[$permalink]; // Preserve this url for later if it's the same as the permalink (no extra stuff) if ( $request_noslash == trim($permalink,'/') ) $_CPRegisteredURL = $request; if ( $term['kind'] == 'category') { $originalUrl = str_replace(trim($permalink,'/'), custom_permalinks_original_category_link($term['id']), trim($request,'/')); } else { $originalUrl = str_replace(trim($permalink,'/'), custom_permalinks_original_tag_link($term['id']), trim($request,'/')); } } } } if ( $originalUrl !== NULL ) { $originalUrl = str_replace('//', '/', $originalUrl); if ( ($pos=strpos($_SERVER['REQUEST_URI'], '?')) !== false ) { $queryVars = substr($_SERVER['REQUEST_URI'], $pos+1); $originalUrl .= (strpos($originalUrl, '?') === false ? '?' : '&') . $queryVars; } // Now we have the original URL, run this back through WP->parse_request, in order to // parse parameters properly. We set $_SERVER variables to fool the function. $oldRequestUri = $_SERVER['REQUEST_URI']; $oldQueryString = $_SERVER['QUERY_STRING']; $_SERVER['REQUEST_URI'] = '/'.ltrim($originalUrl,'/'); $_SERVER['QUERY_STRING'] = (($pos=strpos($originalUrl, '?')) !== false ? substr($originalUrl, $pos+1) : ''); parse_str($_SERVER['QUERY_STRING'], $queryArray); $oldValues = array(); if ( is_array($queryArray) ) foreach ( $queryArray as $key => $value ) { $oldValues[$key] = $_REQUEST[$key]; $_REQUEST[$key] = $_GET[$key] = $value; } // Re-run the filter, now with original environment in place remove_filter( 'request', 'custom_permalinks_request', 10, 1 ); global $wp; $wp->parse_request(); $query = $wp->query_vars; add_filter( 'request', 'custom_permalinks_request', 10, 1 ); // Restore values $_SERVER['REQUEST_URI'] = $oldRequestUri; $_SERVER['QUERY_STRING'] = $oldQueryString; foreach ( $oldValues as $key => $value ) { $_REQUEST[$key] = $value; } } return $query; } /** * Filter to handle trailing slashes correctly * * @package CustomPermalinks * @since 0.3 */ function custom_permalinks_trailingslash($string, $type) { global $_CPRegisteredURL; $url = parse_url(get_bloginfo('url')); $request = ltrim(substr($string, strlen($url['path'])),'/'); if ( !trim($request) ) return $string; if ( trim($_CPRegisteredURL,'/') == trim($request,'/') ) { return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL; } return $string; } /** ** Administration ** **/ /** * Per-post/page options (Wordpress > 2.9) * * @package CustomPermalinks * @since 0.6 */ function custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new_slug) { $permalink = get_post_meta( $id, 'custom_permalink', true ); $post = &get_post($id); if ( preg_match("@view-post-btn.*?href='([^']+)'@s", $html, $matches) ) { $permalink = $matches[1]; } else { list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug); if ( false !== strpos($permalink, '%postname%') || false !== strpos($permalink, '%pagename%') ) { $permalink = str_replace(array('%pagename%','%postname%'), $post_name, $permalink); } } ob_start(); custom_permalinks_form($permalink, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false, $post->ID); $content = ob_get_contents(); ob_end_clean(); if ( 'publish' == $post->post_status ) { $view_post = 'page' == $post->post_type ? __('View Page') : __('View Post'); } return '' . __('Permalink:') . "\n" . $content . ( isset($view_post) ? "$view_post\n" : "" ); } /** * Per-post options (Wordpress < 2.9) * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_post_options() { global $post; $post_id = $post; if (is_object($post_id)) { $post_id = $post_id->ID; } $permalink = get_post_meta( $post_id, 'custom_permalink', true ); ?>

ID; } $permalink = get_post_meta( $post_id, 'custom_permalink', true ); ?>

term_id); if ( $object->term_id ) { $originalPermalink = ($object->taxonomy == 'post_tag' ? custom_permalinks_original_tag_link($object->term_id) : custom_permalinks_original_category_link($object->term_id) ); } custom_permalinks_form($permalink, $originalPermalink); // Move the save button to above this form wp_enqueue_script('jquery'); ?> term_id); $table = get_option('custom_permalink_table'); if ( $permalink ) $table[$permalink] = array( 'id' => $term->term_id, 'kind' => ($term->taxonomy == 'category' ? 'category' : 'tag'), 'slug' => $term->slug); update_option('custom_permalink_table', $table); } /** * Delete post * * @package CustomPermalinks * @since 0.7.14 * @author Piero */ function custom_permalinks_delete_permalink( $id ){ global $wpdb; $wpdb->query("DELETE FROM $wpdb->postmeta WHERE `meta_key` = 'custom_permalink' AND `post_id` = '".mysql_escape_string($id)."'"); } /** * Delete term * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_delete_term($id) { $table = get_option('custom_permalink_table'); if ( $table ) foreach ( $table as $link => $info ) { if ( $info['id'] == $id ) { unset($table[$link]); break; } } update_option('custom_permalink_table', $table); } /** * Options page * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_options_page() { // Handle revert if ( isset($_REQUEST['revertit']) && isset($_REQUEST['revert']) ) { check_admin_referer('custom-permalinks-bulk'); foreach ( (array)$_REQUEST['revert'] as $identifier ) { list($kind, $id) = explode('.', $identifier); switch ( $kind ) { case 'post': case 'page': delete_post_meta( $id, 'custom_permalink' ); break; case 'tag': case 'category': custom_permalinks_delete_term($id); break; } } // Redirect $redirectUrl = $_SERVER['REQUEST_URI']; ?>



$info ) { $row = array(); $term = get_term($info['id'], ($info['kind'] == 'tag' ? 'post_tag' : 'category')); $row['id'] = $info['kind'].'.'.$info['id']; $row['permalink'] = get_home_url()."/".$permalink; $row['type'] = ucwords($info['kind']); $row['title'] = $term->name; $row['editlink'] = ( $info['kind'] == 'tag' ? 'edit-tags.php?action=edit&tag_ID='.$info['id'] : 'categories.php?action=edit&cat_ID='.$info['id'] ); $rows[] = $row; } } // List posts/pages global $wpdb; $query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE $wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != '';"; $posts = $wpdb->get_results($query); foreach ( $posts as $post ) { $row = array(); $row['id'] = 'post.'.$post->ID; $row['permalink'] = get_permalink($post->ID); $row['type'] = ucwords( $post->post_type ); $row['title'] = $post->post_title; $row['editlink'] = 'post.php?action=edit&post='.$post->ID; $rows[] = $row; } return $rows; } /** * Get original permalink for post * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_original_post_link($post_id) { remove_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); // original hook remove_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 ); $originalPermalink = ltrim(str_replace(get_option('home'), '', get_permalink( $post_id )), '/'); add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); // original hook add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 ); return $originalPermalink; } /** * Get original permalink for page * * @package CustomPermalinks * @since 0.4 */ function custom_permalinks_original_page_link($post_id) { remove_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 ); remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 ); $originalPermalink = ltrim(str_replace(get_home_url(), '', get_permalink( $post_id )), '/'); add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 ); add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 ); return $originalPermalink; } /** * Get original permalink for tag * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_original_tag_link($tag_id) { remove_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 ); remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 ); $originalPermalink = ltrim(str_replace(get_home_url(), '', get_tag_link($tag_id)), '/'); add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 ); add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 ); return $originalPermalink; } /** * Get original permalink for category * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_original_category_link($category_id) { remove_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 ); remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 ); $originalPermalink = ltrim(str_replace(get_home_url(), '', get_category_link($category_id)), '/'); add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 ); add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 ); return $originalPermalink; } /** * Get permalink for term * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_permalink_for_term($id) { $table = get_option('custom_permalink_table'); if ( $table ) foreach ( $table as $link => $info ) { if ( $info['id'] == $id ) { return $link; } } return false; } /** * Set up administration * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_setup_admin() { add_management_page( 'Custom Permalinks', 'Custom Permalinks', 5, 'custom_permalinks', 'custom_permalinks_options_page' ); if ( is_admin() ) wp_enqueue_script('admin-forms'); } if ( !function_exists("get_home_url") ) { function get_home_url() { return get_option('home'); } } $v = explode('.', get_bloginfo('version')); add_action( 'template_redirect', 'custom_permalinks_redirect', 5 ); add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 ); add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 ); add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 ); add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 ); add_filter( 'request', 'custom_permalinks_request', 10, 1 ); add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 ); if ( $v[0] >= 2 ) { add_filter( 'get_sample_permalink_html', 'custom_permalink_get_sample_permalink_html', 10, 4 ); } else { add_action( 'edit_form_advanced', 'custom_permalinks_post_options' ); add_action( 'edit_page_form', 'custom_permalinks_page_options' ); } add_action( 'edit_tag_form', 'custom_permalinks_term_options' ); add_action( 'add_tag_form', 'custom_permalinks_term_options' ); add_action( 'edit_category_form', 'custom_permalinks_term_options' ); add_action( 'save_post', 'custom_permalinks_save_post' ); add_action( 'save_page', 'custom_permalinks_save_post' ); add_action( 'edited_post_tag', 'custom_permalinks_save_tag' ); add_action( 'edited_category', 'custom_permalinks_save_category' ); add_action( 'create_post_tag', 'custom_permalinks_save_tag' ); add_action( 'create_category', 'custom_permalinks_save_category' ); add_action( 'delete_post', 'custom_permalinks_delete_permalink', 10); add_action( 'delete_post_tag', 'custom_permalinks_delete_term' ); add_action( 'delete_post_category', 'custom_permalinks_delete_term' ); add_action( 'admin_menu', 'custom_permalinks_setup_admin' ); ?>