Last active
August 17, 2018 14:56
-
-
Save wphelpdesk-gist/cd22d6311aacc16b3d5c24a1bb2e3e18 to your computer and use it in GitHub Desktop.
The featured image and the product title (the_permalinks) will now link to the External product URL that is set in the WooCommerce Product.
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
add_filter( 'post_type_link', 'wphd_wc_link_permalink_external', 10, 2 ); | |
function wphd_wc_link_permalink_external( $url ) | |
{ | |
global $product; | |
if (!empty($product->product_url)){ | |
$url_external = $product->product_url; | |
return $url_external; | |
}else{ | |
return $url; | |
} | |
} | |
/** | |
* Opens ALL external links in new tabs | |
*/ | |
add_action( 'wp_footer', 'wphd_wc_link_permalink_external_script' ); | |
function wphd_wc_link_permalink_external_script(){ | |
?> | |
<script type="text/javascript"> | |
//<![CDATA[ | |
jQuery(document).ready(function($) { | |
$('a').each(function() { | |
var a = new RegExp('/' + window.location.host + '/'); | |
if(!a.test(this.href)) { | |
$(this).click(function(event) { | |
event.preventDefault(); | |
event.stopPropagation(); | |
window.open(this.href, '_blank'); | |
}); | |
} | |
}); | |
}); | |
//]]> | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment