Skip to content

Instantly share code, notes, and snippets.

@lelandf
Last active March 10, 2022 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lelandf/6ff9e9bf91066e09a5dbf6c0b3c8cbee to your computer and use it in GitHub Desktop.
Save lelandf/6ff9e9bf91066e09a5dbf6c0b3c8cbee to your computer and use it in GitHub Desktop.
Open /event/ links in new window
<?php
add_action( 'wp_footer', function() {
// Evaluate conditional here
// Maybe an is_page conditional? https://developer.wordpress.org/reference/functions/is_page/
if ( true === true ) :
?>
<script>
(function () {
// CUSTOMIZE DOMAIN NAME HERE
const DOMAIN_NAME = 'www.example.com';
// Grab all links that start with /event/
const eventLinks = document.querySelectorAll( `a[href^="https://${DOMAIN_NAME}/event/"]` );
// Apply the target="_blank" attribute/value to each link
eventLinks.forEach( link => link.setAttribute( 'target', '_blank' ) );
} )();
</script>
<?php
endif;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment