gpt4 book ai didi

php - Wordpress:自动更改帖子中的特定 URL

转载 作者:行者123 更新时间:2023-12-03 11:22:49 24 4
gpt4 key购买 nike

我找到了一个解决方案来更改我的 wordpress 主题中的链接,但不是内容中的链接。如何获取内容中的 URL,以便我也可以更改它们?

我需要使用 the content筛选。但是如何更改像 apple.com/test/apple.com/test-123/、apple.com、microsoft.com、microsoft.com/test/这样的 URL。该函数还应该正确更改内容中每个匹配的 URL。

add_filter('the_content ', 'function_name');

answer of a similiar question不幸的是不起作用。

这是我更改链接的工作解决方案,但不是内容中的链接。

add_filter('rh_post_offer_url_filter', 'link_change_custom');
function link_change_custom($offer_post_url){

$shops= array(
array('shop'=>'apple.com','id'=>'1234'),
array('shop'=>'microsoft.com','id'=>'5678'),
array('shop'=>'dell.com','id'=>'9876'),
);
foreach( $shops as $rule ) {
if (!empty($offer_post_url) && strpos($offer_post_url, $rule['shop']) !== false) {
$offer_post_url = 'https://www.network.com/promotion/click/id='.$rule['id'].'-yxz?param0='.rawurlencode($offer_post_url);
}
}
$shops2= array(
array('shop'=>'example.com','id'=>'1234'),
array('shop'=>'domain2.com','id'=>'5678'),
array('shop'=>'domain3','id'=>'9876'),
);
foreach( $shops2 as $rule ) {
if (!empty($offer_post_url) && strpos($offer_post_url, $rule['shop']) !== false) {
$offer_post_url = 'https://www.second-network.com/promotion/click/id='.$rule['id'].'-yxz?param0='.rawurlencode($offer_post_url);
}
}

return $offer_post_url;
}

最佳答案

如果我理解正确,那就是你所需要的

add_filter( 'the_content', 'replace_links_by_promotions' );
function replace_links_by_promotions( $content ) {
$shop_ids = array(
'apple.com' => '1234',
'microsoft.com' => '5678',
'dell.com' => '9876',
);

preg_match_all( '/https?:\/\/(www\.)?([-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6})\b([-a-zA-Z0-9()@:%_\+.~#?&\/=]*)/', $content, $matches, PREG_OFFSET_CAPTURE );

foreach ( $matches[2] as $index => $match ) {
if ( ! isset( $shop_ids[ $match[0] ] ) ) {
continue;
}

$offer_post_url = 'https://www.network.com/promotion/click/id=' . $shop_ids[ $match[0] ] . '-yxz?param0=' . rawurlencode( $matches[0][ $index ][0] );
$content = substr_replace( $content, $offer_post_url, $matches[0][ $index ][1], strlen( $matches[0][ $index ][0] ) );
}

return $content;
}

关于php - Wordpress:自动更改帖子中的特定 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60829912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com