Created
March 20, 2017 22:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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¬hing=$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