0 ) { foreach ( $filters as $item ) { if ( $item['slug'] ) { $slug = str_replace( home_url() . '/', '', get_permalink( $item['id'] ) ); add_rewrite_rule( '^' . $slug . '([^/]*' . $item['var'] . ')/([^/]*)/?', 'index.php?page_id=' . $item['id'] . '&$matches[1]=$matches[2]', 'top' ); } } flush_rewrite_rules(); } } /** * Build the array of post IDs that have a slug for the URL * along with what custom query variable to use. * * @package PluginNamespace\Front\Rewrites * @since 1.0.0 * * @return array> */ public function build_rewrite_urls(): array { $pages = array(); $page_list = array( 'NMSP_PLUGIN_SLUG_PAGE_NAME' => array( 'foo' ), ); foreach ( $page_list as $key => $params ) { $page_id = get_option( constant( $key ) ); $page = ( $page_id ) ? get_post( $page_id ) : false; if ( ! empty( $page ) ) { foreach ( $params as $param ) { $pages[] = array( 'id' => $page_id, 'slug' => $page->post_name, 'var' => $param, ); } } } return $pages; } /** * Append custom query variables to the list WordPress maintinas * with each request. * * @package PluginNamespace\Front\Rewrites * @since 1.0.0 * * @param array $vars * * @return array */ public function custom_query_vars( array $vars ): array { $vars[] = 'foo'; return $vars; } } new Rewrites();