admin管理员组文章数量:1122832
My goal:
I want external links to shops I'm affiliated with (for example amazon) to automatically get the affiliate ID attached to it.
Example:
link I put in my blog post editor /
changes in frontend to /?tag=myafftag-02
What I have found:
The creator of the theme I'm using is offering something very close to what I'm looking for:
add_action('wpfepp_form_actions', 'link_change_custom');
function link_change_custom($data){
if ( !empty( $_POST['rehub_offer_product_url'] ) ) {
$url = $_POST['rehub_offer_product_url'];
$checkdomain = 'amazon';
if (!empty($url) && strpos($url, $checkdomain) !== false) :
$afftag = 'myafftag-02'; //our affiliate ID
$affstring = 'tag='; // url parameter for affiliate ID
if (parse_url($url, PHP_URL_QUERY)): //check if link has query string
if (strpos($affstring, $url) !== false) : //check if link already has affiliate ID
$url = preg_replace("/(".$affstring.").*?(\z|&)/", "$1".$afftag."$2", $url);
else:
$url = $url.'&'.$affstring.$afftag;
endif;
else:
$url = $url.'?'.$affstring.$afftag;
endif;
endif;
update_post_meta( $data['post_id'], 'rehub_offer_product_url', esc_url($url) );
}
}
The problem:
It only works on links that are stored in the field "rehub_offer_product_url", but I want it to work on any links to amazon I put in the content of my blog posts.
I appreciate any help.
My goal:
I want external links to shops I'm affiliated with (for example amazon) to automatically get the affiliate ID attached to it.
Example:
link I put in my blog post editor https://www.amazon.com/dp/B07K344J3N/
changes in frontend to https://www.amazon.com/dp/B07K344J3N/?tag=myafftag-02
What I have found:
The creator of the theme I'm using is offering something very close to what I'm looking for:
add_action('wpfepp_form_actions', 'link_change_custom');
function link_change_custom($data){
if ( !empty( $_POST['rehub_offer_product_url'] ) ) {
$url = $_POST['rehub_offer_product_url'];
$checkdomain = 'amazon.com';
if (!empty($url) && strpos($url, $checkdomain) !== false) :
$afftag = 'myafftag-02'; //our affiliate ID
$affstring = 'tag='; // url parameter for affiliate ID
if (parse_url($url, PHP_URL_QUERY)): //check if link has query string
if (strpos($affstring, $url) !== false) : //check if link already has affiliate ID
$url = preg_replace("/(".$affstring.").*?(\z|&)/", "$1".$afftag."$2", $url);
else:
$url = $url.'&'.$affstring.$afftag;
endif;
else:
$url = $url.'?'.$affstring.$afftag;
endif;
endif;
update_post_meta( $data['post_id'], 'rehub_offer_product_url', esc_url($url) );
}
}
The problem:
It only works on links that are stored in the field "rehub_offer_product_url", but I want it to work on any links to amazon I put in the content of my blog posts.
I appreciate any help.
Share Improve this question asked Feb 3, 2020 at 20:54 AlinaAlina 212 bronze badges2 Answers
Reset to default 0The code you found works if you're using the Classic Editor. It takes POST data (as in, a POST HTTP request) and converts it before it enters the database. So, in addition to only updating a certain meta field, it also only works from the point you add the code forward. Any old links aren't affected.
Assuming you probably want to affect existing links as well as new ones, you can filter the_content
instead. This will work in the Classic Editor too, but also with the Block Editor.
This should put you on the right path - you'll just need to work out the right regular expression to identify an Amazon link's query string. (If you already know that none of your Amazon links have query strings, you can skip the regular expression completely and just go ahead and do the str_replace()
step.)
<?php
// Add a filter to `the_content`, which is called when pulling the post data out of the database
add_filter('the_content', 'wpse_357790_add_amazon_affiliate');
function wpse_357790_add_amazon_affiliate($content) {
// See if "amazon.com" is found anywhere in the post content
if(strpos($content, 'amazon.com') !== false) {
// Capture all Amazon links and their query strings
// TO DO: work out the correct regex here. This next line is not complete.
preg_match_all('\?tag=[^&]+(&|")', $content, $matches);
// Add the affiliate query string to each URL
foreach($matches as $url) {
$content = str_replace($url, $url . '?tag=myafftag-02', $content);
}
}
// Always return the content, even if we didn't change it
return $content;
}
?>
Benefits: will work with either editor, filters all post content, works on all content - already-published and not-yet-created.
I needed the same automatic capability on one of my sites: the need to change any Zon link to include my affiliate code, whether that was a link in a post, page, or comment. Couldn't find one that did that, so built my own plugin.
It's called "AmazoLinkenator" here https://wordpress.org/plugins/amazolinkenator/ . You can look at the code inside if you want to 'fork' it, of just use the plugin. (I note that I should change the 'tested to' value to 5.3; it works just fine in current WP version.) Support via the plugin page.
But, all you need is the proper regex to find/replace any Zon links. (I had to add code to also process shortened URLs.) There's about 80-100 lines of code to do the replace.
Plus, if you have a Bit.ly API code, it will shorten the URLs it finds. Only works on Zon URLs.
本文标签: phpChange links automatically to affiliate links
版权声明:本文标题:php - Change links automatically to affiliate links 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289492a1928316.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论