• Using this function successfully per the code below. It creates an accessible permalink like domain.com/category/post/deals. However, what I need the permalink to render as is actually domain.com/category/post-deals (no trailer slash just “-deals”) but still have it accessible in the vars so I can load the proper template. Is there a way to do that with this function?

    function wpd_add_deals(){
        add_rewrite_endpoint( 'deals', EP_PERMALINK );
    }
    add_action( 'init', 'wpd_add_deals' );
    
    add_filter('request', function($vars) {
    	if (isset($vars['deals'])) {
    		$vars['deals'] = true;
    	}
    	return $vars;
    });
    
    add_filter('template_include', function($template) {
    	if (is_singular() && get_query_var('deals')) {
    		$post = get_queried_object();
    		return locate_template(['my-template.php']);
    	}
    	return $template;
    });
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Why do you want to eliminate the trailing slash? WP wants all of its permalinks to terminate in a slash. When one is missing, WP will do a 302 redirect to the URL with a slash. AFAIK it’s difficult to alter this behavior. You’ll need a very good reason and be strongly motivated if you want to change this. I recommend leaving it be.

    Thread Starter aeamarkg

    (@aeamarkg)

    Yes, I have a good reason for it. A permalink like this “domain.com/category/post-deals” is perfectly legitimate. The issue is with the function being used. It appends “deals” after a slash in the permalink. I would rather it append “deals” to the end of the permalink not create the additional slash, yet still be available to vars.

    Moderator bcworkz

    (@bcworkz)

    That doesn’t have anything to do with the terminal slash. It’s a matter of adding an appropriate rewrite rule to separate “post” from “deals” so they are treated as separate query vars instead of a single post slug. An endpoint is not helpful in this case because by definition it’s a permalink element which occurs between slashes. You’re appending “-deals” to a post slug. While it may be at the end, it cannot be an endpoint per se.
    https://developer.www.ads-software.com/reference/functions/add_rewrite_rule/

    Also use appropriate filters (post_link or post_type_link) to cause WP to output your desired form of permalink. Whether WP then adds a terminal slash to “post-deals” or not is irrelevant.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Use add_rewrite_endpoint without the trailing slash’ is closed to new replies.