I’m trying to load a slideshow if the browser is a certain screen size – i.e. I don’t want the slideshow to load on smaller, mobile screens. The slideshow plugin provides a shortcode:
<?php echo do_shortcode('[metaslider id="8302"]'); ?>
But when I try to load it with .load(), it just spits out the string and doesn’t run the php.
So, looking around it seems that there’s a couple of posts related to this:
call shortcode in javascript
Why might a plugin’s ‘do_shortcode’ not work in an AJAX request?
But unfortunately I don’t have a high enough reputation to contribute comments to those, so I’m opening a new question.
I wonder if anyone can help explain this to someone in a very simplified way to someone who is not good familiar PHP?
You cannot load a file directly like this:
Also note that WordPress loads jQuery in noConflict mode, so the
$
alias does not work.If you load a file directly none of the WordPress functions will work. You should be using the AJAX API so that everything loads in WordPress context.
You would wrap your processing PHP is a function:
Hook that into the AJAX system:
Submit your request to
http://site/wp-admin/admin-ajax.php
and passmy_ajax_shortcode
as an argument when the Javascript makes a request.You can set
ajax_url
similarly to this from the Codex: