Utilize WordPress Authentication Only

Not sure, but currently in my mind the first solution. Do you need the data from WordPress for the authentication. If you include the wp-load.php you have access to WordPress and his functions, maybe to identifier. But the file to include as static path is not really great and solid.

define( 'WP_USE_THEMES', FALSE );
require( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

Query and redirect

You can change the query with a custom value and if this value is existent you can redirect via hook to your game. After this redirect you access to all wp functions, also to check the users. This idea works only in front-end, no solution to change pages in the back-end, only as hint.

The follow source is only an example to enhance the query with a custom string to identifier and redirect to your custom code.

add_filter( 'query_vars', 'fb_plugin_add_trigger', 10, 1 );
function fb_plugin_add_trigger( $vars ) {

    $vars[] = 'fb_plugin_trigger';
    return $vars;
}

add_action('template_redirect', 'fb_plugin_trigger_check');
function fb_plugin_trigger_check() {

    if ( (int) get_query_var('fb_plugin_trigger') === 1 ) {

                // Here is your custom code.
            exit();
    }
}

The URL for this trigger is as example example.com/?fb_plugin_trigger=1.

Custom endpoint

Also a option there you should try. Add a custom endpoint.

add_action( 'init', 'fb_my_endpoint' );
function fb_my_endpoint() {

    add_rewrite_endpoint( 'fb_plugin_trigger', EP_ROOT );
}

After this you will find the value in the url, like example.com/fb_plugin_trigger/.

Small hint, flush the rewrite rules after adding of the endpoint. Also a hint, if you don’t use the permalinks, use the function add_query_arg() to add parameters in the url to identifier.

Now you can also parse this value in the query and include your game source.

add_action( 'parse_query', 'fb_parse_query' );
function fb_parse_query( $query ) {

    if ( isset( $query->query_vars['my_api'] ) ) {
        include( plugin_dir_path( __FILE__ ) . 'game/index.php');
        exit;
    }
}

Leave a Comment