Forums

OK to init WP by manually incl. wp-config, functions, classes & plugins.php? (4 posts)

  1. emmern
    Member
    Posted 1 year ago #

    Hey!

    I am making a plugin with a file that is to be accessed directly from a device.
    It initializes wordpress manually (to get access to the functions) by doing this:

    // Get necessary stuff to access wordpress' functions
    define('ABSPATH', dirname(dirname(dirname(dirname(__FILE__)))) . '/');
    require_once(ABSPATH .'/wp-config.php');
    require_once(ABSPATH .'/wp-includes/classes.php');
    require_once(ABSPATH .'/wp-includes/functions.php');
    require_once(ABSPATH .'/wp-includes/plugin.php');
    $wp->init();

    Is this okay, or could it harm wordpress in any way?

    Thank you.

  2. emmern
    Member
    Posted 1 year ago #

    Oh, and my intention is NOT to show the frontpage, it is strictly to send data from wordpress to the client (such as recent posts, etc).

  3. Dion Hulse (@dd32)
    WordPress Dev
    Posted 1 year ago #

    Do not do that.

    Well, Specifically, there are 2 routes which should be mentioned:

    1. include wp-load.php, You do not need to include any other files or do any init work WordPress does it for you.
    2. define('ABSPATH', dirname(dirname(dirname(dirname(__FILE__)))) . '/'); is not always true/correct, the plugins directory does not have to exist at wp-content/plugins/ it can be moved elsewhere to such as /my-plugins/
    3. The best way is to direct the client application to http://my-url/wp-admin/admin-post.php or http://my-url/wp-admin/admin-ajax.php and use their loggedin/non-loggedin actions.
    5. Your other option is to use XML-RPC to communicate, which would probably be preferable.

  4. George Stephanis
    Member
    Posted 1 year ago #

    Easiest way is to build an on_init function, that checks for the existence of a named $_GET or $_POST variable ... such as $_GET['myapp_api'].

    Then just do whatever you want, as WP will already be loaded, pass back whatever data you need to, then exit; or wp_exit();

    I had a similar situation before, and this just works better than trying to finnick around with including files manually.

Topic Closed

This topic has been closed to new replies.

About this Topic