I have a client that wanted their website to be primarily just the home page. They also want mobile and tablet users to just be able to swipe through the pages. I found some potential solutions but they were mostly very large, overly complex plugins for a simple need. I’ve been wanting to dive into the WP API and I found this to be a great opportunity. This is VERY basic usage of the WP API, but hey, I’m just getting started. And please read to the bottom if you are knowledgeable of the WP API as I’m a bit confused on why something I tried did not work.
So here is my simple solution for the sliding pages (works for posts and other post_types as well).
JSSOR
First we need to include a slider library. I went with JSSOR because it works great with loads of features and is easy to get started quickly.
http://www.jssor.com/development/
I enqueue this in my themes functions.php file
function theme_scripts() {
wp_enqueue_script( 'jssor', get_template_directory_uri() . '/js/jssor.slider.mini.js', array() );
}
add_action( 'wp_enqueue_scripts', 'theme_scripts' );
build_sliders
Then we have our function for building the slider. This can be tweaked to your liking, I wanted to have title, featured image and content. We take in the slider id and height as parameters and we also take in the full url for the WP API request. We format around the loop through our results with the required JSSOR slider containers. The jQuery at the bottom is used to make the slider responsive.
/**
* Build sliders
*/
function build_sliders( $slider_id, $slider_height, $api_request ) {
?>
';
}
?>
No need to publish this comment but you have some pretty serious security holes going on here. First, you’re directly echoing id and other attributes without esc_attr(). Anything executing PHP with that could easily push out some nasty code to the front end.
Second, file_get_contents is BAD. use wp_remote_get() instead and parse from there. Finally, your JSON itself is echoing variables that could be broken and exploited fairly easily. I would move it all to an external file and use wp_localize_script after wp_enqueue_script to provide the dynamic data to the JS without the problems of echoing it inline. Here’s my own slides on the localization topic (https://www.chriswiegman.com/2013/09/localizing-wordpress-slides-wordpress-austin-advanced-meetup/) . Luke Woodward (https://lkwdwrd.com) has some great stuff on more efficient linking of JS and WordPress as well.
Of course I’m gonna publish this, as usually you’ve taught me a ton in just two paragraphs! Hopefully someone else stumbles on this blog and learns from this comment too.