Remove custom post type slug from URL.
<?php | |
namespace RyanSechrest\Plugin; | |
/** | |
* Plugin Name: Remove CPT Slug | |
* Version: 1.0 | |
* Author: Ryan Sechrest | |
* Author URI: http://ryansechrest.com/ | |
* Description: Remove custom post type slug from URLs. | |
* License: GPL2 | |
*/ | |
// Create shortcut for namespace | |
if (!defined(__NAMESPACE__ . '\NS')) { | |
define(__NAMESPACE__ . '\NS', __NAMESPACE__ . '\\'); | |
} | |
// Create shortcut for plugin slug | |
if (!defined(NS . 'PS')) { | |
define(NS . 'PS', 'remove-cpt-slug'); | |
} | |
/*************************************************************************/ | |
add_action( | |
'init', | |
NS . 'initialize_plugin' | |
); | |
function initialize_plugin() { | |
register_post_type_service(); | |
hook_post_type_link(); | |
hook_pre_get_posts(); | |
} | |
/*************************************************************************/ | |
function register_post_type_service() { | |
$label = __('Services', PS); | |
$labels = array( | |
'name' => __('Services', PS), | |
'singular_name' => __('Service', PS), | |
'menu_name' => __('Services', PS), | |
'all_items' => __('All Services', PS), | |
'add_new' => __('Add Service', PS), | |
'add_new_item' => __('Add New Service', PS), | |
'edit_item' => __('Edit Service', PS), | |
'new_item' => __('New Service', PS), | |
'view_item' => __('View Service', PS), | |
'search_items' => __('Search Services', PS), | |
'not_found' => __('No services found', PS), | |
'not_found_in_trash' => __('No services found in trash', PS), | |
'parent_item_colon' => __('Parent Service', PS) | |
); | |
$description = __('Services', PS); | |
$supports = array( | |
'title', | |
'editor', | |
'page-attributes' | |
); | |
$taxonomies = array(); | |
$arguments = array( | |
'label' => $label, | |
'labels' => $labels, | |
'description' => $description, | |
'public' => true, | |
'show_ui' => true, | |
'menu_position' => 20, | |
'hierarchical' => true, | |
'supports' => $supports, | |
'taxonomies' => $taxonomies, | |
'has_archive' => false, | |
'rewrite' => true, | |
'query_var' => true, | |
'can_export' => false | |
); | |
register_post_type('service', $arguments); | |
} | |
/*************************************************************************/ | |
function hook_post_type_link() { | |
add_filter( | |
'post_type_link', | |
NS . 'post_type_link', | |
10, | |
3 | |
); | |
} | |
function post_type_link($permalink, $post, $leavename) { | |
if (!gettype($post) == 'post') { | |
return $permalink; | |
} | |
switch ($post->post_type) { | |
case 'service': | |
$permalink = get_home_url() . '/' . $post->post_name . '/'; | |
break; | |
} | |
return $permalink; | |
} | |
/*************************************************************************/ | |
function hook_pre_get_posts() { | |
add_action( | |
'pre_get_posts', | |
NS . 'pre_get_posts' | |
); | |
} | |
function pre_get_posts($query) { | |
global $wpdb; | |
if (!$query->is_main_query()) { | |
return; | |
} | |
$post_name = $query->get('pagename'); | |
$post_type = $wpdb->get_var( | |
$wpdb->prepare( | |
'SELECT post_type FROM ' . $wpdb->posts . ' WHERE post_name = %s LIMIT 1', | |
$post_name | |
) | |
); | |
switch ($post_type) { | |
case 'service': | |
$query->set('service', $post_name); | |
$query->set('post_type', $post_type); | |
$query->is_single = true; | |
$query->is_page = false; | |
break; | |
} | |
return $query; | |
} | |
/*************************************************************************/ | |
add_action( | |
'admin_init', | |
NS . 'initialize_admin' | |
); | |
function initialize_admin() { | |
//hook_admin_notices(); | |
} | |
/*************************************************************************/ | |
function hook_admin_notices() { | |
add_action( | |
'admin_notices', | |
NS . 'admin_notices' | |
); | |
} | |
function admin_notices() { | |
echo <<<HTML | |
<div class="updated" style="padding: 7px"> | |
<p>Welcome!</p> | |
</div> | |
HTML; | |
} | |
// EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment