Last active
July 15, 2021 09:41
-
-
Save adamcapriola/21d8956282b77b9c5da088d523d43fa0 to your computer and use it in GitHub Desktop.
Auto-tag Amazon.com links (v1: Raw)
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 | |
/** | |
* Auto-tag Amazon.com links (v1: Raw) | |
* | |
*/ | |
add_filter( 'the_content', 'ac_auto_tag_amazon_links_v1' ); | |
function ac_auto_tag_amazon_links_v1( $content ) { | |
// Variables | |
$amazon_tag = array( 'tag' => 'adamcapr-20', ); // Update with your tracking ID | |
// Add "tag" parameter to Amazon URLs, replacing existing "tag" parameter if present | |
$search = '/a href="(https?:\/\/(?:www)?\.amazon\.com[^"]*)"/'; | |
$replace = 'a href="' . add_query_arg( $amazon_tag, "$1" ) . '"'; // esc_url *should* be used here, but it breaks the URLs for some reason; would need to run second replacement afterward to catch malformed instances of https// and http// | |
$content = preg_replace( $search, $replace, $content ); | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment