PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 2.6.6
Jetpack – WP Security, Backup, Speed, & Growth v2.6.6
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / random-redirect.php
random-redirect.php
44 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Random Redirect
4 Plugin URI: http://wordpress.org/extend/plugins/random-redirect/
5 Description: Allows you to create a link to yourblog.example.com/?random which will redirect someone to a random post on your blog, in a StumbleUpon-like fashion.
6 Version: 1.2-wpcom
7 Author: Matt Mullenweg
8 Author URI: http://photomatt.net/
9 */
10
11 function matt_random_redirect() {
12 // Acceptables URL formats: /[...]/?random=[post type], /?random, /&random, /&random=1
13 if ( ! isset( $_GET['random'] ) && ! in_array( strtolower( $_SERVER['REQUEST_URI'] ), array( '/&random', '/&random=1' ) ) )
14 return;
15
16 // Ignore requests that include more than just the random parameter.
17 if ( ! empty( $_POST ) || ( isset( $_GET['random'] ) && count( $_GET ) > 1 ) )
18 return;
19
20 // Persistent AppEngine abuse. ORDER BY RAND is expensive.
21 if ( strstr( $_SERVER['HTTP_USER_AGENT'], 'AppEngine-Google' ) )
22 wp_die( 'Please <a href="http://en.support.wordpress.com/contact/">contact support</a>' );
23
24 // Use the post type of the current page as the context for the random lookup.
25 $post_type = get_post_type();
26
27 // /?random should always show a random post, even if the home page is a static page.
28 if ( '/' == $_SERVER['DOCUMENT_URI'] )
29 $post_type = 'post';
30 else
31 $post_type = get_post_type();
32
33 if ( ! $post_type )
34 $post_type = 'post';
35
36 global $wpdb;
37 $random_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s AND post_password = '' AND post_status = 'publish' ORDER BY RAND() LIMIT 1", $post_type ) );
38 $permalink = get_permalink( $random_id );
39 wp_safe_redirect( $permalink );
40 exit;
41 }
42
43 add_action( 'template_redirect', 'matt_random_redirect' );
44