Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply To: replies not working sometimes

@clivesmith

Participant

OK There seems to be no fixed reason that the replies are rejected, one minute it works and next it does not. I have 2 pages 1 Thank you page and 1 error page. I am running through cloudflare so I am not sure if that is causing the problem.
This code I have added to the function.php seems to be working

// 1 Check if the reply submission has missing fields & redirect to the error page
add_action(‘template_redirect’, ‘rew_check_reply_submission’);

function rew_check_reply_submission() {
// Check if a reply is being submitted
if (isset($_POST[‘bbp_reply_submit’])) {
// Check if required fields are missing (e.g., content, forum, topic)
if (empty($_POST[‘bbp_reply_content’]) || !isset($_POST[‘bbp_forum_id’]) || !isset($_POST[‘bbp_topic_id’])) {
// Grab the topic ID from the form (ensure it’s valid)
$topic_id = !empty($_POST[‘bbp_topic_id’]) ? intval($_POST[‘bbp_topic_id’]) : 0;

// Redirect to the error page with the topic ID
wp_redirect(site_url(‘/reply_error/?topic_id=’ . $topic_id));
exit; // Stop the script execution after the redirect
}
}
}

// 2️ Modify reply redirection: If pending, go to moderation page
add_filter(‘bbp_new_reply_redirect_to’, ‘rew_pending_check’, 30, 3);

function rew_pending_check($reply_url, $redirect_to, $reply_id) {
$status = get_post_status($reply_id);
$topic_id = bbp_get_reply_topic_id($reply_id);

// If the reply is pending moderation, redirect to the moderation page
if ($status === ‘pending’) {
return site_url(‘/moderation/?moderation_pending=’ . $topic_id);
}

return $reply_url; // Otherwise, return the usual redirect URL
}

// 3 Create a shortcode [mod-return] to display “Return to Topic” on error and moderation pages
add_shortcode(‘mod-return’, ‘mod_return’);

function mod_return() {
// Get the topic ID from the URL (works for both error and moderation pages)
$topic_id = !empty($_GET[‘topic_id’]) ? intval($_GET[‘topic_id’]) : (!empty($_GET[‘moderation_pending’]) ? intval($_GET[‘moderation_pending’]) : 0);

// If a valid topic ID is found, generate the “Return to Topic” button
if ($topic_id) {
$topic_url = get_permalink($topic_id);

if ($topic_url) {
return ‘<div class=”mod-return” style=”text-align:center; margin-top:20px;”>
<a href=”‘ . esc_url($topic_url) . ‘”
style=”color:white; background-color:tomato; padding:12px 20px;
border-radius:5px; text-decoration:none; font-size:16px;”>
⬅ Return to Topic

</div>’;
}
}

// If no valid topic ID found, return nothing or a message
return ‘<p style=”text-align:center; color:red;”>No topic found to return to.</p>’;
}

Skip to toolbar