Skip to content

Instantly share code, notes, and snippets.

@actual-saurabh
Created April 18, 2017 03:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save actual-saurabh/62f078d804e0e1e4f0264f02e6478c15 to your computer and use it in GitHub Desktop.
Save actual-saurabh/62f078d804e0e1e4f0264f02e6478c15 to your computer and use it in GitHub Desktop.
Rewrite Post Type URLs in WordPress
<?php
if ( ! defined( ABSPATH ) ) exit;
if ( ! class_exists( 'PermaPostType' ) ) {
class PermaPostType {
/**
* Initialise
*/
function init() {
// rewrite
add_action( 'init', array( $this, 'rewrite' ) );
// mod query
add_filter( 'query_vars', array( $this, 'query_vars' ) );
// nicer link
add_filter( 'post_type_link', array( $this, 'permalink' ), 10, 4 );
}
/**
* Rewrite the urls
*/
function rewrite() {
// the structure
$structure = '/%listid%/%listtitle%/';
// add the structure
add_permastruct( 'listing', $structure, false );
// create tags that WP can understand (for the urls only)
add_rewrite_tag( "%listid%", '([^/]\d+)', "p=" );
add_rewrite_tag( "%listtitle%", '([^/]+)', "listtitle=" );
// modify the query for variables (for actual processes)
add_action( 'pre_get_posts', array( $this, 'query' ) );
}
/**
* Just add to the standard var array, so WordPress will add it to the array
*
* @param array $query_vars Default query args
* @return string
*/
function query_vars( $query_vars ) {
// this is the one we set, post id is automatically set and handled
$query_vars[] = 'listtitle';
return $query_vars;
}
/**
* Filters the url to create a nice looking permalink
*
* Converts http://site.com/%listid%/%listtitle%/ to
*
* http://site.com/1234/post-title
*
* @param string $permalink The original permalink
* @param object $post The current custom post
* @return string
*/
function permalink( $permalink, $post ) {
// don't do this for other post types and unpublished stati
if ( ('listing' != $post->post_type) || '' === $permalink || in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
return;
}
// those are our tags
$rewritecode = array(
'%listid%',
'%listtitle%'
);
$list_id = 0;
// if there is a list id tag, replace it with post id
if ( strpos( $permalink, '%listid%' ) !== false ) {
$list_id = $post->ID;
}
$list_title = '';
// if there is a list_title tag (even if empty) replace with post title
if ( strpos( $permalink, '%listtitle%' ) !== false ) {
$list_title = sanitize_title( $post->post_title );
}
$rewritereplace = array(
$list_id,
$list_title,
);
// the actual replacement
$permalink = str_replace( $rewritecode, $rewritereplace, $permalink );
return $permalink;
}
/**
* Filter the current query
* @param object $query The default query
*/
function query( $query ) {
// only do this for our posts
if ( isset( $query->query_vars[ 'listtitle' ] ) ) {
// the post type doesn't get set, set it
// otherwise we get a 404
$query->set( 'post_type', 'listing' );
}
// and we're done!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment