Saturday, 1 June 2013

PHP script from functions php is loaded via admin-ajax to div...and the result is 0, not the desired content

PHP script from functions php is loaded via admin-ajax to div...and the result is 0, not the desired content

I wish to use accordion menu http://wordpress.org/plugins/jquery-vertical-accordion-menu/ via function that is loading different menu based on page template.
What is more, I wish to ajaxify that element. Below is all scripts from functions.php:
function get_left_menu() {
if (is_page_template('page-akademia-biznesu.php')) {
    echo do_shortcode('[dcwp-jquery-accordion menu="Business menu" auto_close="true" event="click" save="true" disable="false" count="false" expand="false" disable_class="disabled"]');
} elseif (is_page_template('page-forum-wiedzy-oze.php')) {
    echo do_shortcode('[dcwp-jquery-accordion menu="OZE menu" auto_close="true" event="click" save="true" disable="false" count="false" expand="false" disable_class="disabled"]');
} elseif (is_page_template('page-o-nas.php')) {
    echo do_shortcode('[dcwp-jquery-accordion menu="About menu" auto_close="true" event="click" save="false" disable="false" count="false" expand="false" disable_class="disabled"]');
} elseif (is_page_template('page-program-ambasadorski.php')) {
    echo do_shortcode('[dcwp-jquery-accordion menu="Ambassador menu" auto_close="true" event="click" save="true" disable="false" count="false" expand="false" disable_class="disabled"]');
} elseif (is_page_template('page-program-edukacyjny.php')) {
    echo do_shortcode('[dcwp-jquery-accordion menu="Aside menu" auto_close="true" event="click" save="true" disable="false" count="false" expand="false" disable_class="disabled"]');
}
die();
}
add_action("wp_ajax_nopriv_get_left_menu", "get_left_menu");
add_action("wp_ajax_get_left_menu", "get_left_menu");

function reload_menu_via_ajax()
{
 wp_enqueue_script( 'function', get_template_directory_uri().'/library/js/menuReload.js', 'jquery', true);
 wp_localize_script( 'function', 'left_menu_reload', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('template_redirect', 'reload_menu_via_ajax');
And here we have jquery ajax script (menuReload.js):
jQuery(document).ready(function($) {
$(function() {
    $('nav ul li').click(function() {
        $('#left-menu').empty();
        setTimeout(function() {
            $.ajax({
                method : "POST",
                url : left_menu_reload.ajaxurl,
                data : ( {
                    action : 'get_left_menu()'
                }),
                success : function(data) {
                    $("#left-menu").html(data);
                }
            });
        }, 2000);
    });
});
});
I am not quite skilled if it comes to an ajax calls, so please forgive me if this is some obvious thing. But I am really desperate lately with this and please - help.

No comments:

Post a Comment