• Hi everyone,

    After hours of try/break/research I’ve decided to give it a go and try and get some help from you awesome community.

    The actual situation:
    I’m building a plugin which allows creating user profile pages. Inside the plugin I’ve got a custom post type and a short-code. The idea of building this into a plugin is because it will be good to have the profile pages under multiple child sites.

    The issue:
    Considering that the profile pages are visible on a particular page which has a shortcode, I need to make sure that particular page becomes the parent of the user profiles and therefore the url will be mypage/user_profile rather than custom_post_type/user_profile

    I found out that by adding this ‘rewrite’ => array(‘slug’ => ‘mypage’) to the $args is actually doing what I want, the problem is that the slug is hardcoded and it needed to be dynamically.

    The main problem is that at the point when I’m registering the custom post type using an add_action(‘init’) it won’t know anything about the $post->post_name.

    The way I was trying to achieve this is by checking if the page has_shortcode then update the $custom_slug with it’s slug name and pass it to the ‘rewrite’ => array(‘slug’ => $custom_slug).

    Is there any way I can define the $post before registering the custom post type? Or anyone got a different approach?

    Sorry for this long topic and I hope it’s explanatory enough or otherwise please ask.

    Thank you very much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    This is entirely speculative and untested, but I think it’s plausible. What you might do is initially just register your post type with whatever generic slug in the “init” action. The permalink for the profile would be what you wish to avoid, custom_post_type/user_profile. This is OK for now. Just before registering, start a PHP session. More on this later.

    Hook the “request” action. Your callback will be passed the query vars for the request. This should be enough to determine what the desired post type slug should be. Based on the query vars, you can verify the profile is requested and if so, check that it uses the shortcode by getting the post content directly with $wpdb->get_var(). If that checks out, set a session var with the post type slug and fashion the desired permalink.

    Get the “rewrite_rules” option and alter the post type rewrite rule so it will identify the new slug. Save the altered rules. Now you can redirect to the new permalink. Back in the “init” action where the session was started, but now it’s the second time through, check if the post type session var is set. If not, it’s the first time through, so register the post type with the generic slug. Otherwise register with the slug obtained from the session var. Also then, add a hook to clean up afterwards and reset to the generic slug. More on this later.

    Since the rewrite rules were already altered and the post type is registered to match, the rewritten request should be processed normally and the desired permalink is displayed with the proper content.

    Now we need to clean up and reset in anticipation of another profile request with a generic post type slug. Any hook that occurs after the rewrite rules are needed will work. “wp_head” ought to work. Add the hook from the “init” callback mentioned earlier. The clean up would consist of resetting the rewrite_rules option back to the generic post type slug, unsetting the session var containing the current post type slug, and finally calling session_destroy().

    When we change post type slugs, we normally would flush and regenerate the rewrite rules. This is a very expensive operation computationally speaking. A shame when only one thing is changed. I’m hoping that altering the post type rewrite rule saved in the DB will be adequate to get the desired redirect to work without needing to do the entire flush and regenerate process. Untested though.

    There’s likely some glitch in my scheme I haven’t anticipated. Hopefully whatever it is can be addressed. URLs that always redirect are not good for SEO. Hopefully that’s not a huge concern for you since I cannot imagine how to do this without a redirect. What might help is setting the canonical link in the head section to the generic post type URL.

    I’m assuming the initial link to the profile is the generic version since there is not any way to know what the preferred version is when the link is output. If there is a way to know and the preferred link is output, then the slug with which to register can be extracted for use when registering and the rewrite rules option could be altered so that a redirect and PHP session are not required. I’m not sure when WP gets the rewrite rules from the DB. You’ll need to alter the option prior to this. Doing so when your plugin loads is pretty early and it’s impractical to do so any earlier, so hopefully that would be good enough.

    Thread Starter netdesignr

    (@paladin884)

    Thanks a lot for your reply. I need to try this and I will get back as soon as possible.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Dynamically rewrite the slug on custom post type if has shortcode’ is closed to new replies.