jetpack_matt_random_redirect

Plugin Name: Random Redirect Plugin URI: https://wordpress.org/extend/plugins/random-redirect/ 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.

Description

jetpack_matt_random_redirect(); 

Version: 1.2-wpcom Author: Matt Mullenweg Author URI: http://photomatt.net/


Usage

  1. if ( !function_exists( 'jetpack_matt_random_redirect' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/modules/theme-tools/random-redirect.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = jetpack_matt_random_redirect(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/modules/theme-tools/random-redirect.php  
  1. function jetpack_matt_random_redirect() { 
  2. // Verify that the Random Redirect plugin this code is from is not active 
  3. // See http://plugins.trac.wordpress.org/ticket/1898 
  4. if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { 
  5. require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
  6. if ( is_plugin_active( 'random-redirect/random-redirect.php' ) ) return; 
  7.  
  8. // Set default post type. 
  9. $post_type = get_post_type(); 
  10.  
  11. // Set default category type 
  12. if ( is_category() ) { 
  13. $category = get_the_category(); 
  14. if ( isset( $category ) && ! empty( $category ) ) { 
  15. $random_cat_id = $category[0]->term_id; 
  16.  
  17. // Set author name if we're on an author archive. 
  18. if ( is_author() ) { 
  19. $random_author_name = get_the_author_meta( 'user_login' ); 
  20. $random_author_query = 'AND user_login = "' . $random_author_name . '"'; 
  21.  
  22. // Acceptable URL formats: /[...]/?random=[post type], /?random, /&random, /&random=1 
  23. if ( ! isset( $_GET['random'] ) && ! in_array( strtolower( $_SERVER['REQUEST_URI'] ), array( '/&random', '/&random=1' ) ) ) 
  24. return; 
  25.  
  26. // Ignore POST requests. 
  27. if ( ! empty( $_POST ) ) 
  28. return; 
  29.  
  30. // Persistent AppEngine abuse. ORDER BY RAND is expensive. 
  31. if ( strstr( $_SERVER['HTTP_USER_AGENT'], 'AppEngine-Google' ) ) 
  32. wp_die( 'Please <a href="http://en.support.wordpress.com/contact/">contact support</a>' ); 
  33.  
  34. // Set the category ID if the parameter is set. 
  35. if ( isset( $_GET['random_cat_id'] ) ) 
  36. $random_cat_id = (int) $_GET['random_cat_id']; 
  37.  
  38. // Change the post type if the parameter is set. 
  39. if ( isset( $_GET['random_post_type'] ) && post_type_exists( $_GET['random_post_type'] ) ) 
  40. $post_type = $_GET['random_post_type']; 
  41.  
  42. global $wpdb; 
  43.  
  44. if ( isset( $random_cat_id ) ) { 
  45. $random_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts AS p INNER JOIN $wpdb->term_relationships AS tr ON (p.ID = tr.object_id AND tr.term_taxonomy_id = %s) INNER JOIN $wpdb->term_taxonomy AS tt ON(tr.term_taxonomy_id = tt.term_taxonomy_id AND taxonomy = 'category') WHERE p.post_type = %s AND post_password = '' AND post_status = 'publish' %s ORDER BY RAND() LIMIT 1", $random_cat_id, $post_type, $random_author_query ) ); 
  46. } else { 
  47. $random_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s AND post_password = '' AND post_status = 'publish' %s ORDER BY RAND() LIMIT 1", $post_type, $random_author_query ) ); 
  48.  
  49. $permalink = get_permalink( $random_id ); 
  50. wp_safe_redirect( $permalink ); 
  51. exit;