79083948

Date: 2024-10-13 19:16:33
Score: 0.5
Natty:
Report link

I found a temp workaround:

You can make a plugin that rewrites the permalinks using this code:

<?php
/*
Plugin Name: Custom Post Type Permalink Modifier
Description: Removes the base slug from custom post type permalinks
Version: 1.0
Author: Your Name
*/

function remove_cpt_base($post_link, $post) {
    if ('custompage' === $post->post_type && 'publish' === $post->post_status) {
        $post_link = str_replace('/this-string-gets-removed/', '/', $post_link);
    }
    return $post_link;
}
add_filter('post_type_link', 'remove_cpt_base', 10, 2);

// Flush rewrite rules on plugin activation
function cpt_permalink_modifier_activate() {
    flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'cpt_permalink_modifier_activate');

// Flush rewrite rules on plugin deactivation
function cpt_permalink_modifier_deactivate() {
    flush_rewrite_rules();
}
register_deactivation_hook(__FILE__, 'cpt_permalink_modifier_deactivate');

// Add custom rewrite rule
function custom_rewrite_rules() {
    add_rewrite_rule(
        '^([^/]+)/?$',
        'index.php?post_type=custompage&name=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_rewrite_rules', 10, 0);

Change the ACF post type's Permalink Rewrite to "Custom Permalink". Enter a slug here that will later be removed. You can find this setting at:

ACF > Post Types > Edit Post Type > Advanced Configuration > Advanced Settings > Urls

In the above code, replace:

  1. 'custompage' with the ACF "Post Type Key" from ACF Edit Post Type
  2. '/this-string-gets-removed/' with the slug you entered

This plugin code does not remove the original slug. So there will be two URLs to the same page.

Reasons:
  • Blacklisted phrase (1): This plugin
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: user27559903