1

I’m very new to WordPress Plugin Development. I’ve been learning and developing my plugin for around 2 weeks now.

My plugin is supposed to be a retrieve a question from the WordPress database and display a custom question and button via a ShortCode. This works. When the button is clicked, the code is supposed to use jQuery to send values to another php function.

I was able to test that the jQuery is receiving the information from the page as I used an alert popup to display the values from the form. This is the code I currently have.

Code to Enqueue Script

function ccc_add_script()
{       
    //Enqueue JavaScript File   
    wp_enqueue_script('ccc-script-js', plugins_url('/js/ccc_script.js', __FILE__ ), array( 'jquery' ),'1.0.0', true );          
        
    //Pass ajax_url to ccc_script.js    
    wp_localize_script('ccc-script-js', 'ccc_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' )));
}

add_action('wp_enqueue_scripts','ccc_add_script');

Code for jQuery - the submit_ans is for when the button is clicked.

jQuery(document).ready(function() {     
    
    jQuery("#submit_ans").click(function() {
        
        //AJAX URL      
        var ajax_url = ccc_object.ajax_url; 
        
        var data = {
                'action': 'ccc_update_personal_answer',
                'answerid': document.getElementById('cccanswer').value,
                'userid': document.getElementById('cccuserid').value,
                'question': document.getElementById('cccquestion').innerHTML
            };
        
        jQuery.post(ajax_url, data, function(response) {
            jQuery("#cccresponse").html(response);
        }); 

        
    });     
        
});

Code for the WordPress function after button is clicked and jQuery function fires. All the code is contained in the same plugin page with the ShortCode function as well.

function ccc_update_personal_answer()
{
    if (is_user_logged_in()) {      
        global $wpdb;
        
        $answer = $_POST['answerid'];
        $uid = $_POST['userid'];
        $question = $_POST['question'];
        $errorMsg = "Error: ";
        
        
        echo 'Ajax call output:';   
                
        wp_die();  
    }
    else {
        echo 'Please login to your account to view this page.'; 
        wp_die();  
    }
}

add_action('wp_ajax_ccc_update_personal_answer', 'ccc_update_personal_answer');
add_action('wp_ajax_nopriv_ccc_update_personal_answer', 'ccc_update_personal_answer');
8
  • 1
    have you tried using the modern REST API endpoints instead of the old admin-ajax.php endpoint? It's easier to debug and will give you a human readable error message on errors, whereas admin-ajax.php just says 0. What does the browsers dev tools error console say?
    – Tom J Nowell
    Aug 3 at 15:22
  • I haven't tried the REST API - will look into it. The dev tools aren't showing any issues with my form or anything else really.
    – ari2010
    Aug 3 at 15:48
  • What are you getting for response ? Aug 3 at 16:15
  • I removed the code that I had to output the response. I assumed that the [echo 'Ajax call output:';] would be displayed on the screen after I clicked the button. I will add it back and see if "response" gets placed in the <div></div> that has the id "#cccresponse"
    – ari2010
    Aug 3 at 16:30
  • 1
    ah so you solved your problem and found a new one. Also if you refresh your page then you've called the shortcode again every time it reloads. Remember, nothing persists between requests unless it's stored in the database, there is no background WordPress task like there is with Node etc, WP gets loaded in full from a blank slate when you load the page, and again on every AJAX call
    – Tom J Nowell
    Aug 3 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.