• Resolved Swissprice

    (@swissprice)


    Hello,

    Does anyone knows a plugin or a way to disable the access to a certain page on mobile devices.

    The page could be seen by anyone using a computer but if they are trying to see the page from a mobile device, another page or message would appear asking them to see the page using a computer.

    Thank you in advance for your help. ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • Look up this tag from the codex: wp_is_mobile :

    if ( wp_is_mobile() ) {
    	die("Sorry, access is restricted to desktop visits only.");
    }

    Usage:

    Add the above code in your child theme page.php file (in the top, make sure syntax is correct) IF you want to restrict access for all site pages, or setup a custom template for that specific page you wanted to restrict access to, here’s how to achieve that:

    https://developer.www.ads-software.com/themes/template-files-section/page-template-files/page-templates/#page-templates-within-the-template-hierarchy

    Thread Starter Swissprice

    (@swissprice)

    Hello Samuel,

    Thank you for your answer.

    Is there a way to add this code on a single page without having to modify PHP files?

    Otherwise, Is there a way to redirect mobile devices to another page automatically?

    Thank you for your help!

    OK – see if this does it:

    Add this code to your child theme’s functions.php file:

    function redirect_me() {
    	if(is_single() && wp_is_mobile() || is_page() && wp_is_mobile() ) { // eliminates homepage excerpts
    		ob_start();
    		$redirect_to = home_url();
    		?>
    			<script>window.location.replace("<?php echo $redirect_to;?>");</script>
    		<?php
    		return ob_get_clean();
    	}
    }
    add_shortcode('redirect-me', 'redirect_me');

    Add this shortcode [redirect-me] in the post/page which want to be redirecting to somewhere ($redirect_to) if visited on mobile.

    If you want to redirect to somewhere else not the homepage, edit the code, and change home_url() to the desired URL, like:

    $redirect_to = "https://w.org/";

    Let me know how it goes.

    Thread Starter Swissprice

    (@swissprice)

    Hi Samuel,

    Thank you for your answer.

    I tried that but it did not work…

    Do you think there is some kind a plugin that can do that?

    OK – so wp_is_mobile() is not working ?

    I am sorry, I couldn’t help in light of this new info.

    Thread Starter Swissprice

    (@swissprice)

    Hello Samuel,

    Maybe I inserted it wrong in the PHP file…

    Anyway, I found another way to do what I wanted.

    I am very thankful for your help!

    Hi Swissprice. may i know what did you do to Disable access to page from mobile devices. thanks ??

    Hi hanantq,

    This might work for you:

    add_action('wp', function() {
    
    	$pages = array( 1, 2, 3, 4 );
    	$destination = 'https://google.com/#q=Y+U+NO+use+computer';
    
    	if( wp_is_mobile() && ( is_page( $pages ) && ! is_feed() ) ) {
    		wp_redirect( $destination );
    		exit;
    	}
    
    });

    Add it to your child theme’s functions file, and remember to replace 1, 2, 3, 4 with the targeted pages IDs separated by commas, or empty ($pages = array();) to target all pages. And replace the destination.

    Remember it will work on pages only if it worked.

    BTW, you should have opened an independent support topic as per https://codex.www.ads-software.com/Forum_Welcome#Where_To_Post

    Thread Starter Swissprice

    (@swissprice)

    Hello hannatq,

    Unfortunately, I couldn’t find a proper solution for my problem. Instead, I used WPtouch to adapt my website for mobile browsing.

    I haven’t tried the last solution Samuel Elh provided. Maybe it’ll work for you.

    Hello!it worked for me. Thank you Samuel Elh. And also to you Swissprince. ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Disable access to page from mobile devices’ is closed to new replies.