dongxiangshen7916 2017-04-19 09:15
浏览 86
已采纳

如何在Wordpress中以编程方式重定向URL?

My situation
Currently I'm working on a plugin for redirect the href url in Wordpress. In the end I would like the plugin is working as followed: First it need to check which href is clicked by a user. Then checks if that url exists in the database(it's called redirect_oude_url). If so, than replace the url with the new url(which is called redirect_nieuwe_url en send the user through to the new url instead of the old url.

I'm using a CPT called redirect. I've made a submenu for adding a new redirect and disabled the add_new function for the CPT with the create_post => do_not_allowcapability. I'm able to check if the old url already exist and based on that able to make a new redirect or not on the admin side.

My question
My question is how I'm able to check when a link is clicked on the frontend of pages/posts if it exist in the database and automatically replace the old url with the new url?

My code

<?php
function webor_redirect_display() {
?>
<div class="wrap">
  <h2>Redirect jouw URL</h2>
</div>
<form method="POST">
  <label>Plaats hieronder zowel de oude als de nieuwe URL en druk op: Sla op.<br></label>
  <br>
  <div class="container_url">
    <b>Oude URL:</b>
    <br><input type="text" class="redirect_oude_url" name="redirect_oude_url" id="redirect_oude_url" placeholder="Begin met: https://" size="150">
    <br><br>
    <b>Nieuwe URL:</b>
    <br><input type="text" class="redirect_nieuwe_url" name="redirect_nieuwe_url" id="redirect_nieuwe_url" placeholder="Begin met: https://" size="150">
    <br><br>
    <input type="submit" name="submit_url" value="Sla op" class="button button-primary button-large">
  </div>
</form>
<?php
$redirect_array = array(
  'post_type' => 'redirect',
  'meta_query' => array(
    'relation' => 'AND',
    array(
      'key' => 'redirect_oude_url',
      'value' => $_POST['redirect_oude_url'],
    ), array(
      'key' => 'redirect_nieuwe_url',
    )
  )
);

$redirect_query = new WP_Query($redirect_array);

if ( $redirect_query->have_posts() ) :
  //the loop
  while ( $redirect_query->have_posts() ) : $redirect_query->the_post();
  $oude_url = get_post_meta(get_the_ID(), 'redirect_oude_url', true);
  $nieuwe_url = get_post_meta(get_the_ID(), 'redirect_nieuwe_url', true);

 endwhile;
 wp_reset_postdata();
 else:
 endif;

 if (isset ($_POST['submit_url']) ) {
  //echo '<br> submit clicked';
  if (!empty ($_POST['redirect_oude_url']) ) {
    //echo '<br> filled oude url';
    if (!empty ($_POST['redirect_nieuwe_url']) ) {
      //echo '<br> filled nieuwe url';
      $post_url = $_POST['redirect_oude_url'];
      //echo '<br>' . $post_url;
      if ($post_url == $oude_url){
        echo '<br> Er bestaat al een redirect voor deze oude url';
      } else {
        //echo '<br> niet hetzelfde, maak nieuwe redirect';
        //webor_create_redirect();
        echo "<strong><h4>Uw redirect is aangemaakt!</h4></strong>";
      }

    } else {
      echo '<br> U bent de nieuwe URL vergeten';
    }
  } else {
    echo '<br> Vul zowel de oude als de nieuwe url in!';
  }
  }

  }//end function

add_shortcode( 'redirect_display', 'webor_redirect_display' );

function webor_create_redirect() {
// Make a new post
$new_redirect = array(
  'post_title' => $_POST['redirect_oude_url'] . ' -> ' . $_POST['redirect_nieuwe_url'],
'post_status' => 'publish',
'post_type' => 'redirect',
'meta_input' => array(
  'redirect_oude_url' => $_POST['redirect_oude_url'],
  'redirect_nieuwe_url' => $_POST['redirect_nieuwe_url'],
)
);
wp_insert_post($new_redirect);
}
?>
  • 写回答

1条回答 默认 最新

  • dongpu1315 2017-04-19 14:18
    关注

    What I think you want to achieve is to add a new redirect in your submenu. Then in the CPT overview you have an overview of all the redirected urls. And then if you open a page/post and that specific page/post is added to the redirect CPT it must automatically redirect to the new url?

    If that is what you're looking for, try something like this:
    I suggest to first get the current url by the following code:

    <?php $current_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
    

    Then you can need to check if the current_url is equal to an old url in the database. If that is true, you can use wp_redirect() as mentioned by @dekts to your new url:

    <?php
    //An array for getting the old url with the value of the current url.
    $url_array = array(
      'post_type' => 'redirect',
      'meta_query' => array(
        'relation' => 'AND',
        array(
          'key' => 'redirect_oude_url',
          'value' => $current_url,
        ), array(
          'key' => 'redirect_nieuwe_url',
        )
      )
    );
    
    $url_query = new WP_Query($url_array);
    
    if ( $url_query->have_posts() ) :
      while ( $url_query->have_posts() ) : $url_query->the_post();
      $get_old_url = get_post_meta(get_the_ID(), 'redirect_oude_url', true);
      $get_new_url = get_post_meta(get_the_ID(), 'redirect_nieuwe_url', true);
    
    endwhile;
    wp_reset_postdata();
    else:
    endif;
    
    //status 302 is default 
    $status = 302; 
    //check if current url is equal to the old url, and then redirect to new url
    if ($current_url == $get_old_url){
      wp_redirect($get_new_url, $status);
      exit;
    }
    ?>
    

    Look here for more info about the status codes.
    This should work, so please give it a try and come back to me if it isn't working.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀