Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Permalink structure with $_SESSION variables

So I was able to figure this out myself with the help of a great friend and php developer. I apologize some of the functionality i.e. CUSTOM::slugify() is custom but if necessary you can review my Div Starter theme to learn more about it.

add_action( 'init', 'cc_rewrite_add_rewrites' );
function cc_rewrite_add_rewrites() {
    $specialties = get_specialties(); //Fetches an array of postmeta defined from an ACF repeater field


    add_rewrite_rule( "^(" . implode("|", array_map(function($a) { return strtolower(CUSTOM::slugify($a)); }, $specialties) ) . ")/?", 'index.php', 'top' );
    flush_rewrite_rules();

    // check path for specialty loop through specialties
    foreach($specialties as $specialty) {
        $slug = CUSTOM::slugify($specialty); // I needed to convert to a slug, custom function in Div Starter

        if (preg_match("/\/" . strtolower($slug) ."(\/|$)/i", strtolower($_SERVER['REQUEST_URI']) )) {
        // set session variable
        $_SESSION['specialty'] = $specialty;
        break;
    }
}

}

I noticed my pagination was using this new permalink structure and breaking it so I was able to hook into it and remove the addition like this:

add_filter( 'get_pagenum_link', 'wpse_cc_pagenum_link' );

function wpse_cc_pagenum_link( $link ){
    return $url = str_replace( "https://wordpress.stackexchange.com/".CUSTOM::slugify($_SESSION['specialty']), '', $link ); 
}

For more information about customizing your permalinks this guide was really helpful

Related Posts:

  1. Maintaining two permalink structures
  2. How to add post meta to post permalink with still keeping a good performance?
  3. How to Use metada Value in Url and is it possible? [duplicate]
  4. Is it possible to get a page link from its slug?
  5. Need help with add_rewrite_rule
  6. After server migration only the homepage works
  7. remove “index.php” from permalinks
  8. where is permalink info stored in database?
  9. Changing permalinks gives me 404 errors on nginx
  10. How do I add /blog/ as a prefix to permalink structure for blog posts, tag pages, etc.?
  11. Change media item permalink
  12. Get Permalink without domain (i.e. get relative permalink) [duplicate]
  13. How does WordPress create URLs that Apache knows about?
  14. Strange permalink issue: can’t rename /company-2/ to /company/
  15. Passing and retrieving query vars in wordpress
  16. Get the current page URL (including pagination)
  17. Stop WordPress appending `-2` to the end of my url after i change it
  18. Get default permalink structure from pretty URL’s
  19. How to get permalink and title from post ID?
  20. Why is “/page/2/” not working?
  21. WordPress Permalinks not working in Apache2 Ubuntu 14.04
  22. Performance of my permalink structure?
  23. Permalink format: singular or plural
  24. What is the difference between get_post_permalink and get_permalink?
  25. WordPress matching URLs with trailing tildes
  26. Get current URL (permalink) without /page/{pagenum}/
  27. Use REGEXP in WP_Query meta_query key
  28. How does WordPress handle permalinks?
  29. Pretty permalinks for search results with extra query var
  30. Get page permalink without wpurl
  31. How can I get the privacy policy page?
  32. Can I use REST-API on plain permalink format?
  33. WordPress slug issue with non-latin characters
  34. Rewrite Rule for Multilingual Website, Like qTranslate?
  35. How to get pretty URLs with add_query_arg in permalinks
  36. What is the best permalink structure for SEO?
  37. Change author permalink
  38. Creating custom permalink structure for languages
  39. Hide permalink and preview button and link on custom post
  40. Custom post type permalink endpoint
  41. How can I make Capital letter ( upper-case ) permalinks?
  42. How can I reverse engineer a Permalink to Find the Page?
  43. Mapping Domains to Permalinks (not multisite)
  44. How to remove dates from existing permalinks?
  45. How do I set a custom base URL for standard posts?
  46. Removing Parent Page(s) from Permalink
  47. Why permalinks work with /index.php/%postname% but not with just %postname%?
  48. Setting up WordPress with Custom Permalinks and no .htaccess File?
  49. Getting the Site URL Including the Front Base
  50. Does WordPress Change the .htaccess File When Updating?
  51. Using $_GET variables in the URL?
  52. Removing the redirect after changing a page’s slug
  53. How to display a public profile page for registered users with custom slug?
  54. Removing leading zeros from custom permalink structure
  55. Must slugs be unique?
  56. Setting 404 page in Nginx
  57. How to get the clean permalink in a draft?
  58. How to use relative links on my pages?
  59. How does wordpress keep track of post id when post id is not used in permalinks?
  60. How to filter to output of the get_permalink() function
  61. Stop WordPress from reserving slugs for media items?
  62. Including category-base in a post permalink results in 404
  63. How to change ‘with_front” key from an existing custom post type?
  64. Change slug on post creation
  65. Does WordPress automatically do 301 redirect if I change permalink
  66. How can I store page ID in a post instead of other selected permalink?
  67. WordPress Permalink changes to question mark (?) in URL
  68. WordPress thinks my custom route is a 404
  69. Prevent WordPress from abbreviating-long-slugs…-in-the-admin
  70. Adding another word in front of post url
  71. Permalink with sub-sub-category and post name
  72. Post type child of another post type
  73. Permalink not working for page without title
  74. Remove index.php in permalink structure on IIS server
  75. What is the difference between get_permalink vs get_the_permalink?
  76. Page begins with number, WordPress adds 2
  77. Move WordPress to subdirectory, keep ALL URLs
  78. How would I create a different permalink structure for pages and posts?
  79. My permalinks are broken! Can I use mod_rewrite to ignore a physical file?
  80. Custom Taxonomy in Permalink of Post
  81. Is the permalink structure /%post_id%/%postname% faster or slower than just /%postname%?
  82. WooCommerce return 404 on category pages
  83. How to prevent apostrophes and quotes from appearing in permalinks?
  84. Minimal custom permalink structure
  85. the_permalink displays post ID URL for future posts
  86. Why does `get_permalink()` produces an add. DB request without $post->filter?
  87. WordPress 4.4+ breaks Walker Extension
  88. permalink independent on the post’s name stored in the database
  89. Date based URLs for custom posts and pagination
  90. Archive permalinks
  91. Regex in add_rewrite_tag not accepting OR operators?
  92. Removing hierarchical pages in the permalink
  93. How to add a custom URL placeholder to author archives?
  94. 301 Redirects for Changed Permalink Structure & Category Base
  95. Is there a user-facing interface to edit an attachment’s permalink?
  96. How to custom change author base without $this->front?
  97. Permalinks to Custom does not work (out of box)
  98. URL rewrite based on a custom field value
  99. Meta compare with date (stored as string) not working
  100. Getting attachments by meta value
Categories permalinks Tags meta-query, permalinks, post-meta, session
Scaling an image in a WordPress post
Permalink for custom post type

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress