Skip to content

Instantly share code, notes, and snippets.

@khoipro
Created January 15, 2024 11:42
Show Gist options
  • Save khoipro/71455b630e769bce2c0be57c9b1d0c73 to your computer and use it in GitHub Desktop.
Save khoipro/71455b630e769bce2c0be57c9b1d0c73 to your computer and use it in GitHub Desktop.
The sample class Rewrites to remove a post type slug/
<?php
/**
* Rewrites Class
*
* @package CodetotTheme
* @author codetot
* @since 0.0.1
*/
namespace CODETOT\Theme;
class Rewrites {
public function __construct() {
add_filter('post_type_link', [$this, 'post_type_link'], 10, 3);
add_action('init', [$this, 'rewrite_rules']);
add_filter('wp_unique_post_slug', [$this, 'prevent_slug_duplicates'], 10, 6);
add_action('pre_get_posts', [$this, 'pre_get_posts']);
}
function post_type_link($post_link, $post, $leavename) {
if (isset($post->post_type) && $post->post_type === 'podcast') {
$post_link = home_url($post->post_name);
}
return $post_link;
}
function rewrite_rules() {
add_rewrite_rule('(.+?)/?$', 'index.php?post_type=podcast&name=$matches[1]', 'bottom');
}
function prevent_slug_duplicates( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
$check_post_types = array(
'post',
'page',
'podcast'
);
if ( ! in_array( $post_type, $check_post_types ) ) {
return $slug;
}
if ( 'podcast' == $post_type ) {
// Saving a custom_post_type post, check for duplicates in POST or PAGE post types
$post_match = get_page_by_path( $slug, 'OBJECT', 'post' );
$page_match = get_page_by_path( $slug, 'OBJECT', 'page' );
if ( $post_match || $page_match ) {
$slug .= '-duplicate';
}
} else {
// Saving a POST or PAGE, check for duplicates in custom_post_type post type
$custom_post_type_match = get_page_by_path( $slug, 'OBJECT', 'custom_post_type' );
if ( $custom_post_type_match ) {
$slug .= '-duplicate';
}
}
return $slug;
}
function pre_get_posts( $query ) {
if (!$query->is_main_query()) {
return;
}
if (!isset($query->query['page']) || 2 !== count($query->query)) {
return;
}
if (empty($query->query['name'])) {
return;
}
$query->set('post_type', array('post', 'page', 'podcast'));
}
}
new Rewrites();
@khoipro
Copy link
Author

khoipro commented Jan 15, 2024

Cài đặt (dành cho anh em Việt Nam)

Hãy cẩn trọng, code này có thể khiến web của bạn bị lỗi 404 nên cần làm cẩn thận hoặc nhờ team DEV làm
Các khách hàng của CODE TỐT có sử dụng dịch vụ bảo trì web thì liên hệ team mình để triển khai, hạng mục này nằm trong tất cả gói.

Để sử dụng code này, bạn làm như sau:

Bước 1: Copy vào trong theme ở cấu trúc dạng wp-content/themes/xxx/class-rewrites.php, nhớ thay thế hết chữ 'podcast' = slug bạn muốn gỡ bỏ.
Bước 2: Thêm dòng code vào trong functions.php

require_once get_stylesheet_directory() . '/class-rewrites.php';

Bước 3: Tìm chỗ đăng ký post type và set 'rewrite' => false
Bước 4: Vào mục Settings > Permalink (Cài đặt > Đường dẫn tĩnh), bấm Save/Lưu lại 1 lần.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment