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?
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?
All problems solved by adding relocation to Submit button 🙂
onclick="location.href = '?p=<?php echo $custom_id; ?>';"
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 () {};
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.