Last active
November 29, 2018 18:30
-
-
Save xnau/ba510e7b65693264d32a6750a961749b to your computer and use it in GitHub Desktop.
Making Participants Database list links to external sites open in a new tab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: PDB External List Links | |
* Description: makes Participants Database list links to external sites open in a new tab | |
* Version: 2.0 | |
*/ | |
add_filter( 'pdb-before_display_form_element', 'xnau_alter_external_links', 10, 2 ); | |
/** | |
* adds a target=blank attribute to off-site links | |
* | |
* @param string $html the HTML this will be an empty string at this point | |
* @param PDb_Field_Item $element the field data object | |
* | |
* @return string HTML or blank string to let the plugin create the display | |
*/ | |
function xnau_alter_external_links( $html, $element ) | |
{ | |
/* | |
* we test for the link type field, and see that it has an URL in it | |
*/ | |
if ( $element->form_element() === 'link' ) { | |
// if the link property is set to a URL | |
if ( filter_var( $element->link, FILTER_VALIDATE_URL ) !== false ) { | |
/* | |
* now check to see that the URL does not point to our own site | |
*/ | |
if ( stripos( get_bloginfo('url'), parse_url( $element->link, PHP_URL_HOST ) ) === false ) { | |
// set the target attribute | |
$element->attributes['target'] = '_blank'; | |
} | |
} | |
} | |
return $html; | |
} |
I had to update the plugin to work with the latest version of Participants Database
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. Really helped.