Archived topic
Add element with shortcode and diplay it just in mobile.
4 replies · Started by Rostyslav on January 1, 2023
Hello,
I am using slider evolution plugin that provides me a shortcode to insert. I insert it as element>hook>execute shortcode (display on homepage).
I would like to display it just on mobile (with php function). (I don`t want to use css: hide-on-mobile because there is still a lot of requests from that plugin that make the website slow on mobile). I just want to make that plugin work just on desktop view.
Thanks.
As hero on desktop I am using a shortode from sliderrevolution inside a generateblocks container(block element) with css: hide-on-mobile.
And as hero image on mobile, I am using a generateblocks styles with css: hide-on-desktop.
On this image there is a view of mobile and all the html and css from slider-revolution is showing.
https://ibb.co/0ZHvYXf
I would like to remove all that code on mobile maybe with element hooks with display rules.
Hi Rostyslav,
In your Hook Element, add this at the very top of the code input box:
<?php if( wp_is_mobile() ) { ?>
Then, add this at the very bottom:
<?php } ?>
Your overall code should look something like this:
<?php if( wp_is_mobile() ) { ?>
[YOUR_SHORTCODE_HERE]
<?php } ?>
Make sure enable PHP is toggled on in the Hook Element settings.
Hello!
Doesn't work at all.
Thanks
Hi there,
wp_is_mobile() is a core function, that will only fire when the page is first visited from a client that matches that function.
This means using responsive views in a browser will not apply to it.
It also means its prone to caching issues ie. if your site/server has any kind of page caching then it must have a separate cache for mobile and for desktop. Otherwise the server will deliver whatever code was cached after the first visit.