Jump to content

Prevent links to current site from having protocol and host


Recommended Posts

Posted

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.

Posted

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
Posted

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
Posted

Thanks @wbmnfktr I think the Markup/HTML option refers to the where internal links go to rather than the format.

Thanks @Robin S. I do often add that in the notes but it does seem to confuse some users — of they just don't read it. I like your hook idea though; that could be very effective.

  • Like 2
  • 2 weeks later...
Posted

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 . '”');
                
            }
            
        }
        
    }
    
});

 

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
  • Recently Browsing   0 members

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