Created
March 11, 2011 14:35
-
-
Save RalfAlbert/865951 to your computer and use it in GitHub Desktop.
WordPress - cipher user_nicename when using get_author_posts_url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Cipher Author in get_author_posts_url | |
Plugin URI: http://yoda.neun12.de/artikel-29/2 | |
Description: Cipher the author nicename when using get_author_posts_url() | |
Version: 1.0 | |
Author: Ralf Albert | |
Author URI: http://neun12.de | |
*/ | |
/** | |
* Three easy steps to use this code in your functions.php (instead as a plugin): | |
* | |
* 1. Remove the comment block above | |
* 2. Include the code with include_once/require_once 'filename.php' (filename.php is the filename under which you have saved this script) | |
* ALTERNATIVELY | |
* 2a. Copy & paste the whole code beneath this comment block to your functions.php | |
* 3. Remove the line starting with 'add_action...' at the end of this script | |
* and replace it with something like '$cipher_author = new CipherAuthor;' | |
* | |
* That's it. | |
*/ | |
if( !class_exists( 'CipherAuthor') ) | |
{ | |
class CipherAuthor | |
{ | |
public function __construct() | |
{ | |
// add filter & hooks | |
add_filter( 'author_link', array( &$this, 'author_cipher' ) ); | |
add_filter( 'query', array( &$this, 'author_decipher' ) ); | |
} | |
/** | |
* Part 1 | |
* Cipher the author nicename with md5 | |
*/ | |
public function author_cipher( $link ) | |
{ | |
global $wp_rewrite; | |
preg_match( "/.*\/(.*)?\//", $link, $parts ); | |
$author_nicename = md5( $parts[1] ); | |
$perma_link = $wp_rewrite->get_author_permastruct(); | |
if( empty( $perma_link ) ) | |
return $link; | |
$link = str_replace('%author%', $author_nicename, $perma_link); | |
$link = home_url( user_trailingslashit( $link ) ); | |
return $link; | |
} | |
/** | |
* Part 2 | |
* Decipher the author nicename in any query which contains the string 'user_nicename' | |
*/ | |
public function author_decipher( $query ) | |
{ | |
global $wpdb; | |
preg_match( "/user_nicename = '(.*)'/", $query, $md5 ); | |
if( !empty( $md5 ) ) | |
{ | |
$user_md5 = $md5[1]; | |
$users = $wpdb->get_col( "SELECT user_nicename FROM {$wpdb->users}" ); | |
if( in_array( $user_md5, $users ) ) | |
return $query; | |
$user_nicename = ''; | |
foreach( $users as $user ) | |
{ | |
if( $user_md5 == md5( $user ) ){ | |
$user_nicename = $user; | |
break; | |
} | |
} | |
if( '' != $user_nicename ) | |
{ | |
$query = str_replace($user_md5, $user_nicename, $query); | |
} | |
} | |
return $query; | |
} | |
} // end class | |
add_action( 'init', create_function( '', 'return new CipherAuthor;' ) ); | |
} // end if class exists |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment