Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

Those is_page_template call will all be false. The AJAX request is an independent request to the server from the client browser. As far as that AJAX request is concernend the page you are on is admin-ajax.php. You will need to pass a parameter with the AJAX request that you can use to determine which shortcode to run. For example:

data : ( {            
    action : 'get_left_menu()',
    runsc  : 'Business menu'
}),

Also, your AJAX action is get_left_menu(). It should be get_left_menu to match the wp_ajax_* actions.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.