The effects of alcohol on sustanon 250 leucine for – real weight loss & bodybuilding benefits?
Redirect to new pos...
 
Share:
Notifications
Clear all

Question [Solved] Redirect to new post after comment submit from action hook

3 Posts
2 Users
2 Reactions
826 Views
Posts: 3
Topic starter
(@dc10000)
Active Member
Joined: 1 year ago

I suspect this has an easy solution but I'm banging my head and hopefully someone willing to kindly help me out.

Quick version:

Is there a wpdiscus action hook that fires after a comment has saved, that will also allow for a javascript page redirect without the "unsaved changes" popup confirmation dialog firing? I've tried wpdiscuz_after_comment_post but the page believes that there are unsaved changes no matter what I do.

Longer version with details:

I have a post called "New Topic" that I plan to use as a special spot where users can submit to any Topic based on a query_var in the URL (so ?topic_id=345 etc.)

I have a function working using the standard WP 'comment_post' action, which basically looks for the topic id, then updates the comment comment_post_ID to whatever topic post the comment should be moved to.

All that works, but I want to redirect after the comment is saved, to the topic post where the new comment has ben assigned.

PHP redirect won't work because the headers have been sent. I tried echoing javascipt with window.href= etc., and that does work, but I get the popup confirmation that there are unsaved changes on the page. I need to avoid that.

Any suggestions? Any built in redirect hooks that I can leverage maybe?

Thanks!

2 Replies
Asti
Posts: 7540
 Asti
Support
(@asti)
Illustrious Member
Joined: 7 years ago

First solution

We may suggest you check out the wpDiscuz built-in "Redirect First Comment to" option. It's located in the Dashboard > wpDiscuz > Settings > General tab.

More info here: https://wpdiscuz.com/docs/wpdiscuz-7/plugin-settings/general-settings/  

Another solution

wpDiscuz has a wpdiscuz_ajax_callbacks hook. You can use it to get it to work as you like: 

The PHP code: 

add_filter("wpdiscuz_ajax_callbacks", function ($response) {
    $response["callbackFunctions"][] = "myCustomCallback";
    return $response;
});

Your JS code:

window.myCustomCallback = function (response) {
        if (response.success) {
            //do something
        }
    };

 

Posts: 3
Topic starter
(@dc10000)
Active Member
Joined: 1 year ago

Brilliant! Thank you so much, a tiny bit of fiddling and console.logging, but wpdiscuz_ajax_callbacks worked perfectly, and probably opens up answers to a whole slew of upcoming challenges. Thanks so much, really appreciate the quick and thorough answer. BTW I've already bought one paid addon, I'm sure I'll be back for more.

Share: