Support » Plugin: Absolute Relative URLs » Get it to work in a hosted wordpress

  • Resolved colinguqbio

    (@colinguqbio)


    Hi,

    I tried to use this plugin in a hosted site on wordpress.com. I installed the plugin and tried using all the filters using Code Snippit as well as functions.php in a child theme, but seems none of them worked. Please advice.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author intuitart

    (@intuitart)

    Is it possible that wordpress.com does something to block filters? I’m not aware of it. And I’m unable to test at wordpress.com.

    Otherwise, there isn’t enough information for me to help troubleshoot.

    Have you had it working on a hosted site elsewhere?

    Was the plugin activated?

    Does the plugin work, but not the filters, or is nothing working?

    Have you put something in your code to determine whether or not the filters fire?

    Let me know what you find.

    Andrew P.

    Thread Starter colinguqbio

    (@colinguqbio)

    No sure if wordpress.com is blocking filters, I’m checking with their support team. To answer your questions:

    Have you had it working on a hosted site elsewhere?
    NO

    Was the plugin activated?
    YES

    Does the plugin work, but not the filters, or is nothing working?
    Not sure. The plugin gets activated fine, the filters were added to child theme’s functions.php. I also tried to uncomment the code directly inside absolute-relative-url.php.

    Have you put something in your code to determine whether or not the filters fire?
    No. Please advice how to verify that. Pretty new to wordpress.

    Thanks

    Plugin Author intuitart

    (@intuitart)

    OK first things first. Make sure the plugin is working before trying to modify the default behaviour. In most cases you shouldn’t need to customize it (i.e. no filters required in functions.php in order for the plugin to work).

    If wordpress.com is allowing you to install plugins, it is unlikely they are blocking filters.

    To test your own filters, just echo some text to the browser or save some text to a debug file. Dig in to php to learn those things. When you see the text you display or save, you know the filter is firing.

    Thread Starter colinguqbio

    (@colinguqbio)

    You were right that wordpress.com does not block the filter at all. And I think I misunderstood what your plugin does as I was looking for something that can translate the urls to relative when being viewed/displayed on the UI, and I thought by saving the posts’ url to relative will automatically guarantee them being displayed the same way.

    Plugin Author intuitart

    (@intuitart)

    You can accomplish what you are trying to do with this plugin, that is save and display URLs as relative. This is a scenario where you want to override the plugin’s default behaviour.

    By default, URLs are saved as relative and displayed as absolute. To have them displayed as relative you can disable the conversion back to absolute when they are displayed. But leave the conversion to relative when they are saved in place.

    Add this to functions.php:

    
    add_filter( 'of_absolute_relative_urls_enable_absolute', function() { return false; } );
    

    If I understand your requirement, that should do it.

    Andrew P.

    Thread Starter colinguqbio

    (@colinguqbio)

    Sorry for the late response…yes you understand my requirement perfectly, however what you suggested didn’t work. Tried adding filter ‘of_absolute_relative_urls_enable_absolute’ also tried setting the following in my functions.php but got no luck. Not sure what did I miss.

    
    add_filter( 'of_absolute_relative_urls_related_sites',
    	function( $related_sites ) {
    		$related_sites[]['url'] = MY_URL;
    		return $related_sites;
    	}
    );
    

    I ended up disabling the plugin and add the following function to functions.php to get the url kind of working…but by doing so I might need to override all the other links in addition to ‘post_link’ to make the entire site’s urls relative.

    
    function make_relative(  $url, $post, $leavename ) {
        if ( $post -> post_type == 'post' ) {
            $url = wp_make_link_relative($url);
        }
    	$prefix = '/blog'; // Subdirectory
        return $prefix . $url;
    }
    add_filter( 'post_link', 'make_relative', 10, 3 );
    
    Plugin Author intuitart

    (@intuitart)

    OK I’m not convinced I understand the requirement, especially if your hook into ‘post_link’ resolves it for you.

    Can we back up a step, and would you tell me why you want relative links? i.e. what problem, or situation, are you trying to solve?

    By the way, a relative link for me is a URL without the scheme and domain. For example, an absolute URL of https://www.mydomain.com/wp-content/2019/12/image.jpg would become /wp-content/2019/12/image.jpg when converted to relative. That’s what would be saved in the database using this plugin.

    A special case for WordPress sites where the site itself is in a folder would drop the folder, along with the scheme and domain, when converting to relative. For exampe, an absolute URL of https://www.mydomain.com/blog/wp-content/2019/12/image.jpg would become /wp-content/2019/12/image.jpg when converted to relative, the same as above.

    As long as the WordPress and Site URLs are set properly in the Settings, the relative URL would be converted back to it’s original form when displayed. In the first example https://www.mydomain.com would be prefixed to the relative URL, and in the second example https://www.mydomain.com/blog would be prefixed to the relative URL.

    • This reply was modified 3 years, 2 months ago by intuitart.
    • This reply was modified 3 years, 2 months ago by intuitart.
    Thread Starter colinguqbio

    (@colinguqbio)

    So these are my requirement for using relative url:
    1. I got a website running on https://www.mydomain.com, and I want https://www.mydomain.com/blog to be pointed to the blog hosted on wordpress.com, say https://mydomain.wordpress.com/. It seems WordPress.com doesn’t support subdirectory.
    2. Now let’s say I have a post under url 2019/12/05/post-1/, I want users to access the post through https://www.mydomain.com/blog/2019/12/05/post-1/. However, due of the fact that wordpress saves absolute urls by default, this post is saved and rendered under https://mydomain.wordpress.com/2019/12/05/post-1/. Therefore when user click the link from the “home” page, it will be redirected to https://mydomain.wordpress.com/2019/12/05/post-1/, which is not ideal.

    I have already configured the redirects on my website allow any url on https://www.mydomain.com/blog/* to render content from https://mydomain.wordpress.com/*, which means if I paste the url https://www.mydomain.com/blog/2019/12/05/post-1/ in the browser directly, the post renders property. But when user click any link there it will still go to https://mydomain.wordpress.com/. I think relative url will solve this problem.

    In wordpress.com, I can’t set either Site Address (URL) nor WordPress Address (URL), they are grayed out in the settings.

    Plugin Author intuitart

    (@intuitart)

    Aaaah, ok, now I understand. This is a novel requirement, one I hadn’t considered when developing the plugin. I also understand now why it doesn’t work for you. The plugin relies on the WordPress and Site URLs to work. And unfortunately, there isn’t currently a filter to override those. But that is what you need to do.

    If you are willing to hack the code, you could override those.

    In the set_vars() function, add the following lines immediately after the code where those variable are currently set:

    
    self::$wpurl = "https://www.mydomain.com/blog";
    self::$url = "https://www.mydomain.com/blog";
    

    Now your domain will get used when building the absolute URL for display in the browser. However, changing this also means the wordpress.com domain will not be removed when saving relative URLs in the databases. That’s where the relates_sites filter can help. In functions php, add the following filter:

    
    add_filter( 'of_absolute_relative_urls_related_sites',
      function( $related_sites ) {
        $related_sites[]['url'] = "https://mydomain.wordpress.com";
        return $related_sites;
      }
    );
    

    Now the wordpress.com domain will get removed even though it is no longer the WordPress or Site URL.

    Hope this helps. Let me know how you make out.

    Andrew P.

    Thread Starter colinguqbio

    (@colinguqbio)

    Works perfectly! Thank you so much for the help Andrew!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Get it to work in a hosted wordpress’ is closed to new replies.