0

I'm currently working on a Wordpress site. I'm building a custom plugin that allows for lazy loading of custom post types.

Some of these post types contain shortcodes, that I need to display. I have a primary Javascript file that makes Ajax requests to my PHP code in my plugin, which returns a JSON object containing the shortcode like this:

Object {shortcode: "[embedyt]https://www.youtube.com/watch?&height=300[/embedyt]"}

I'm then trying to display this shortcode on the front-end, since I'm dynamically building HTML blocks based on the JSON response.

However, when I try to output the shortcode to the page via the Javascript, it just displays as text, like this:

enter image description here

Here's my PHP code and Javascript:

$packet = array();
$formatted_url = "[embedyt]" . $youtube_root . $video_id . "&height=300" . "[/embedyt]";
$packet["shortcode"] = do_shortcode($formatted_url);
echo json_encode($packet); // This sends a response back to the Ajax request

And:

$.ajax({
        type: 'POST',
        url: tlas_lazy_load.ajax_url,
        data: {
            action: 'tlas_lazy_load',
            post_type: post_type,
            posts_per_page: 6,
            paged: page
        },
        success: function(packet) {
            response = JSON.parse(packet);
            for(var i = 0; i < response.length; i++) {
                build_lazy_load("#lazy-load-row", packet[i], i);
            }
        }
    });

function build_lazy_load(element, packet, index) {
    var block = "";
    block += "<div class=\"col-md-4\">";
    block += "  <div class=\"video-individual\">";
    block += "      <div class=\"panel panel-default\">";
    block += "          <div class=\"panel-body\" id=" + index + ">";
    block +=                packet.shortcode;
    block += "          <\/div>";
    block += "      <\/div>";
    block += "  <\/div>";
    block += "<\/div>";
    $(element).append(block);
};

What gives me hope that this can be done is the answer to this question:

Wordpress: Outputting shortcode with Javascript?

And I'm following along with what that person suggested. However can this be done?

Update:

So I can display the shortcode if I do this in enqueue scripts method:

add_action( 'wp_enqueue_scripts', 'tlas_lazy_load_enqueue_scripts' );
function tlas_lazy_load_enqueue_scripts() {
    wp_enqueue_script( 'tlas-lazy-load', plugins_url( 'js/tlas_lazy_load.js', __FILE__ ), array('jquery'), '1.0', true );
    wp_localize_script( 'tlas-lazy-load', 'tlas_lazy_load', array(
        'ajax_url' => admin_url( 'admin-ajax.php' )
        // BELOW
        'videos_shortcode' => do_shortcode("[embedyt]" . $youtube_root . $video_id . "&height=300" . "[/embedyt]")
        ));
}

By setting it as a localized variable which my Javascript can access. The problem is that I have to add the shortcode after this function has already been declared. I tried localizing it later within the file in my above function by doing:

...
$packet = array();
$formatted_url = "[embedyt]" . $youtube_root . $video_id . "&height=300" . "[/embedyt]";
wp_localize_script( 'tlas-lazy-load', 'tlas_lazy_load', array(
    'videos_shortcode' => do_shortcode($formatted_url)
));
...

But this didn't work, it simply returned nothing. I'm on the right track (I think?) but I'm not sure how you can dynamically localize a script/Javascript variable.

4
  • Your json should not contain any shortcodes as they should have been processed when you do: $packet["shortcode"] = do_shortcode($formatted_url); Commented Jul 21, 2015 at 15:51
  • You do know that you can use single quotes too? Instead of escaping every quote? But since shortcodes can do a lot of stuff, I see that your only real option is "rendering" it server side. Make a page that takes shortcode as input and return the value. Commented Jul 21, 2015 at 15:52
  • @Christina, I know. I just prefer to escape so as to keep the quotations uniform. Commented Jul 21, 2015 at 16:56
  • @jeroen What do you mean they "should have been processed"? Commented Jul 21, 2015 at 16:56

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.