0

I just bought the Visual Composer plugin from wpbakery, my problem is that I'm making ajax calls to get the content of posts that have been edited with Visual Composer when I get the data it only show me the shortcodes of the plugin

As I see from the information that I have found on how to fix this problem is that ajax calls can't show shortcodes because of the admin ajax url that they dont have full access to wordpress enviornment but I want to know if someone has done it please it would help me a lot

basically this is how I print it, the ajax call is working fine it shows me what I saved but the elements doesnt show correctly it only shows shortcodes

this code is in a plugin I made

function prefix_load_proyect () {
$post_id = $_POST[ 'proyect' ];
$the_query = new WP_Query(array('p'=>$post_id, 'post_type' => 'home_portfolio'));

while ( $the_query->have_posts() ) : $the_query->the_post();

$content = apply_filters('the_content', $post->post_content);
$content = do_shortcode(get_post_field('post_content', $postid));
$html=$content;

endwhile; 
wp_reset_postdata();

        echo $html;
        die();
}

then in the js file I just make the ajax call and put the html response with jquery into a container

 jQuery.ajax({
        type: 'POST',
        url: ajaxurl,
        data: {"action": "load-filter-proyect", proyect: proyectID },
        success: function(response) {

            jQuery('#agregarProyectContainer').html(response);

    });
8
  • can you show some code that you have written? I believe it should be fine to use shortcodes in ajax calls. You should have access to all the Wordpress functions. Are you using the do_shortcodes function?
    – JoeMoe1984
    Commented Jan 6, 2016 at 9:13
  • $content = apply_filters('the_content', $content); $content = do_shortcode(get_post_field('post_content', $postid)); $html.=$content; but it still printing me the shortcodes, the ajax is working because it actually brings me what I saved but it isn't showing like it should be Commented Jan 7, 2016 at 0:08
  • Just a tip, its better to update your question to show your code attempts. This will help people who are answering your question see what you have attempted without reading all the comments. Plus you can format the code snippets to make them easier to read. Reading code snippets in the comments is not very fun to read. ;)
    – JoeMoe1984
    Commented Jan 7, 2016 at 0:14
  • thanks so much I'm new in stackoverflow I'll update the question and let you know :) Commented Jan 7, 2016 at 0:22
  • i just updated it :) Commented Jan 7, 2016 at 0:31

2 Answers 2

2

Since Visual Composer Version 4.9 you have to use WPBMap::addAllMappedShortcodes() before the apply_filters('the_content', ...) or do_shortcode(). You don't need both by the way, because apply_filters('the_content', ...) executes do_shortcode() anyway.

Your function would then be:

function prefix_load_proyect () {

  $post_id = $_POST[ 'proyect' ];
  $the_query = new WP_Query(array('p'=>$post_id, 'post_type' => 'home_portfolio'));


  /* --- Necessary since Visual Compoer V 4.9 --- */
  WPBMap::addAllMappedShortcodes();


  while ( $the_query->have_posts() ) : $the_query->the_post();

    $content .= apply_filters('the_content', $post->post_content);
  endwhile;

  echo $content;
  wp_die(); // wp_die() instead of die() provides better integration with WordPress
}
1

...ajax calls can't show shortcodes because of the admin ajax url that they dont have full access to wordpress enviorment

Not true. AJAX calls have exactly the same resources at their disposal as regular WordPress requests. Just make sure to parse the content in the same way the_content() does:

$parsed_content = apply_filters( 'the_content', $post->post_content );

Update: Your code is a mess, try this instead:

function prefix_load_proyect() {
    if ( ! isset( $_POST['proyect'] ) || ! $post_id = absint( $_POST['proyect'] ) )
        return;

    if ( ! $post = get_post( $post_id ) )
        return;

    echo apply_filters( 'the_content', $post->post_content );
    exit;
}
3
  • I type it like that it didn't worked :/ Commented Jan 7, 2016 at 0:46
  • There's a lot of bugs in the code you provided, see my suggested revision. Commented Jan 7, 2016 at 10:44
  • I did it like that still didn't worked :/ it keeps showing me the shortcodes Commented Jan 9, 2016 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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