\WP_Stager_Integration\Event::resolve_event_permalinks(\WP $wp): void
\WP_Stager_Integration\Event::resolve_event_permalinks(\WP $wp): voidDescription
Attempts to resolve event permalinks by parsing the URL and setting query vars.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$wp | \WP | The WordPress environment instance. |
Returns:
void
Information
| File | class-event.php line 301 |
|---|
Full Code
/**
* Attempts to resolve event permalinks by parsing the URL and setting query vars.
*
* @param \WP $wp The WordPress environment instance.
*
* @return void
*/
public static function resolve_event_permalinks( $wp ) {
global $wpdb;
// Bail if event pages are disabled
if ( ! Options::is_enabled_event_pages() ) {
return;
}
// Get data
$permalink_base = (string) (Options::get_event_permalink_base() ?: '');
$permalink_structure = (string) Options::get_event_permalink_structure();
// Bail if no permalink base
if ( ! $permalink_base ) {
return;
}
// Get current request URI
$request_uri = (string) ($_SERVER[ 'REQUEST_URI' ] ?? '');
// Bail if no request URI
if ( ! $request_uri ) {
return;
}
// Parse request URI
$parsed_uri = parse_url( $request_uri );
$path = (string) ($parsed_uri[ 'path' ] ?? '');
// Remove leading/trailing slashes and split
$path_parts = array_values( array_filter( explode( '/', trim( $path, '/' ) ) ) );
// Split permalink base into parts (supports forward slashes)
$permalink_base_parts = array_values( array_filter( explode( '/', trim( $permalink_base, '/' ) ) ) );
// Bail if path doesn't start with permalink base
if ( empty( $path_parts ) || count( $path_parts ) < count( $permalink_base_parts ) ) {
return;
}
// Check if path starts with all permalink base parts
for ( $i = 0; $i < count( $permalink_base_parts ); $i ++ ) {
// Bail if path part doesn't match permalink base part
if ( ! isset( $path_parts[ $i ] ) || $path_parts[ $i ] !== $permalink_base_parts[ $i ] ) {
return;
}
}
// Remove permalink base parts from path parts
$path_parts = array_slice( $path_parts, count( $permalink_base_parts ) );
// Determine which field to extract and extract it based on permalink structure
$event_id = 0;
$event_slug = '';
$use_event_id = false;
$use_event_slug_with_suffix = false;
switch ( $permalink_structure ) {
case Options::PERMALINK_STRUCTURE_EVENT_ID: // {event_id}
case Options::PERMALINK_STRUCTURE_EVENT_ID_SLASH_EVENT_NAME: // {event_id}/{event_name}
$event_id = (int) ($path_parts[ 0 ] ?? 0);
$use_event_id = true;
break;
case Options::PERMALINK_STRUCTURE_EVENT_NAME_SLASH_EVENT_ID: // {event_name}/{event_id}
$event_id = (int) ($path_parts[ 1 ] ?? 0);
$use_event_id = true;
break;
case Options::PERMALINK_STRUCTURE_EVENT_NAME: // {event_name}
$event_slug = (string) ($path_parts[ 0 ] ?? '');
$use_event_slug_with_suffix = true;
break;
case Options::PERMALINK_STRUCTURE_EVENT_NAME_HYPHEN_EVENT_ID: // {event_name}-{event_id}
$combined = (string) ($path_parts[ 0 ] ?? '');
if ( preg_match( '/^(.+)-(\d+)$/', $combined, $matches ) ) {
$event_id = (int) ($matches[ 2 ] ?? 0);
$use_event_id = true;
}
break;
case Options::PERMALINK_STRUCTURE_EVENT_ID_HYPHEN_EVENT_NAME: // {event_id}-{event_name}
$combined = (string) ($path_parts[ 0 ] ?? '');
if ( preg_match( '/^(\d+)-(.+)$/', $combined, $matches ) ) {
$event_id = (int) ($matches[ 1 ] ?? 0);
$use_event_id = true;
}
break;
}
// Init
$event_post = null;
if ( $use_event_id && $event_id ) {
// Get post by event ID (stored as post_name)
$event_post = get_page_by_path( (string) $event_id, OBJECT, self::CPT_SLUG );
} elseif ( $use_event_slug_with_suffix && $event_slug ) {
// Get post by unique slug meta
$meta_key = self::get_meta_key( self::META_KEY_SLUG_UNIQUE );
$post_id = $wpdb->get_var( $wpdb->prepare(
"SELECT post_id FROM {$wpdb->postmeta}
WHERE meta_key = %s AND meta_value = %s LIMIT 1",
$meta_key,
$event_slug
) );
if ( $post_id ) {
$event_post = get_post( (int) $post_id );
}
}
// If we found an event post, set up the query vars
if ( $event_post ) {
// Clear any existing error state and conflicting query vars
unset( $wp->query_vars[ 'error' ] );
// Clear any conflicting query vars that might have been set by WordPress trying to parse the URL
unset( $wp->query_vars[ 'attachment' ] );
unset( $wp->query_vars[ 'page' ] );
unset( $wp->query_vars[ 'pagename' ] );
unset( $wp->query_vars[ 'name' ] );
// Set query vars so WordPress conditionals work
$wp->query_vars[ 'p' ] = $event_post->ID;
$wp->query_vars[ 'post_type' ] = self::CPT_SLUG;
add_filter( 'redirect_canonical', '__return_false' );
}
}💡 If you ever get stuck or have a question, please check our FAQs, our Free Integration Service, our paid Full Integration Service, or reach out to us!

Get WP Stager Integration
🎁 Limited offer: Use code WELCOME26 to get your first month for free!

