I sometimes get requests to have Participants Database links to locations off the website open in a new window or tab. Depending on the context, this can be inadvisable from a user experience point of view, but even so, we need to be able to set that up sometimes. So, with that caveat in mind, this is how we do it…
To alter the way database data is shown, we use the ‘pdb-before_display_form_element’ filter. It gives us the field object which contains all the data for the field, and expects the HTML in return. If we want the plugin to do it’s thing normally, we return an empty string.
This filter is applied to all fields before getting displayed, so we want to be sure to only alter the ones we want changed. We need to determine if the field is a link field and that it has a link to an external location. If not, return the empty string.
This plugin is very simple: create a new file with the name
pdb_external_links.php and code below. Upload it to your plugins folder and activate.
Pro tip: click on the filename in the code window above, then download the .zip file and install it.
That’s it, all external links will open in a new tab or window when the user clicks on them.
(Note: this post was edited for better code on Nov 30, 2016)