Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2022-05-21 10:45:23

Melonking
Member
Registered: 2022-05-10
Posts: 21
Website

Making outgoing links open in a new tab?

Apologies if this has been asked before but Iv been searching and I cannot find an answer!
Is it possible to make outgoing links open in a new tab (e.g. adding target=”_blank”) to any link that’s not the same domain as textpattern?

Edit to clarify: Outgoing links that are within html formatted articles!

Last edited by Melonking (2022-05-21 11:41:22)


The world will tremble

Offline

#2 2022-05-21 12:03:36

etc
Developer
Registered: 2010-11-11
Posts: 4,662
Website GitHub

Re: Making outgoing links open in a new tab?

The ol’good etc_query would do it by replacing <txp:body /> in the relevant article form with

<txp:etc_query data='<txp:body />' replace='//a[not(starts-with(@href, "<txp:site_url />"))]@@target=_blank' />

Another option is to rewrite link targets client-side via JS. But you’d better do it only once on article save, rather than processing the body on each access. There should be a generic-purpose plugin for this, but I have no reference, sorry. Probably time for a new etc_query version..

Offline

#3 2022-05-21 12:15:00

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,366
Website

Re: Making outgoing links open in a new tab?

Or you can just use a snippet of JavaScript that retrofits the target="blank" to links not on your host on the fly. See for example this snippet

If you only want it to act on links in a particular container, just restrict the link processing to that part of the DOM.


TXP Builders – finely-crafted code, design and txp

Offline

#4 2022-05-21 16:09:03

Melonking
Member
Registered: 2022-05-10
Posts: 21
Website

Re: Making outgoing links open in a new tab?

Aha, thank you! I went with the script option because I’m lazy, but the PHP replacer is really good to know about too.

It would be cool if there was a post processor option that could run on publish that would run arbitrary Javascript code on your published article to make once off lasting changes!


The world will tremble

Offline

#5 2022-05-21 16:40:28

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,366
Website

Re: Making outgoing links open in a new tab?

Melonking wrote #333416:

It would be cool if there was a post processor option that could run on publish that would run arbitrary (Javascript) code on your published article to make once off lasting changes!

There is a callback you can use to hook into an article_saved or article_posted events.* You can use these to post-process the html output before it gets saved to the database, i.e. as a one-off occurrence. I don’t see why it couldn’t do the same for adding target="blank" to links.**

Here’s a very simple example of an admin-side plugin code for converting three underscores to an <hr>:

<?php
// Admin-side callback: act on post or save article

if (txpinterface === 'admin') {
    register_callback('jcr_article_postprocess', 'article_posted');
    register_callback('jcr_article_postprocess', 'article_saved');
}

function jcr_article_postprocess($event, $step, $rs)
{
    if (!($row = safe_row('Body_html', 'textpattern', 'ID='.$rs['ID']))) return;
    extract($row);

    // initialise find/replace arrays
    $patterns = array();
    $replacements = array();

    // ___ -> <hr>
    $patterns[] = '/(<p>\_{3}\s*?<\/p>)/';
    $replacements[] = '<hr>';

    // further pattern-replacement pairs
    // $patterns[] = '/…/';  // regex pattern
    // $replacements[] = '…';  // html output

    // Do all the pattern replacements
    $Body_html_postprocessed = doSlash(preg_replace($patterns, $replacements, $Body_html));

    // re-save body_html
    safe_update('textpattern', "Body_html = '$Body_html_postprocessed'", 'ID = '.$rs['ID']);

}

I use an expanded version of this to give clients textile-like shorthand for accessing shortcode tags and to do things like adding responsive wrappers around tables.

*What I don’t know is if these are ever triggered with your method of posting via XML_RPC. If that triggers its own events, you could use those in place of article_saved and article_posted.

**There is an argument for adding it via javascript. Opening in a new tab or window is in essence a ‘behavioural’ thing that doesn’t need to be intrinsic to the link itself. Personally, I would prefer to be able to decide myself whether to ctrl/command-click or not but I know not everyone feels that way or knows the shortcuts to do that. Some people add an optional “open external links in a new window” tick-box that activates or deactivates the javascript code.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB