Make WordPress Core

Opened 10 years ago

Last modified 7 years ago

#35916 new defect (bug)

WP_Rewrite::generate_rewrite_rules() ignores boolean $endpoints / $feed parameters for CPT

Reported by: solo14000's profile solo14000 Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Rewrite Rules Keywords: needs-patch needs-unit-tests
Focuses: Cc:

Description (last modified by johnbillion)

Hello,

I'm trying to declare custom rewrite rules for a custom post type without endpoints and feeds.
As result, WP ignore the endpoint flag in the WP_Rewrite::generate_rewrite_rules().

What I did is as following.

I created a custom post type called 'event' with the following arguments for register_post_type() :

array( // Array of WP post type args
        'labels' => array(
                'name' => 'Events',
                'singular_name' => 'Event',
        ),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => false,
        'rewrite' => false,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-tickets-alt',
        'supports' => array(
                'title',
                'editor',
                'thumbnail',
        ),
        'taxonomies' => array(
                'post_tag',
                'my_category',
        ),
);

Then I called

$args_post_type = array(
        'feed'          => false,
        'paged'         => false,
        'ep_mask'       => EP_NONE,
        'endpoints'     => false,
);
add_permastruct('events', 'events/%event%', $args_post_type);

The resultings rewrite rules are :

'events/[^/]+/attachment/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32)
'events/[^/]+/attachment/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb=1' (length=37)
'events/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49)
'events/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49)
'events/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage=$matches[2]' (length=50)
'events/[^/]+/attachment/([^/]+)/embed/?$' => string 'index.php?attachment=$matches[1]&embed=true' (length=43)
'events/([^/]+)/embed/?$' => string 'index.php?event=$matches[1]&embed=true' (length=38)
'events/([^/]+)/trackback/?$' => string 'index.php?event=$matches[1]&tb=1' (length=32)
'events/([^/]+)(?:/([0-9]+))?/?$' => string 'index.php?event=$matches[1]&page=$matches[2]' (length=44)
'events/[^/]+/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32)
'events/[^/]+/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb=1' (length=37)
'events/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49)
'events/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49)
'events/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage=$matches[2]' (length=50)
'events/[^/]+/([^/]+)/embed/?$' => string 'index.php?attachment=$matches[1]&embed=true' (length=43)

Looking at class-wp-rewrite.php, I noticed in generate_rewrite_rules() that piece of code that forces the $post flag for CTP

if ( ! $post ) {
        // For custom post types, we need to add on endpoints as well.
        foreach ( get_post_types( array('_builtin' => false ) ) as $ptype ) {
                if ( strpos($struct, "%$ptype%") !== false ) {
                        $post = true;

                        // This is for page style attachment URLs.
                        $page = is_post_type_hierarchical( $ptype );
                        break;
                }
        }
}

and that piece of code that unconditionnaly add extra endpoints / feeds... for post

// If we're matching a permalink, add those extras (attachments etc) on.
if ( $post ) {
        // Add trackback.
        $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite);

Could confirm that problem?

Thanks

Change History (3)

#1 @johnbillion
10 years ago

  • Component changed from Permalinks to Rewrite Rules
  • Description modified (diff)
  • Keywords needs-patch needs-unit-tests added
  • Version 4.4.2 deleted

Thanks for the report, @solo14000, and welcome to WordPress Trac.

I've confirmed this bug. Rewrite rules for endpoints and feeds still get generated for the permalink structure in this situation.

Last edited 10 years ago by johnbillion (previous) (diff)

#2 @thomascharbit
10 years ago

Hi,

can you also confirm that the codex seems wrong about what argument to use to disable pagination?

On: https://codex.wordpress.org/Function_Reference/register_post_type#Arguments

It says to set pages argument to false, while it should be paged according to what add_permastruct() does?

I don't know if the docs should be edited or if the pages argument should be handled, like it's done for feeds (which is mapped to feed in register_post_type())

#3 @solo14000
9 years ago

Registering custom post type with argument '_builtin' = true seems to be a workaround.
Can anybody tell me more about side effects setting '_builtin' true?

Thanks

Note: See TracTickets for help on using tickets.