繁体   English   中英

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

[英]How do I redirect URL programmatically in Wordpress?

我的情况
目前我正在开发一个插件,用于重定向Wordpress中的href url。 最后我想插件的工作方式如下:首先需要检查用户点击了哪个href。 然后检查数据库中是否存在该URL(称为redirect_oude_url )。 如果是这样,那么将url替换为新的url(称为redirect_nieuwe_url将用户发送到新的url而不是旧的url。

我正在使用称为redirect的CPT。 我已经创建了一个子菜单,用于添加新的重定向,并使用create_post => do_not_allow功能禁用CPT的create_post => do_not_allow功能。 我能够检查旧网址是否已经存在,并且基于能够在管理端进行新的重定向。

我的问题
我的问题是,如果在页面/帖子的前端点击链接,如果它存在于数据库中并自动用新网址替换旧网址,我该如何检查?

我的代码

<?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);
}
?>

我认为您想要实现的是在子菜单中添加新的重定向。 然后在CPT概述中,您可以概览所有重定向的网址。 然后,如果您打开一个页面/帖子并且该特定页面/帖子被添加到重定向CPT,它必须自动重定向到新的URL?

如果这是你正在寻找的,尝试这样的事情:
我建议先通过以下代码获取当前网址:

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

然后,您可能需要检查current_url是否等于数据库中的旧URL。 如果是这样,您可以使用@dekts提到的wp_redirect()到您的新网址:

<?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;
}
?>

在此处查看有关状态代码的更多信息。
这应该有效,所以请试一试,如果不能正常工作,请回复我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM