Updating the Link of a page - WordPress - wordpress

I want to edit this link via wordpress
http://domain-name.com/self-coaching-tips/media/
when I click on edit, I am able to edit the MEDIA part only. I want to update the whole link to
http://domain-name.com/media/
How can I do this?

Add to your functions.php file:
function cstm_url_redirects() {
$redirect_rules = array(
array('old'=>'/self-coaching-tips/media/','new'=>'/media/'),
//array('old'=>'/some-other-old/page/','new'=>'/some/new/page/'),
);
foreach( $redirect_rules as $rule ) :
// if URL of request matches with the one from the array, then redirect
if( urldecode($_SERVER['REQUEST_URI']) == $rule['old'] ) :
wp_redirect( site_url( $rule['new']) , 301 );
exit();
endif;
endforeach;
}
add_action('template_redirect', 'cstm_url_redirects');
More examples here

Related

Non-existent category url works with "Shop base with category"

I set "Shop base with category" option in the Permalink Settings.
Now my product URLs are like that:
site.com/shop/category1/product1
However, this URL also goes to product1:
site.com/shop/randomword/product1
There is no category such "randomword" and also site.com/shop/randomword/ gives 404 error but the URL above works. It does not redirect to /category1/product1, it just works.
Therefore remove_filter( 'template_redirect', 'redirect_canonical' ); is not working. It works for other posts but not products.
Is there any way to prevent this?
I found a solution for this.
The page works with a random category URL. So when the URL is not equal to permalink I decide to redirect to permalink. So when there is a random URL, it redirects to actual URL.
<?php
global $wp;
$current_url = home_url( $wp->request );
$actual_url = get_permalink();
if ((is_product()) && ($current_url !== $actual_url)) {
wp_redirect( $actual_url, 301 );
exit();
} ?>
You can put this code in the header.php
When you enter
site.com/shop/randomword/product1
it redirects to
site.com/shop/category1/product1
Edit:
The code breaks the draft preview. Need to specify it is published.
global $wp;
$current_url = home_url( $wp->request );
$actual_url = get_permalink();
$current_post_status = get_post_status();
if ((is_product()) && ($current_post_status == 'publish' ) && ($current_url !== $actual_url)) {
wp_redirect( $actual_url, 301 );
exit();
}

How can I redirect users on the new 404 page without plugin?

I have a 404 page in my theme but I am not using that page. I have created a new 404 page in WordPress using wpbakery page builder. I need to know how can I redirect users on the new 404 page without a plugin?
You can use plugin 404page.
Or some code adapted from this plugin:
add_filter(
'404_template',
static function () {
global $wp_query;
$wp_query = new WP_Query();
$wp_query->query('page_id='.$pageID);
$wp_query->the_post();
$template = get_page_template();
rewind_posts();
add_filter(
'body_class',
static function ($classes) {
if (!in_array('error404', $classes, true)) {
$classes[] = 'error404';
}
return $classes;
}
);
return $template;
},
999
);
Crate 404page in the admin.
create a custom page template for that page.
add your custom 404 content
open 404.php file in your theme.
add this below code at the top of that file.
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".home_url('/404page/'));
exit();
try to find something that not found and you will be redirected to your custom 404 page
also, you can try this action hooks for redirect to custom 404 page. put this code in your function.php file. this is the replace option of above point number 5)
add_action( 'template_redirect', 'redreict_to_custom_404_page' );
function redreict_to_custom_404_page(){
// check if is a 404 error
if( is_404() ){
wp_redirect( home_url( '/404page/' ) );
exit();
}
}
Or if you want to create 404 page with WP bakery
Create a private 404page and build with WP bakery in the admin.
open 404.php file and get content of 404page by code below
$page_id = 123; // 404page id
$page = get_post( $page_id );
$content = $page->post_content;
echo $content;

Wordpress redirect if URL parameter is empty for each post

So there are people who access my website directly (not thru the tracking link I got with Voluum), thus they are not able to click the links and I can't see them as a part of my stats.
How can I redirect users who don't have a /?voluumdata=BASE64... URL parameter to a tracked URL, and to have a different redirect for each blog post?
I was testing and looking for a plugin / .htaccess trick for hours but nothing seemed to help.
Edit: I found a solution that I was certain is going to work but for some reason it didn't:
[insert_php]
if(empty($_GET['voluumdata']))
{
header('Location: REDIRECT_URL');
exit;
}
[/insert_php]
Also tried:
[insert_php]
if(!isset($_GET['voluumdata']))
{
header('Location: REDIRECT_URL');
exit;
}
[/insert_php]
Both just break the page loading proccess.
Unfortunately, I cannot understand what is the purpose of the code you have entered in your question. I mean that is not clear the reason you use the tags [insert_php].
A solution to your problem it can be the following.
function redirect_direct_access( ) {
// You may use the code:
//
// global $wp_query
//
// in order to determine in which pages you should run your
// redirection code. If you only check for the token existence
// then you will be faced with redirection loop. I don't explain in
// depth how to use the $wp_query as it is not part of your question
// but you always have the opportunity to check what is the contents
// of this variable using the code:
//
// echo "<pre>";
// print_r( $wp_query );
// echo "</pre>";
//
// This way you will be able to build your specific if statement
// for the page you like to test.
if (
! isset( $_GET[ 'voluumdata' ] ) ||
empty( $_GET[ 'voluumdata' ] )
) {
wp_redirect( home_url( '/page/to/redirect/' ) );
exit();
}
}
add_action( 'template_redirect', 'redirect_direct_access' );
You can find more information in the WordPress documentation related to the template_redirect hook.
Just in case anyone would face the same issue, #Merianos Nikos has given a half-answer and I mastered it into this:
function redirect_direct_access( ) {
$post_id = get_the_ID();
if (
$post_id == POST_ID &&
!isset( $_GET[ 'voluumdata' ] )
) {
wp_redirect( 'REDIRECT_URL' );
exit();
}
if (
$post_id == POST_ID &&
!isset( $_GET[ 'voluumdata' ] )
) {
wp_redirect( 'REDIRECT_URL' );
exit();
}
}
add_action( 'template_redirect', 'redirect_direct_access' );

How to redirect on particular page in wordpress?

<?php
if($result_array['ACK']== 'Success')
{
wp_redirect( get_page_by_title( 'thank-you' ) );
}else{
//redirect like php
header("Location : http://localhost/mysite/faq");
} ?>
My query is working but I have no idea that how I simply redirect in my page and display any my page content data in wordpress. So please help me.
You should pass the actual title into that function, not the slug:
wp_redirect( get_page_by_title( 'Thank You' ) );

Header location doesn't work in my wordpress plugin.

I'm writing an Wordpress plugin. With this plugin I update some data. The query and updating works fine, but my header("location: url"); doesn't work. If I place an echo, it won't give any error that the headers already send. It looks it doesn't do anything with those lines. My code...
<?php require_once('../../../wp-config.php');
$baanstatus_table=$wpdb->prefix . 'baanstatus';
$id = $_GET['id'];
$bijgewerkt =$_GET['bijgewerkt'];
$baanstatus= $_GET['baanstatus'];
$handicarts = $_GET['handicarts'];
$trolleys = $_GET['trolleys'];
$winterontheffing = $_GET['winterontheffing'];
$zomergreens = $_GET['zomergreens'];
$qualifying = $_GET['qualifying'];
$onderhoud_greens = $_GET['onderhoud_greens'];
$onderhoud_anders = $_GET['onderhoud_anders'];
$opmerkingen = $_GET['opmerkingen'];
global $wpdb;
$data_array =array('id' => $id,
'bijgewerkt' => $bijgewerkt,
'baanstatus' => $baanstatus,
'handicarts' => $handicarts,
'trolleys' => $trolleys,
'winterontheffing' =>$winterontheffing,
'zomergreens' =>$zomergreens,
'qualifying' =>$qualifying,
'onderhoud_greens' =>$onderhoud_greens,
'onderhoud_anders' =>$onderhoud_anders,
'opmerkingen' =>$opmerkingen
);
$where =array('id' => $id);
$wpdb->update( $baanstatus_table, $data_array, $where );
header("location:http://almeerderhout.fcklap.com/wp-admin/options-general.php?page=my-unique-identifier");
exit();
?>
Perhaps you should try the javascript, instead of PHP location.
<?php
echo '<script>location.href="http://almeerderhout.fcklap.com/wp-admin/options-general.php?page=my-unique-identifier";</script>';
?>
The below code will help you
<?php
wp_redirect( $location, $status );
exit;
?>
The above wordpress function will use to redirect Codex Link function reference
You should hook your plugin to a proper Wordpress action to avoid the "headers already sent error" when trying to redirect.
I found that a good place to perform redirects is the template_redirect action, so you can write something like this:
function do_something_then_redirect() {
// do something with $_GET or $_POST data
// then redirect to some url defined in the $redirect_url variable
wp_redirect($redirect_url);
die;
}
add_action('template_redirect', 'do_something_then_redirect');

Resources