Leave a Reply

1 comment

  1. 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( '/'.CUSTOM::slugify($_SESSION['specialty']), '', $link ); 
    }
    

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