-
AuthorPosts
-
May 22, 2020 at 2:47 pm #1215247
Hi Guys
I am looking for help!!
Because I am using Ninja Form – I cannot place 2 version of a form on one WordPress web page as they won’t work – it only accepts 1 form per page.
SO i am not able to do my mobile only sections on the same WordPress page – therefore I need to do alternative pages – for only a few pages on the website – for the mobile viewing.
I have used the code below before to redirect the user on detecting they are using mobile devices to an alternative home page. I am wondering if I can use the same code to sent the user to other alternative pages on the site in the same way – so if I wanted them to go to a different about page when they are using mobile – how would I do this?
Here is the code i have used to redirect to an alternative home page:if(isset($avia_config[‘use_child_theme_functions_only’])) return;
add_theme_support(‘avia_template_builder_custom_css’);function so16165211_mobile_home_redirect(){
if( wp_is_mobile() && is_front_page() ){
wp_redirect( ‘/home-mobile’ );
exit;
}
}
add_action( ‘template_redirect’, ‘so16165211_mobile_home_redirect’ );Any help would be much appreciated. Bear in mind I do not have a full mobile site available – so I only need a few pages done this way for now.
Kevin.
May 27, 2020 at 3:08 pm #1216975Hey Kevin,
Thank you for the inquiry.
You can try this snippet to check for the current page ID and redirect accordingly.
function so16165211_mobile_home_redirect(){ global $post; $redirect = false; if ( is_front_page() ) { $redirect = '/home-mobile'; } switch ($post->ID) { case 13: $redirect = '/about-mobile'; break; case 4: $redirect = '/contact-mobile'; break; case 31: $redirect = '/shop-mobile'; break; default: } if ( wp_is_mobile() && $redirect ) { wp_redirect( $redirect ); exit; } } add_action( 'template_redirect', 'so16165211_mobile_home_redirect' );
Don’t forget to replace the value or use the actual ID of the pages in the switch statement.
Best regards,
IsmaelMay 27, 2020 at 3:45 pm #1216997Hi Ismael
Is there any way you can explain this piece of code to me as I am not very good with the syntax:
What is this referring to: so16165211_mobile_home_redirect()
I understand that this code:
if ( is_front_page() ) {
$redirect = ‘/home-mobile’;
}
Checks if the page is the front page [home] then it redirects the user to the mobile version /home-mobile…
In this next statement:
switch ($post->ID) {
case 13:
$redirect = ‘/about-mobile’;
break;
What does case 13 refer to?
Is it the page ID?
If so – do I find the page ID’s and start from there?May 29, 2020 at 10:19 am #1217641Hi,
Thank you for the update.
The so16165211_mobile_home_redirect is the name of the function — you can change it to whatever you want. And yes, the integer beside the case refers to the ID of the page which is then compared to the value of the $post->ID. If the value of the case match with the value of the expression ($post->ID), we assign the appropriate URL string to the $redirect variable.
And yes, you have to replace the case value with the actual ID of the pages in your installation, so 13 should be replaced with the ID of the about page because if that case matched, we assigned the ‘/about-mobile’ to the $redirect variable. You can of course add more cases in the switch if you want.
Best regards,
Ismael -
AuthorPosts
- You must be logged in to reply to this topic.