Advertisement
mbis

Remove CPT permalink base

Nov 28th, 2020 (edited)
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * Use only for specific CPT posts
  5.  */
  6. define('PM_CPT_NAME', 'courses');
  7.  
  8. /**
  9.  * The function that detects the CPT posts permalinks
  10.  */
  11. function pm_detect_cpt_slugs($query) {
  12.     global $wpdb, $pm_query, $wp, $wp_rewrite;
  13.    
  14.     // Do not run when Elementor is opened
  15.     if((!empty($_REQUEST['action']) && strpos($_REQUEST['action'], 'elementor') !== false) || isset($_REQUEST['elementor-preview'])) {
  16.         return $query;
  17.     }
  18.  
  19.     // 1. Get the slug
  20.     preg_match("/([^\/]+)(?:\/{$wp_rewrite->pagination_base}\/([\d]+))?$/", $wp->request, $parts);
  21.  
  22.     if(!empty($parts[1])) {
  23.         $post_name = basename($parts[1]);
  24.         $post_type = PM_CPT_NAME;
  25.  
  26.         // 2. Check if the slug is assigned to any post item
  27.         $sql_query = "SELECT ID FROM $wpdb->posts WHERE 1=1 AND post_name = %s AND post_type = %s";
  28.         $post_id = $wpdb->get_var($wpdb->prepare($sql_query, array($post_name, $post_type)));
  29.  
  30.         // 3. Filter the query if post was found
  31.         if(!empty($post_id)) {
  32.             $post = get_post($post_id);
  33.            
  34.             if(empty($post->post_type)) { return $query; }
  35.  
  36.             $post_type_object = get_post_type_object($post->post_type);
  37.             $final_uri = (!empty($post->post_name)) ? $post->post_name : false;
  38.  
  39.             // Fix for hierarchical CPT & pages
  40.             if(!(empty($post->ancestors)) && !empty($post_type_object->hierarchical)) {
  41.                 foreach($post->ancestors as $parent) {
  42.                     $parent = get_post($parent);
  43.  
  44.                     if($parent && $parent->post_name) {
  45.                         $final_uri = $parent->post_name . '/' . $final_uri;
  46.                     }
  47.                 }
  48.             }
  49.  
  50.             $new_query = array(
  51.                 'post_type' => $post->post_type,
  52.                 $post->post_type => $final_uri,
  53.                 'name' => $final_uri,
  54.                 'do_not_redirect' => 1,
  55.             );
  56.  
  57.             // 4. Disable canonical redirect
  58.             remove_action('template_redirect', 'wp_old_slug_redirect');
  59.             remove_action('template_redirect', 'redirect_canonical');
  60.             add_filter('wpml_is_redirected', '__return_false', 99, 2);
  61.             add_filter('pll_check_canonical_url', '__return_false', 99, 2);
  62.  
  63.             $query = $new_query;
  64.         }
  65.     }
  66.    
  67.     if(isset($_GET['debug_query'])) {
  68.         echo sprintf('<pre>Old query: %s</pre>', print_r($query, true));
  69.         echo (!empty($new_query)) ? sprintf('<pre>New query: %s</pre>', print_r($new_query, true)) : '';
  70.         echo (!empty($parts)) ? sprintf('<pre>Parts: %s</pre>', print_r($parts, true)) : '';
  71.         die();
  72.     }
  73.  
  74.     return $query;
  75. }
  76. add_filter('request', 'pm_detect_cpt_slugs', 9999);
  77.  
  78. function bis_filter_cpt_permalinks($url, $element) {
  79.     if(!empty($element->post_type) && ($element->post_type == PM_CPT_NAME)) {
  80.         $full_uri = get_page_uri($element->ID);
  81.         $url = trim(get_option('home'), '/') . "/{$full_uri}";
  82.     }
  83.  
  84.     return $url;
  85. }
  86. add_filter('post_type_link', 'bis_filter_cpt_permalinks', 999, 2);
  87.  
  88. function pm_redirect_native_cpt_permalinks() {
  89.     global $wp;
  90.  
  91.     if(!empty($wp->request) && is_singular(PM_CPT_NAME)) {
  92.         $cpt_data = get_post_type_object(PM_CPT_NAME);
  93.         $cpt_slug = preg_quote($cpt_data->rewrite['slug'], '/');
  94.  
  95.         if(!empty($cpt_slug) && preg_match("/^({$cpt_slug})/", $wp->request)) {
  96.             $url = get_permalink();
  97.             wp_safe_redirect($url, 301);
  98.             exit();
  99.         }
  100.     }
  101. }
  102. add_action('template_redirect', 'pm_redirect_native_cpt_permalinks', 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement