WordPress.org

Ready to get started?Download WordPress

Forums

[closed] adding a ajax hook not working in shortcode? (3 posts)

  1. yu0307
    Member
    Posted 1 year ago #

    here is how it set up:
    add_shortcode('abc_abc','shortfun');

    function shortfun()
    {
    include 'ajax_res.php';
    }

    ----------------------------in ajax_res.php:--------------------------------------------
    add_action('wp_ajax_ex_abc','ex_abc');
    function ex_abc()
    {
    echo 'here!';
    die();
    }

    -------------------------------js call-------------------------------
    jQuery.ajax({
    type : "post",
    url : myAjax.ajaxurl,
    data : {action: "ex_abc"},
    success: function(response) {
    alert(response);
    }
    }

    why is the output always 0. and it looks like the function hooked onto the ajax call "ex_abc" is never called.
    Blog url:

  2. yu0307
    Member
    Posted 1 year ago #

    anyone please help~~~~~~~~~

  3. yu0307
    Member
    Posted 1 year ago #

    UPDATE:

    it works if only if add_action and function was included outside the shortcode function.

    meaning it needs to register when plugin loads, it we were trying to load this during shortcode rendering. this will not work.

    anyone explain why????

    [closed - focus on one topic, and continue with http://wordpress.org/support/topic/adding-a-ajax-hook-not-working-in-shortcode-1?replies=4 ]

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.