2

I am challenging Ajax implementation when replying by "BBPress".

However, reply posted in this manner is displayed in the dashboard, but it is not displayed in the loop of the replies list.

This will be displayed in the loop of the replies list only when you click "Update" on the dashboard.

I want to know the code that displays correctly in the loop.

This is my code:

my-reply.js

"use strict";
(function($){
$(document).on("click","#bbp_reply_submit", function() {    
    // get
    var inputForum = '56'; //There is only one forum.
    var inputTopic = $('#bbp_topic_id').val();
    var inputTitle = $('#bbp-reply-form-info').text();
    var inputContent = $('#bbp_reply_input').val();
    var inputParent = $('#bbp_reply_to').val();
    var inputIp = $('#my_reply_ip').val();
    // Ajax     
    $.ajax({
        url: MY_AJAX.api,
        type: 'POST',
        data: {
            // action name
            action: MY_AJAX.action,
            // nonce
            nonce: MY_AJAX.nonce,
            // submit data
            submitForum: inputForum,
            submitTopic: inputTopic,
            submitTitle: inputTitle,
            submitContent: inputContent,
            submitParent: inputParent,
            submitIp: inputIp,
        }
    })
    // scusess
    .done(function( response ) {        
         alert('success'); // Actually, I will display reply instead of alerts.
    })
    // error
    .fail(function( jqXHR, textStatus, errorThrown ) {
        alert('error');     
    }); 
});
})(jQuery);

functions.php

// nonce
function my_enqueue_scripts() {
    $handle = 'my-script';
    $jsFile = 'path/to/myscript.js';
    wp_register_script($handle, $jsFile, ['jquery']);
    $acrion = 'my-ajax-action';
    wp_localize_script($handle, 'MY_AJAX', [
        'api'    => admin_url( 'admin-ajax.php' ),
        'action' => $acrion,
        'nonce'  => wp_create_nonce( $acrion ),
    ]);
    wp_enqueue_script($handle);
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );

// post reply
function my_ajax_event() {
    $action = 'my-ajax-action';
    if( check_ajax_referer($action, 'nonce', false) ) {
        $submitForum = esc_html( $_POST['submitForum'] );
        $submitTopic = esc_html( $_POST['submitTopic'] );
        $submitTitle = esc_html( $_POST['submitTitle'] );
        $submitContent = esc_html( $_POST['submitContent'] );   
        $submitParent = esc_html( $_POST['submitParent'] ); 
        $submitIp = esc_html( $_POST['submitIp'] ); 
        $submitTopicslug = esc_html( $_POST['submitTopicslug'] );   
        $page = get_post( $submitTopic );
        $slug = $page->post_name;
        $date = 'Y-m-d H:i:s';

        $my_post = array(
            'post_title' => (string)wp_strip_all_tags($_POST['submitTitle']),
            'post_content' => (string)$_POST['submitContent'],
            'post_status' => 'publish',
            'post_author' => get_current_user_id(),
            'post_type' => 'reply',
            'meta_input' => array(
                '_bbp_forum_id' => $_POST['submitForum'],
                '_bbp_topic_id' => $_POST['submitTopic'], 
                '_bbp_reply_to' => $_POST['submitParent'],
                '_bbp_author_ip' => $_POST['submitIp'],
                '_edit_last' => get_current_user_id(),
                '_bbp_topic_sort_desc' => 'dft',
                '_bbp_sort_desc' => 'dft',
                '_bbp_topic_sort_show_lead_topic' => 'dft',
                '_bbp_topic_sort_show_lead_topic_forum' => 'dft',
                '_wp_old_slug' => $slug,
                '_bbp_last_active_time' => $date,
                '_bbp_reply_count' => '',
                '_bbp_engagement' => '',
                '_bbp_reply_count_hidden' => '',
                '_bbp_voice_count' => '',
            )           
        );
        wp_insert_post( $my_post );

    } else {
        status_header( '403' );
    }

    die();
}
add_action( 'wp_ajax_my-ajax-action', 'my_ajax_event' );
add_action( 'wp_ajax_nopriv_my-ajax-action', 'my_ajax_event' );
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71

0 Answers0