Home / Admin / Showcase IDX plugin – Replace all Permalink URLs with current URL in Schema
Duplicate Snippet

Embed Snippet on Your Site

Showcase IDX plugin – Replace all Permalink URLs with current URL in Schema

This snippet replaces all the Permalink URLs with the current URL in Schema markup.

80+
Code Preview
php
<?php
add_filter( 'aioseo_schema_output', 'aioseo_schema_change_urls' );
function aioseo_schema_change_urls( $graphs ) {
    if ( false === strpos( $_SERVER['REQUEST_URI'], 'search-results/listing/' ) ) {
        return $graphs;
    }
	
    $request_uri = explode( '?', $_SERVER['REQUEST_URI'] );
	$request_uri = explode( '#', $request_uri[0] );
    $url_parts = trim($request_uri[0], '/');
    $url_parts = explode('/', $url_parts);
    $last_segment = end($url_parts);
    $old_url = get_permalink();
    $new_url     = home_url( $request_uri[0] );	
	
    foreach ( $graphs as $index => &$value ) {
        if ( is_array( $value ) ) {
            $value = aioseo_schema_change_urls( $value );
        }
        if ( is_string( $value ) ) {
            if ( $value === $old_url ) {
                $value = $new_url;
            } elseif ( false !== strpos( $value, $old_url . '#' ) ) {
                $value = str_replace( $old_url, $new_url, $value );
            }
        }
		
    }
    return $graphs;
}

Comments

Add a Comment