Jump to content

Prevent links to current site from having protocol and host


DrQuincy
 Share

Recommended Posts

Is there any way in a CKEditor field to have links to the current host automatically converted to root relative links?

E.g. I'm on domain.com and I add this code in source:

<a href="https://www.domain.com/foo">Foo</a>

And on save or in the front end it converts it to:

<a href="/foo">Foo</a>

Any other links would stay as is.

PW seems to be good at using root relative links (e.g. if you select a page from the link CKEditor dialog) but wondered if this is possible without having to do a find/replace in the database. I'm thinking of instances where you move from development to product and end up absolute links to the development domain.

Link to comment
Share on other sites

I'm not totally sure about the HTML Options in the textarea settings if they might already do that or just look for changes.

2021-02-05_21-33.png.575b7eae3228848667463ad1968e0465.png

But ProCache is able to do that (never tested to be honest).

Edit: I have to revoke this. ProCache does not change any <a href="">.

2021-02-05_21-34.thumb.png.a0d5293bd1365fff5354931f8c9fc2e7.png

  • Like 1
Link to comment
Share on other sites

Do you mean that editors are pasting absolute internal links into the link dialog in CKEditor? If so you could try an education approach first, i.e. "Please create internal links via the fields in the link dialog". That way you won't get absolute internal links in the first place.

Beyond that you could use a saveReady hook to do a string replacement in the relevant field(s):

$pages->addHookAfter('saveReady', function(HookEvent $event) {
	$page = $event->arguments(0);
	// Perhaps limit by template as needed
	if($page->template == 'basic-page') {
		// Internal links: replace absolute href with relative href
		$page->body = str_replace('href="https://www.domain.com/', 'href="/', $page->body);
	}
});

 

  • Like 3
Link to comment
Share on other sites

  • 2 weeks later...

In case anyone is interested this will remove absolute links on all specified HTML (CKEditor Textarea) fields using the current host (HTTP and HTTPS) and is case-insensitive. Based on @Robin S’s code.
 

$wire->addHookAfter('Pages::saveReady', function(HookEvent $event) {
    
    $htmlFields = [ // NOTE: Set your fields here
        
        'html1',
        'html2',
        
    ];
    
    $host = $_SERVER['HTTP_HOST'];
    $find = [
        
        'href="http://'  . $host . '/',
        'href="https://' . $host . '/',
        
    ];
    
    $page = $event->arguments(0);
    
    foreach ($htmlFields as $htmlField) {
        
        if ($page->$htmlField !== null) {
            
            $numChanged = 0;
            
            foreach ($find as $f) {
                
                $numChanged += mb_substr_count(mb_strtolower($page->$htmlField), $f);
                
                $page->$htmlField = str_ireplace($f, 'href="/', $page->$htmlField);
                
            }

            if ($numChanged > 0) {
                
                $this->message($numChanged . ' ' . $host . ' link(s) converted to relative links for field “' . $htmlField . '”');
                
            }
            
        }
        
    }
    
});

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...