I am attempting an OAuth call to a 3rd party service but having issues with my callback script. I have a file oauth.php that is within my plugin directory.
First question, do I need to go to the url of this script? For example do I need my login button to link to http://example.com/wp-content/my-plugin/oauth.php
? Or is there a way to make this into an endpoint so that I can just link to http://example.com/my-provider/oauth/
?
Second question, once I go to this page, at the moment it seems none of the hooks are firing. For example if I add_action('init', 'test')
I can’t get anything to print with the test function. I have a feeling this has to do with my first question and I’m not understanding fully.
UPDATE: Progress Report
I’ve looked through many forums and tutorials and a constant suggestion I’m seeing references add_rewrite_endpoint
. One tutorial made it seem that this is what I’m looking for but when I attempted to use it I still ran into issues.
private function init(){
# Register query vars
add_action( 'init', array( $this, 'endpoints') );
add_action( 'init', array( $this, 'register_query_vars') );
add_action( 'template_redirect', array( $this, 'template_redirect_intercept') );
}
public function endpoints(){
add_rewrite_endpoint( 'oauth', EP_ATTACHMENT | EP_PAGES );
}
public function register_query_vars() {
$this->register_rewrites();
global $wp;
$wp->add_query_var( 'oauth' );
}
function register_rewrites() {
add_rewrite_rule( '^oauth/(.+)','index.php?oauth','top' );
}
function template_redirect_intercept( $template ) {
global $wp_query;
if ( is_page('{provider}') && isset( $wp_query->query_vars['oauth'] ) ) {
require_once dirname( __FILE__ ) . '/oauth.php';
exit;
}
return $template;
}
With the following I can navigate to http://example.com/{provider}/oauth/
and the page loads as expected WITH WordPress initialized!! Only issue now is the following error message:
Warning: preg_match(): Compilation failed: nothing to repeat at offset 1 in C:\..\example.dev\wp-includes\class-wp.php on line 231
I know another possibilty is hooking a query var in the init
but I was trying to setup a specific callback URL if possible.