• Hello,

    I’m building a WordPress website using WPML, Beaver Builder, and several other plugins. I aim to implement relative URLs across the entire site while creating multiple new pages and custom post types. To achieve this, I’ve applied various filters.

    However, when I used the post_type_link filter as shown below:

    add_filter('post_type_link', 'convert_custom_post_type_links_to_relative', 10, 2);

    I noticed that the GUID values for my custom post types were also converted to relative paths. According to developer forums, modifying the GUID is not recommended, as it can cause issues.

    My goal is to ensure all links throughout the site are relative without having to rely on extensive search-and-replace operations, especially since my database is quite large (approximately 8-9 GB). and moving migration from dev , stage and prod environment. How can I achieve this while avoiding unintended changes to the GUID field?

    Thanks,

    Vipul Jadvani.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Subrata Debnath

    (@subrata-deb-nath)

    Hi @vipuljadvani,

    To implement relative URLs throughout your WordPress site without unintentionally modifying the GUID field, you’ll need a targeted approach. The GUID field should always remain an absolute URL because it is meant to serve as a unique identifier for posts. Here’s how you can adjust your setup:

    1. Adjust the post_type_link Filter

    The post_type_link filter can be used to make the permalink for custom post types relative, but this filter does not directly affect the GUID field unless another part of your code is overriding it. Ensure your filter function is specific to permalinks.

    Here’s an improved implementation of the post_type_link filter:

    add_filter('post_type_link', 'convert_custom_post_type_links_to_relative', 10, 2);

    function convert_custom_post_type_links_to_relative($permalink, $post) {
    // Ensure the $permalink is an absolute URL and belongs to your site.
    if (strpos($permalink, home_url()) !== false) {
    $permalink = wp_make_link_relative($permalink);
    }
    return $permalink;
    }

    2. Ensure GUID is Not Modified

    Verify that no other filter is modifying the GUID. A typical function modifying GUID might look like this:

    add_filter('get_post_metadata', function($value, $object_id, $meta_key) {
    if ($meta_key === '_guid') {
    return null; // Ensure this returns null to avoid modifications to the GUID.
    }
    return $value;
    }, 10, 3);

    3. Use the wp_make_link_relative Function

    WordPress includes the wp_make_link_relative function, which safely converts absolute URLs to relative ones. Use this function wherever you’re generating links (e.g., in templates, widgets, etc.) to ensure consistency.

    4. Avoid Database Modifications

    Instead of modifying the database directly, rely on filters to dynamically adjust URLs for display purposes. This approach ensures that the GUID and other sensitive fields remain untouched.

    For example:

    • Use the_permalink filter to ensure links in posts and pages are relative:
    add_filter('the_permalink', 'make_permalink_relative');
    function make_permalink_relative($permalink) {
    return wp_make_link_relative($permalink);
    }

    When migrating between environments (e.g., development, staging, production), tools like WP Migrate DB Pro or DUPLICATOR can handle search-and-replace operations for URLs in the database. These tools avoid touching fields like GUID unless explicitly configured to do so.

    Let me know if you face further issues.

    Thread Starter vipuljadvani

    (@vipuljadvani)

    Hi @subrata-deb-nath ,

    Thank you for your input. However, when I perform an auto-search in Beaver Builder, it doesn’t seem to work as expected. I’ve attached a screenshot for reference.

    When I try to create a link from one post to another, the URLs are still displayed as absolute instead of relative. I would like these URLs to appear as relative, not absolute.

    I have added as per below code :

    add_action('fl_builder_before_render', 'convert_beaver_builder_urls');

    function convert_beaver_builder_urls($settings) {
    $site_url = home_url();

    // Recursively replace URLs in all settings
    array_walk_recursive($settings, function (&$value) use ($site_url) {
    if (is_string($value) && strpos($value, $site_url) === 0) {
    $value = wp_make_link_relative($value);
    }
    });

    return $settings;
    }

    add_filter('post_type_link', 'convert_custom_post_type_links_to_relative', 10, 2);

    function convert_custom_post_type_links_to_relative($permalink, $post) {
    // Ensure the $permalink is an absolute URL and belongs to your site.
    if (strpos($permalink, home_url()) !== false) {
    $permalink = wp_make_link_relative($permalink);
    }
    return $permalink;
    }


    add_filter('get_post_metadata', function($value, $object_id, $meta_key) {
    if ($meta_key === '_guid') {
    return null; // Ensure this returns null to avoid modifications to the GUID.
    }
    return $value;
    }, 10, 3);
    add_filter('the_permalink', 'make_permalink_relative');
    function make_permalink_relative($permalink) {
    return wp_make_link_relative($permalink);
    }

    function convert_absolute_to_relative_in_beaver_settings($settings, $id) {
    // Loop through all settings
    foreach ($settings as $key => $value) {
    // Check if the setting is a URL and convert it to relative
    if (is_string($value) && filter_var($value, FILTER_VALIDATE_URL)) {
    $settings->$key = wp_make_link_relative($value);
    }
    }

    return $settings;
    }
    add_filter('fl_builder_node_settings', 'convert_absolute_to_relative_in_beaver_settings', 10, 2);

    add_filter('post_link', 'wp_make_link_relative', 10, 3);
    add_filter('page_link', 'wp_make_link_relative', 10, 2);
    add_filter('attachment_link', 'wp_make_link_relative', 10, 2);

    Thanks,

    Vipul

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.