Frage

i would load via ajax the content of page that have inside the shortcode of one of my form generated via CF7 Plugin. When the content is rendered the shortcode is not processed and come print as text. Is there anyway to force execution of shortcode in an ajax call? Thank you, M.

This is the js script:

function getcontent(postid){
    // here is where the request will happen
    jQuery.ajax({
        url: '/wp-admin/admin-ajax.php',//ajax controller request
        data:{
            'action':'dch',//action invoked
            'fn':'ghcontent',//function required
            'postid':postid//if of post required
        },
        cache: false,
        async:true,
        timeout: 3000,
        success:function(data){
            jQuery("#posthome").html(data);//print the html (content)
        },
        error: function(errorThrown){
            console.log(errorThrown);
        }
    });
}

And this is my php code:

add_action('wp_ajax_nopriv_dch', 'ghcontent');
add_action('wp_ajax_dch', 'ghcontent');
function ghcontent(){
  $args = array('page_id' => $_REQUEST['postid']);
  query_posts($args);
  if(have_posts()) : 
    while (have_posts()) : the_post();
      the_content();
    endwhile;
  endif;
  die();
}
War es hilfreich?

Lösung

do_shortcode won't work in your admin-ajax.php Instead try generating your own ajax action. Check the following answer to a similar question: https://wordpress.stackexchange.com/questions/53309/why-might-a-plugins-do-shortcode-not-work-in-an-ajax-request

Andere Tipps

Shortcodes are applied when the the_content filter runs:

$post = get_post( $_REQUEST['postid'] );
$return = apply_filters( 'the_content', $post->post_content );
echo $return;
die();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top