Skip to:
Content
Pages
Categories
Search
Top
Bottom

Ajaxify create new topic form


  • tpsr51
    Participant

    @tpsr51

    I am trying to replace all the topics in a specific forum with the new topic form whenever I press on the Create New Topic button.
    Then I tried to ajaxify it using the shortcode [bbp-topic-form], but the form didn’t showed up…

    In the functions.php I have added this:

    
    wp_enqueue_script( 'script', get_stylesheet_directory_uri() . '/js/topicButton.js', array ( 'jquery' ), 1.1, true);
    wp_localize_script( 'script', 'script', array(
    	'ajaxurl' => admin_url( 'admin-ajax.php' )
    ));
    ..
    ..
    add_action('wp_ajax_script', 'scriptfunc');
    add_action('wp_ajax_nopriv_script', 'scriptfunc');
    function scriptfunc() {
    	echo do_shortcode("[bbp-topic-form]");
    	die();
    } 

    The ajax file

    (function($) {
    	
    		function find_forum_id() {
    			var forumidval = document.getElementsByTagName("article")[0].id;
    			forumidval = forumidval.replace(/\D/g,'');
    			return parseInt( forumidval );
    		}
    	
    		$(document).on( 'click', '.bbp-new-topic-button', function( event ) {
    			event.preventDefault();
    			
    			forumid = find_forum_id();
    			
    			$.ajax({
    				url: ajaxurl,
    				type: 'post',
    				data: {
    					action: 'scriptfunc',
    					forumid: forumid
    				},
    				beforeSend: function() {
    					$('.bbp-topics').remove();
    				},
    				success: function( html ) {
    					$('article').append( html );
    				}
    			})
    		
    		});
     })(jQuery);
  • You must be logged in to reply to this topic.
Skip to toolbar