Skip to content

Plugin Container (_xe_plugin())

The Xe_Plugin\Plugin class acts as the central service container for the entire plugin. It is exposed globally via the helper function _xe_plugin().


Global Access Function

/**
 * Returns the singleton instance of the Plugin class.
 *
 * @return \Xe_Plugin\Plugin
 */
function _xe_plugin(): \Xe_Plugin\Plugin {
  return \Xe_Plugin\Plugin::instance();
}

Available Container Services

You can access plugin services anywhere across your codebase using _xe_plugin():

1. Options (options())

Provides access to the PluginOptions singleton for reading and updating settings in wp_options.

// Retrieve an option (falls back to Defaults if not set in DB)
$sample_value = _xe_plugin()->options()->get( 'sample' );

// Save a new value
_xe_plugin()->options()->set( 'sample', 'Updated Value' );

// Get all options
$all_options = _xe_plugin()->options()->all();

2. Defaults (defaults())

Accesses default values registered in Xe_Plugin\Defaults.

$default_currency = _xe_plugin()->defaults()->get( 'currency', 'USD' );
$all_defaults = _xe_plugin()->defaults()->all();

3. Endpoints (endpoints())

Manages rewrite endpoints and generates URLs.

// Get the full URL to the dashboard endpoint
$dashboard_url = _xe_plugin()->endpoints()->get_url_by_key( 'xep-dashboard' );

// Get the current endpoint's key
$current_key = _xe_plugin()->endpoints()->get_key();

// Check if currently viewing a specific endpoint
if ( _xe_plugin()->endpoints()->is_current( 'xep-login' ) ) {
  // Logic for login endpoint...
}

4. Page Templates (templates())

Manages virtual page templates.

// Get page ID assigned to a custom template
$login_page_id = _xe_plugin()->templates()->get_page_id( 'xep-login' );

// Check if current page is using a specific template
if ( _xe_plugin()->templates()->is_current( 'xep-dashboard' ) ) {
  // Logic for dashboard template...
}

Path & URL Utilities

The container provides reliable path and URL resolution helpers that handle slashes automatically:

// Get full server path to a plugin directory or file
$template_path = _xe_plugin()->path( 'templates/custom-view.php' );
// Output: /var/www/html/wp-content/plugins/my-plugin/templates/custom-view.php

// Get full URL to a plugin asset
$image_url = _xe_plugin()->url( 'assets/img/logo.png' );
// Output: https://example.com/wp-content/plugins/my-plugin/assets/img/logo.png

// Get path to the main plugin entry file
$main_file = _xe_plugin()->file();

Plugin Metadata (data())

Retrieves cached metadata from the plugin's header docblock using get_plugin_data():

$version = _xe_plugin()->data()->Version;
$name    = _xe_plugin()->data()->Name;
$author  = _xe_plugin()->data()->Author;