• I’d like to be able to put a button into a menu and/or footer that when people press it – directs them to a random PAGE on the site.

    This would be a way of them coming across pages they’d never think to navigate to, or hadn’t seen in the main menu/s.

    There are plenty of (older) plugins which can direct to a random post, but not a page.

    Ideally I’d also be able to exclude various pages (so people don’t end up on test pages etc)

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Moved to Fixing WordPress, this is not an Everything else WordPress topic.

    Please do not create new topics for the same thing. I’ve removed your new topic.

    Here’s one way to do it. You would create a page titled “Random Page” which gives it the slug random-page. Add the link(s) to it in your menu/footer. Then add this to your functions file or use the Code Snippets plugin.

    add_action('template_redirect', 'wpsf_random_page_redirect');
    function wpsf_random_page_redirect()
    {
    	if (is_page('random-page')) {
    		$excluded_pages = array(15, 9);
    		$pages = get_pages(array('exclude' => $excluded_pages));
    		$random_page = array_rand($pages);
    		$page_url = get_page_link($random_page);
    		wp_redirect($page_url);
    		exit;
    	}
    }

    In my case, the Random Page itself has an ID of 15 so I exclude it. (And another page I want to exclude has the ID of 9.) So, the Random Page is a placeholder that no visitor will ever actually arrive at.

    Thread Starter LandyVlad

    (@landyvlad)

    @rickymccallum87 thank you! An elegant solution
    The functions.php file is, I believe, a theme file.
    If that’s the case I’m running a child theme so it might be easy enough to edit that directly.

    @jdembowski I don’t recall making duplicate topics on this?
    Apologies for wrong area but ‘fixing wordpress’ to me implies something is broken – that’s not the case here. I’m looking to do something specific therefore I thought that came under ‘everything else’.

    I think I created a thread asking how to create an actual button and animate it when clicked. That is related but is a very different question.

    • This reply was modified 3 years, 4 months ago by LandyVlad.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Button to redirect to random page on site?’ is closed to new replies.