wordpress站内链接跳转到外部链接实现方法

再收藏一篇wordpress站内链接跳转到外部链接的实现方法。今天分享的外链转内链功能,主要是依靠wordpress的自定义域功能来实现。

1、添加外部链接

先给文章添加一个自定义字段用来输入一个要跳转的站外链接(自定义这里不详细介绍,大家可以自行百度)我们添加了一个名为_buy_link的自定义字段,用来输入我们的站外链接。

2、将站内链接重写到主题的go.php处理文件
在主题中新建一个go.php文件,然后在functins.php文件中添加下列代码:
//为不带http的地址添加 http
function addhttp($url) {
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
return $url;
}
/********添加query变量************/
function ashuwp_query_vars($public_query_vars) {
$public_query_vars[] = 'my_custom_page_type';
$public_query_vars[] = 'pid';
return $public_query_vars;
}
/************重写规则*************/
function ashuwp_rewrite_rules( $wp_rewrite ){
$new_rules = array(
'go/?([0-9]{1,})/?$' => 'index.php?my_custom_page_type=buy_page&pid='.$wp_rewrite->preg_index(1),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
/************模板载入规则****************/
function ashuwp_template_redirect(){
global $wp,$wp_query,$wp_rewrite;
if( !isset($wp_query->query_vars['my_custom_page_type']) )
return;
$reditect_page = $wp_query->query_vars['my_custom_page_type'];
if ($reditect_page == "buy_page"){
include(get_template_directory().'/go.php');
die();
}
}
/*********更新重写规则***************/
function ashuwp_flush_rewrite_rules() {
global $pagenow, $wp_rewrite;
if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )
$wp_rewrite->flush_rules();
}
add_action( 'load-themes.php', 'ashuwp_flush_rewrite_rules' ); //启用主题的时候
add_action('generate_rewrite_rules', 'ashuwp_rewrite_rules' ); //添加重写规则
add_action('query_vars', 'ashuwp_query_vars');
add_action("template_redirect", 'ashuwp_template_redirect');
上面的代码将站内所有类似 http://xxx.com/go/xxx 这种地址重写载入go.php文件来处理。

3、添加go.php中的跳转代码。

提供两种方式,任选一种即可。

第一种:通过js来跳转。
<?php
/*******跳转模板*******/
global $wp_rewrite,$wp_query;
//从请求的地址中获取文章ID
if( isset($wp_query->query_vars['pid']) && $wp_query->query_vars['pid']!='' )
$post_id = $wp_query->query_vars['pid'];
else
$post_id = 0;
//如果文章ID存在
if( $post_id ){
//通过文章ID获取要跳转的站外链接--自定义字段
$buy_link = get_post_meta($post_id,'_buy_link',true);
$buy_link = htmlspecialchars_decode($buy_link);//将html实体换回预定义字符
$buy_link = trim($buy_link);
$buy_link = addhttp(trim($buy_link));
if($buy_link){ ?>
<?php //跳转到对应的链接?>
<script type="text/javascript">window.location.href="<?php echo $buy_link;?>";</script>
<?php }else{ ?>
<?php //如果没有设置跳转到奥首页?>
<script type="text/javascript">window.location.href="<?php echo home_url();?>";</script>
<?php }
}else{ ?>
<?php //无ID则跳转到首页 ?>
<script type="text/javascript">window.location.href="<?php echo home_url();?>";</script>
<?php } ?>
第二种,使用wp_redirect函数
<?php
/*******跳转模板*******/
global $wp_rewrite,$wp_query;
//从请求的地址中获取文章ID
if( isset($wp_query->query_vars['pid']) && $wp_query->query_vars['pid']!='' )
$post_id = $wp_query->query_vars['pid'];
else
$post_id = 0;
if( $post_id ){
//获取跳转链接
$buy_link = get_post_meta($post_id,'_buy_link',true);
$buy_link = htmlspecialchars_decode($buy_link);//将html实体换回预定义字符
$buy_link = trim($buy_link);
$buy_link = addhttp(trim($buy_link));
if($buy_link){
//跳转
wp_redirect($buy_link, 301);
}else{
//跳转到首页
wp_redirect(get_bloginfo('url'), 301);
}
}else{
//无ID则重定向到首页
wp_redirect(get_bloginfo('url'), 301);
} ?>
用法:

yiguochun

yiguochun(http://blog.0660hf.com/)0660SEO博客是以seo网站优化,Seo学习,sEO知识,SEO网络推广技巧及站长分享经历的媒体博客,为SEO爱好者和广大站长提供一个SEO案例分享和交流的平台,从分享SEO分享交流过程中带来更多的心满意足。

You may also like...

28 Responses

  1. tadalafil debajo de la lengua cialisle.com cialis effect video.

  2. viagra online说道:

    is sildenafil safer than cialis buy viagra how often can sildenafil be used.

  3. viagra说道:

    is sildenafil safe to buy over the internet viagra prices http://viagrauga.com/ viagrauga.com chocolate better than sildenafil

  4. cialis cost说道:

    generic-tadalafil-tadalafil.com http://www.cialislet.com/ online cialis.
    bula de cialis 20 mg.

  5. tadalafil vor oder nach dem essen einnehmen http://genericalis.com que es el tadalafil professional

  6. musmypsumpDepher说道:

    cialis causes joint pain buy generic viagra at what age is viagra needed

  7. buy female viagra how to buy viagra online viagra for sale [url=http://www.bioshieldpill.com/]online doctor prescription for viagra[/url]

  8. tadalafil hcl说道:

    cialis 5 mg para sirve [url=http://calisgenhea.org]cialis for sale[/url] soft cialis online

  9. musmypsumpDepher说道:

    tadalafil 5 mg mezza pastiglia buy cialis online come si prende tadalafil

  10. how to stop sildenafil email spam viagra online http://viagrapid.com/ buy viagra herbal sildenafil side effects in urdu

  11. musmypsumpDepher说道:

    tadalafil bph trials http://cialisloq.com/ caixa cialis

  12. jrlkqfd tadalafil controindicazioni con altri farmaci http://cialissom.com/ cialis 5mg usa cialis 5 mg best price tadalafil reçete ile mi
    satılıyor

  13. is there any sildenafil for ladies
    viagra without a doctor prescription
    safest way to buy sildenafil online
    [url=http://viagrarow.com/]viagra generico in farmacia[/url]

  14. musmypsumpDepher说道:

    tadalafil 5 mg precios mexico buy cialis tadalafil vs viagra reviews

  15. Rebekkapag说道:

    click now is uk shop

    88fc acheter is pri

  16. Rebekkapag说道:

    generische is uk

    88fc best discount at is

  17. Rebekkapag说道:

    enter site online is fast

    88fc try is free

  18. Kallinikapag说道:

    be is in the us

    441d is prices in england

  19. Kallinikapag说道:

    comprar is en internet

    441d le ru is gnrique

  20. Kallinikapag说道:

    is 5mg vs

    441d good choice is on line

  21. Kristianapag说道:

    look here be is com

    b5db nome remedio ato is

  22. Kristianapag说道:

    sales of is

    b5db is non at from canada

  23. Gordianpag说道:

    effective at is

    be07 no online is

  24. Gordianpag说道:

    levitra gnrique de is

    be07 is tomar media pastilla

  25. Gordianpag说道:

    is jelly uk boots

    be07 is tadalafil belgique

  26. Gordianpag说道:

    starting dose is

    be07 is solubile

  27. Ermilpag说道:

    the original online is

    aa72 do you need rx is soft

  28. Ermilpag说道:

    is at best price

    aa72 is ou acheter en ligne

发表评论