0

I have posted this question: How to use shortcodes on a widget sidebar when doing an ajax call?, with no answers. Now I want to ask a more specific question.

The problem seems to be that the shortcode is not processed by the plugin., as if I replace the shortcode with e.g. [audio src="audioFile.ogg"] it works perfect.

So I'm guessing that I could make it work if the plugin is loaded before. But I don't know how to achieve it (maybe I'm wrong with it).

Any suggestion?

This is the code in functions.php:

add_action( 'init', function() { 
  ps_register_shortcode_ajax( 'ps_get_slider', '8' ); 
} );

function ps_register_shortcode_ajax( $callable, $action ) {
    if(isset($_POST['href'])) {
        $pageId = preg_match( '/^http:\/\/.+\/\?page_id=\d+$/', $_POST['href'] ) ? substr( strrchr( $_POST['href'], '=' ), 1 ) : '8' ;

        if ( empty( $pageId ) || $pageId != $action )
            return;

        call_user_func( $callable );
    }
}

function ps_get_slider() {
    //require_once('../wp-load.php'); Nothing changes

    apply_filters('ps_register_shortcode_ajax', 'slider_widget_init');
    if ( dynamic_sidebar('Slider') ) : else : endif;
    //echo do_shortcode( '[promoslider post_type="news" height="300px" width="70%" numberposts="5" start_on="first" display_title="fancy" display_excerpt="excerpt"  pause_on_hover="pause"]' );
    get_template_part( 'home', 'ajax' );
    die(); 
} 

The Slider side-bar is loaded just in home and-home-ajax pages (id=8). It works right in home page, but not in home-ajax page.

6
  • Post your code please.
    – s_ha_dum
    Commented Jan 9, 2014 at 14:29
  • @s_ha_dum - Done
    – Manolo
    Commented Jan 9, 2014 at 14:43
  • There is too much going on there for me to be be able to replicate the problem in any reasonable time. Please reduce that to a bare minimum sample case that demonstrates the problem. Sorry, but I need to paste that into a dev-stack and run the code. I don't have time to reverse engineer it. Also, this is the same question as your other one even if worded slightly differently. I'd delete one or the other.
    – s_ha_dum
    Commented Jan 9, 2014 at 14:53
  • @s_ha_dum - All right. Now it is the most relevant code.
    – Manolo
    Commented Jan 9, 2014 at 15:28
  • @s_ha_dum - I'd prefer to delete one question when the problem is resolved.
    – Manolo
    Commented Jan 9, 2014 at 15:29

1 Answer 1

0

You are not using ajax right. Why are you adding your ajax callback function to the init action? You need to read http://codex.wordpress.org/AJAX_in_Plugins

Attach your ajax callback function to wp_ajax_your_defined_action like this:

add_action( 'wp_ajax_your_defined_action', 'your_callbck_function' );

For this to work you have to use wp-admin/admin-ajax.php as your ajax url and you have to post your_defined_action to action.

            $.ajax({
                type: 'POST',
                url: ajaxurl, //This is defined only on admin side
                data: {
                    action: 'your_defined_action',
                    some_var: 'Some Var Content'
                },
                success: function (response) {
                    alert(response);
                }
            });
1

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