Soil Root Relative URL + Plugins

I’m having issues with a few plugins that needs the full URL for media files, but are outputting the relative URLs.

For instance, I’m using the Schema plugin for outputting structured data, and it’s causing markup errors since the media files are relative.

/media/my-image.jpg (The value provided for image.url must be a valid URL.)

Is there a way to selectively restrict the relative URLs?

Can you try this?

add_action('schema_json', function () {
  remove_filter('wp_get_attachment_url', 'Roots\\Soil\\Utils\\root_relative_url');
});

Thanks Ben, I’ll give that a shot.

I’m also experiencing this issue with Yoast Sitemaps, specifically taxonomy indexes. (such as category-sitemap.xml & post_tag-sitemap.xml) All other indexes contain the domain but no taxonomy indexes, including custom.

The following code dropped into functions.php or another file loaded early solved that problem for me (I didn’t write it, but unfortunately I don’t remember where I found it :frowning: ):

function remove_relative_links_in_sitemaps() {
    if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
        return;
    }

    $request_uri = $_SERVER['REQUEST_URI'];
    $extension   = substr( $request_uri, -4 );

    if ( false !== stripos( $request_uri, 'sitemap' ) && in_array( $extension, array( '.xml', '.xsl' ) ) ) {

        remove_filter( 'bloginfo_url', 'Roots\\Soil\\Utils\\root_relative_url', 10 );
        remove_filter( 'the_permalink', 'Roots\\Soil\\Utils\\root_relative_url', 10 );
        remove_filter( 'wp_list_pages', 'Roots\\Soil\\Utils\\root_relative_url', 10 );
        remove_filter( 'wp_list_categories', 'Roots\\Soil\\Utils\\root_relative_url', 10 );
        remove_filter( 'the_tags', 'Roots\\Soil\\Utils\\root_relative_url', 10 );
        remove_filter( 'get_pagenum_link', 'Roots\\Soil\\Utils\\root_relative_url', 10 );
        remove_filter( 'get_comment_link', 'Roots\\Soil\\Utils\\root_relative_url', 10 );
        remove_filter( 'month_link', 'Roots\\Soil\\Utils\\root_relative_url', 10 );
        remove_filter( 'day_link', 'Roots\\Soil\\Utils\\root_relative_url', 10 );
        remove_filter( 'year_link', 'Roots\\Soil\\Utils\\root_relative_url', 10 );
        remove_filter( 'term_link', 'Roots\\Soil\\Utils\\root_relative_url', 10 );
        remove_filter( 'the_author_posts_link', 'Roots\\Soil\\Utils\\root_relative_url', 10 );
        remove_filter( 'wp_get_attachment_url', 'Roots\\Soil\\Utils\\root_relative_url', 10 );

    }
}

add_action( 'init', 'remove_relative_links_in_sitemaps' );
2 Likes

Thanks! The issue persisted for custom taxonomies but adding the following resolved it:

remove_filter( 'get_taxonomies', 'Roots\\Soil\\Utils\\root_relative_url', 10 );
remove_filter( 'get_taxonomy', 'Roots\\Soil\\Utils\\root_relative_url', 10 );

Cheers,
Dan

Did this end up working? We’re experiencing the same issue with the Rank Math SEO plugin and Soil, and can’t get this snippet to work.

Have you tried some of the other suggestions in this thread?

You might also try looking at the code for Rank Math SEO to determine

  1. Which filter or filtered function it’s trying to get an absolute URL from?
  2. If it is a filter/action the priority of that filter/action, and could it be that it’s attaching to the filter/action before the above snippet is removed?

Thanks for the quick response! I ended up commenting out the theme support for relative URLs in Sage’s setup.php file;

//add_theme_support(‘soil-relative-urls’)

That’ll do for now I think, I’m not convinced we need root relative URLs.