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