Redirect to custom ...
 
Notifications
Clear all

Redirect to custom URL

5 Posts
3 Users
0 Reactions
1,955 Views
(@sasostebe)
New Member
Joined: 5 years ago
Posts: 3
Topic starter  

Hello!
I'm bringing a variable to the comment form (hidden field), let's call it $custom_id.
I am trying to redirect user to custom URL after posting, including this custom id. I found this code on your support:

    public function redirect() {
$messageArray = array('code' => 0);
$commentId = isset($_POST['commentId']) ? intval($_POST['commentId']) : 0;
if ($this->optionsSerialized->redirectPage && $commentId) {
$comment = get_comment($commentId);
if ($comment->comment_ID) {
$messageArray['code'] = 1;
$messageArray['redirect_to'] = get_permalink($this->optionsSerialized->redirectPage);
}
}
$this->commentsArgs['caller'] = '';
wp_die(json_encode($messageArray));
}

I tried replacing this line:

$messageArray['redirect_to'] = get_permalink($this->optionsSerialized->redirectPage);

With this:

$messageArray['redirect_to'] = 'mydomain.com/?p=' . $_POST['custom_id'];

But it's not working. It redirects to mydomain.com. Any ideas?


   
Quote
(@sasostebe)
New Member
Joined: 5 years ago
Posts: 3
Topic starter  

Another problem I'm facing is that this redirection is taking too much time - i would like to redirect user as soon as possible after hitting the send button.

So I tried making a redirect in addComment() function, but that's not working either...

Is there a better way?


   
ReplyQuote
(@sasostebe)
New Member
Joined: 5 years ago
Posts: 3
Topic starter  

All problems solved by adding relocation to Submit button 🙂

onclick="location.href = '?p=<?php echo $custom_id; ?>';"

   
ReplyQuote
Astghik
(@astgh)
Illustrious Member Admin
Joined: 7 years ago
Posts: 6193
 

@sasostebe

Thank you for using wpDiscuz and for contacting us.

Our developers checked your solution. They may suggest another more convenient solution for you. If you decide to use the solution provided by us follow the steps:

1. Put the following code in the active theme functions.php file:

add_filter('wpdiscuz_comment_post', function ($response) {
   $response["callbackFunctions"][] = "customRedirect";
   return $response;
});

2. Below is provided the javascript code, that should be added in your active theme js files and you'll just need to add your custom redirect js code:

wpdiscuzAjaxObj.customRedirect = function () {};

   
ReplyQuote
(@galewinston)
New Member
Joined: 5 years ago
Posts: 1
 
Posted by: @sasostebe

onclick="location.href = '?p=<?php echo $custom_id; ?>';"

Use  window.location.replace('http://w3c.com');

It’s better than using window.location.href = 'http://w3c.com';

Using replace() is better for redirect, because it does not keep the originating page in the session history, meaning the user won’t get stuck in a never-ending back-button fiasco.


   
ReplyQuote
Share:
Scroll to top