【问题标题】:How do I redirect URL programmatically in Wordpress?如何在 Wordpress 中以编程方式重定向 URL?
【发布时间】:2017-09-15 10:12:24
【问题描述】:

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

我正在使用名为 redirect 的 CPT。我为添加新重定向创建了一个子菜单,并使用create_post => do_not_allowcapability 禁用了 CPT 的 add_new 功能。我可以检查旧网址是否已经存在,并根据该网址是否能够在管理员端进行新的重定向。

我的问题
我的问题是我如何能够检查在页面/帖子的前端单击链接是否存在于数据库中并自动将旧 url 替换为新 url?

我的代码

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

【问题讨论】:

  • 你可以使用:wp_redirect($url);退出;
  • @dekts 我如何在每个页面/帖子前端页面上实现此功能?需要将此代码添加到每个文件中,我可以将其添加到我的插件文件中吗?
  • 在@Stan 周围做一些谷歌并检查如下内容:developer.wordpress.org/reference/functions/wp_redirect
  • 我会这样做的,希望你把我引向正确的方向! @dekts

标签: php wordpress redirect


【解决方案1】:

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

如果这是您要找的东西,请尝试以下方法:
我建议先通过以下代码获取当前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;
}
?>

查看here 了解有关状态代码的更多信息。
这应该可以,所以请试一试,如果它不起作用,请回复我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-31
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多