Skip to content

Instantly share code, notes, and snippets.

@prantu
Last active July 11, 2018 20:32
Show Gist options
  • Save prantu/791bbbf0174c556f6c98ce53223f3d36 to your computer and use it in GitHub Desktop.
Save prantu/791bbbf0174c556f6c98ce53223f3d36 to your computer and use it in GitHub Desktop.
Add different versions of Sliders for Mobile and Desktops with shortcode [WordPress]
<?php
if( !function_exists('rev_slider_detection') ) {
function rev_slider_detection( $atts ) {
extract(shortcode_atts(array(
'alias' => '' // the alias name of your desktop rev-slider
), $atts));
$rev_output = '[rev_slider alias="';
if (isset($_GET['width'])) {
$width = $_GET['width'];
echo "<script language='javascript'>if(window.location.href.indexOf('preview=true') < 1) {window.history.replaceState(null, null, window.location.pathname);}</script>";
// Answer by evanjmg https://stackoverflow.com/questions/22753052/remove-url-parameters-without-refreshing-page
if ($width <= 480) {
$rev_output .= $alias . '-mobile"]';
}
else {
$rev_output .= $alias . '"]';
}
return do_shortcode( $rev_output );
}
else {
// [PHP version of CSS Media Queries] Answer by Suvi Vignarajah https://stackoverflow.com/questions/5570580/php-version-of-css-media-queries
echo "<script language='javascript'>\n";
echo "location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}" . "&width=\" + screen.width; \n";
echo "</script>\n";
exit();
}
}
add_shortcode('my_rev', 'rev_slider_detection');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment