3

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.

  • You may need to register an endpoint for the API. See this: add_rewrite_endpoint – Ahmed Fouad Sep 27 '16 at 7:16
  • I've been attempting this but nothing I'm attempting works so if you have an answer with examples I'll definitely give it a try. I'll update my answer right now with some things I've tried – Xtremefaith Sep 27 '16 at 7:22
  • Right. posting your code attempts will certainly help us :) I mentioned that function because I'm using it to create custom API endpoint for a plugin, that's why. – Ahmed Fouad Sep 27 '16 at 7:24
  • 1
    add_rewrite_rule would be the appropriate function to start searching for, but I would note that standalone endpoints in your plugin are a security risk as they will work even when your plugin is disabled, and for all sites on a multisite regardless of settings – Tom J Nowell Sep 27 '16 at 7:28
  • So what do you suggest @TomJNowell ? – Xtremefaith Sep 27 '16 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.