Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple OAuth script to use with your wp plugins.
* OAuth script for WordPress
* @author Louy Alakkad <>
* @website
if( !defined( 'WP_OAUTH' ) ) :
define('WP_OAUTH', true);
* Don't forget to call oauth_activate() when you activate your plugin.
* just to make sure rewrite rules will be flushed.
add_action('init', 'oauth_init');
function oauth_init() {
global $wp, $oauth_activate;
add_rewrite_rule('oauth/(.+)/?$', 'index.php?oauth=$matches[1]',1);
add_rewrite_rule('oauth/?', 'index.php?oauth=null',1);
add_action('template_redirect', 'oauth_template_redirect');
function oauth_template_redirect() {
if( get_query_var('oauth') ) {
$oauth_sites = apply_filters('oauth_sites', array());
$site = explode('/',get_query_var('oauth'));
$site = $site[0];
if( !in_array($site, $oauth_sites)) {
die( __('OAuth site not recognized!') );
function oauth_link($site,$args=array()){
$link = get_bloginfo('url');
$link .= '/oauth/' . $site;
$link = add_query_arg($args, $link);
return $link;
function oauth_activate() {

azizur commented Sep 20, 2010

For every:

add_action('init', 'oauth_init');

You are potentially calling flush_rewrite_rules();

This could cause performance issue.

Instead call flush_rewrite_rules(); on plugin activate and deactivate action hook.

See: for more info.

ozh commented Sep 20, 2010

This is a nice example of the rewrite API, but by no means it is an "oauth script"...


louy commented Sep 20, 2010

Well, azizur. looks like i've fixed that already.
when you activate your plugin, you just need to set $oauth_activate to true on activation.

as for deactivation, i think we shouldn't remove the rule as some other plugins may want to use it, anyway i guess user will get a 404 error page on "/oauth/" if the OAuth plugin is deactivated, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment