Skip to content

Instantly share code, notes, and snippets.

@klihelp
Last active August 29, 2015 13:57
Show Gist options
  • Save klihelp/9529141 to your computer and use it in GitHub Desktop.
Save klihelp/9529141 to your computer and use it in GitHub Desktop.
WordPress - Prefix Custom Post Type posts slugs
<?php
/**
* Prefix Posts Slugs
* This hooks into wp_unique_post_slug and adds prefix to post slug. Eg: have post Hello title and slug will be my-prefix-hello.
* @author Klipolis
* @since 0.1.0
* @version 0.1.0
* @access public
* @main WordPress
*/
/**
* Prefix my post slugs
*/
add_filter( 'wp_unique_post_slug', 'prefix_wp_unique_post_slug', 2, 6 );
function prefix_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
if ( $post_type, 'my_postype_only') ) {
$prefix = 'my-prefix-';
if ( strripos($slug, $prefix) !== 0 ) {
$slug = $prefix . $slug;
}
}
return $slug;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment