0

I'm new to the ajax world, but I've successfully followed the instructions here:

The problem I'm having is that I'm trying to conditionally run the ajax on a specific page. This is to keep the code from running unnecessarily on pages where it's not applicable. I have tried multiple methods including using the wordpress is_page() function. I have tried adding a filter to the_content() and then adding the action to run the ajax (though this didn't feel like good coding). None of this worked.

I should probably add that the code I'm running for the ajax call queries a mysql database, returns the data, and adds it to the options in a dropdown menu (this is what I DONT want running every time. It works great when run unconditionally. When trying add the page conditional, it doesn't work. Below is the code:

add_action( 'wp_enqueue_scripts', 'ajax_test_enqueue_scripts' );

function ajax_test_enqueue_scripts() {

    wp_enqueue_script( 'orgs', plugins_url( '/love.js', __FILE__ ), array('jquery'), '1.0', true );
    wp_enqueue_script( 'love', plugins_url( '/chosen.jquery.min.js', __FILE__ ), array('jquery'));

    wp_localize_script( 'love', 'postlove', 
        array(
        'ajax_url' => admin_url( 'admin-ajax.php' ),
        )
    );

}


function post_love_add_love() {
    global $wpdb; 

    $query = "SELECT id,name FROM wp_qp_orgs";
    $results = $wpdb->get_results($query,ARRAY_A);

    $optionsString = '<option></option>';
    foreach($results as $k => $v){
        $optionsString .= '<option value="' . $v['id'] . '">' . $v['name'] . '</option>';
    }

    //https://stackoverflow.com/questions/14348470/is-ajax-in-wordpress
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 
        echo $optionsString;
        die();
    }
}

function qp_tester(){
    if(is_page()){
        echo do_shortcode('[formidable id=36]');
    }
}

add_filter('the_content','qp_tester');

add_action( 'wp_ajax_nopriv_post_love_add_love', 'post_love_add_love' );
add_action( 'wp_ajax_post_love_add_love', 'post_love_add_love' );

and here is the jquery:

 jQuery( document ).ready(function() {

        jQuery.ajax({
            url : postlove.ajax_url,
            type : 'post',
            data : {
                action : 'post_love_add_love',
            },
            beforeSend: function() {
                jQuery('#qpForm').delay(700).show(100);
            },
            success : function( response ) {
                //to see the immediate response from ajax - uncomment below
                alert(response);
                jQuery('#field_iv2eu').html(response);
                jQuery('#field_iv2eu').chosen({
                    disable_search_threshold: 5,
                    no_results_text: "No results using",
                    placeholder_text_multiple: "Choose your organization",
                    search_contains: true,
                    max_shown_results: 8

                });
            }
        });

 });

Any suggestion would be really appreciated!

Community
  • 1
  • 1
user791187
  • 657
  • 3
  • 11
  • 23

1 Answers1

0

Try with this:

add_action('template_redirect', 'qp_tester');

function qp_tester() {
    if(is_page()){
        echo do_shortcode('[formidable id=36]');
    }
}
Vel
  • 9,027
  • 6
  • 34
  • 66
  • the filter I included to put in the form isn't what I'm trying to accomplish though the form is where I want the options to populate from the ajax call. The problem is the post_love_add_love function. The ajax actions work great if their NOT in a conditional (such as is_page) - but if I try adding the function or an action to the conditional, the ajax doesnt work – user791187 Dec 02 '16 at 10:09
  • is it possible to pass the page id from ajax call? – Vel Dec 02 '16 at 10:52