3

I want to do a small game for the users of my WP site. The game itself will only use the authentication system from WP, but all of its other logic is entirely external.

Right now, if I use a standard WordPress page with a custom template (embedding my game's PHP script) it loads, but seems to load too slowly for my users. My game script uses the same database with WP and it is fast. For tests I turned it off (just test output left), but even this "empty" page in WP is too slow.

Is it possible to bypass WordPress entirely (i.e. forgo the template system and everything except for the authentication flows) and load my game directly? My first guess would be to host a separate script in the root of my site's installation, but I can't figure out how to use just the authentication components of WordPress - is it possible to use just a single component from the WordPress engine in this way?

Maybe there's an alternative way to speed up the page hosting my game's script...

2
  • 2
    To reopen this, I'd recommend adding some details about how you're adding your game to the page. Is it a JavaScript snippet? Is it a separate PHP script loading inside a template? Does the game hit an external database? There could be any number of reasons the game is slow, so we need to either focus on speed here (which might be a question to be asked elsewhere than WPSE) or focus on plugging the auth components of WP into an external game script ...
    – EAMann
    Aug 24, 2016 at 4:49
  • Nobody can help?
    – Irina
    Aug 28, 2016 at 18:12

3 Answers 3

2
+100

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;
    }
}
2
  • I may be naive, but I'd think that after adding those first two lines of code you have access to is_user_logged_in() and that would be enough. What am I missing?
    – cjbj
    Sep 1, 2016 at 13:43
  • You mean the first line include wp-load.php? Right, after this all access to the WP functions, but you must include it with a static pfad, that is bad style and not solid, the path can change it.
    – bueltge
    Sep 1, 2016 at 17:34
1

I would think the cause of the slowdown would be related to all of the scripts WordPress is loading in wp_head(). If you're running any number of plugins, or any js/css that is render blocking, you'll see a big performance hit.

Try a loading a custom header file like header-mygame.php and replace the get_header() in your template file with get_header('mygame').

In header-mygame.php, remove wp_head() and anything else that's not needed to run your game. Add back only the dependancies that you need manually with <script> and <link> tags.

You can do the same with get_footer() as WordPress may be enqueuing scripts there as well.

Obviously this is not enqueuing assets in the 'WordPress way' but it seems that the WordPress way is already being circumvented, so...

1

Here are the steps which you can follow to use WP just for Authentication.

  1. Create a separate folder in the root for your code.
  2. Copy wp-config.php in a separate file and remove require_once ABSPATH . 'wp-settings.php';
  3. Include copied wp-config file in your code
  4. Code a helper function to invoke WP:

    function i_need_wp() {  
        global $table_prefix;
        require_once ABSPATH . 'wp-settings.php';
    }
    
  5. Call i_need_wp() before using WP authentication.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.