Skip to content

Instantly share code, notes, and snippets.

@ryansechrest
Last active April 15, 2016 09:33
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 ryansechrest/580d976b9f2183ce9f32 to your computer and use it in GitHub Desktop.
Save ryansechrest/580d976b9f2183ce9f32 to your computer and use it in GitHub Desktop.
Remove post type slug from WordPress custom types
<?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) {
// Remove post type slug from these post types
$post_types = ['service'];
// If current post doesn't have post type listed above
if(!in_array($post->post_type, $post_types)) {
return $permalink;
}
// Remove post type slug from permalink
$permalink = str_replace($post->post_type . '/', '', $permalink);
return $permalink;
}
/*************************************************************************/
function hook_pre_get_posts() {
add_action(
'pre_get_posts',
NS . 'pre_get_posts'
);
}
function pre_get_posts($query) {
global $wpdb;
// If current query is WordPress' main query
if (!$query->is_main_query()) {
return;
}
// Get page name from query
$post_name = $page_name = $query->get('pagename');
// Determine if post name contains slash
$last_pos = strrpos($post_name, '/');
// If post name contains slash, it's hierarchical
if ($last_pos !== false) {
// Extract actual post name
$post_name = substr($post_name, $last_pos + 1);
}
// Retrieve post type based on post name
$post_type = $wpdb->get_var(
$wpdb->prepare(
'SELECT post_type FROM ' . $wpdb->posts . ' WHERE post_name = %s LIMIT 1',
$post_name
)
);
// Only process specified post types
switch ($post_type) {
case 'service':
$query->set('service', $page_name);
$query->set('post_type', $post_type);
$query->is_single = true;
$query->is_page = false;
break;
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment