Skip to content

Instantly share code, notes, and snippets.

@nigelheap
Created March 20, 2017 22:40
Show Gist options
  • Save nigelheap/16f443f2568b7e8afd8f276764b1cc20 to your computer and use it in GitHub Desktop.
Save nigelheap/16f443f2568b7e8afd8f276764b1cc20 to your computer and use it in GitHub Desktop.
Wordpress: Updates a post type's slug and allows it to work with a custom segment.
<?php
/**
* Updates the url to events plugin posts and adds new url rule to allow a string before the slug
*/
add_action('init', function(){
$args = get_post_type_object("eventbrite_events");
$args->rewrite["slug"] = "events/%organizer_name%";
register_post_type($args->name, $args);
add_rewrite_rule('events/(.+?)/(.+?)$', 'index.php?post_type=eventbrite_events&nothing=$matches[1]&name=$matches[2]', 'top');
});
/**
* [$id description]
* @var integer
*/
add_filter('post_type_link', function($post_link, $id = 0, $leavename = FALSE) {
if (strpos('%organizer_name%', $post_link) === FALSE) {
$post = get_post($id);
if(!$post){
return str_replace('%organizer_name%', 'default', $post_link);
}
$category = get_post_meta($post->ID, 'organizer_name', true);
$category = str_ireplace('ecreators', '', $category);
$category = sanitize_title_with_dashes($category);
return str_replace('%organizer_name%', $category, $post_link);
}
}, 1, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment