Wordpress kärna och ordning
Här kan du hitta mer om Wordpress.
Om man ska bygga en hemsida i Wordpress med hjälp av plugins, teman och allmän PHP-kod via functions.php så kan det vara bra att känna till i vilken ordning Wordpress laddas internt. Genom att ha koll på detta så blir koden i projektet mycket bättre. Det är inte alltid så lätt att veta om alla konstanter och hooks som körs i systemet.
Ett exempel är att man ska lägga till lite kod som ska köras bara i admin men ej i frontend eller under AJAX requests. Många skulle så säga att man bara använder sig av is_admin(). Detta kommer inte att fungera eftersom den också retunerar true för AJAX request mot admin-ajax.php.
Eller så kanske du kör in lite kod via admin_init hooken och sedan undrar varför det funkar med AJAX requests med. Eller ett annat exempel. När någon kod exekveras i temats functions.php-fil och har beroende av en annan kod som är skriven i init hooken. Då kan det bli fel eftersom koden i functions.php körs före init.
Det finns massor av fall som dessa och då underlättar då väldigt mycket att förstå hur Wordpress laddar allt internt.
Wordpress-laddningsordning
Att förstå hela flödet med Wordpress kan ta ett tag att lära sig. Men jag tycker att denna illustration verkligen gör det lättare att förstå processen och ordningen.

Det finns fyra huvud varianter när den laddar Wordpress.
- FrontEnd (theme) laddning.
- REST request laddning.
- Admin panel laddning.
- AJAX request (admin-ajax.php) laddning.
I alla dessa fall så laddas kärnan wp-load.php.
WordPress kärnans laddningsordning i text
Som man ser nedan så körs allt genom wp-load.php
wp-load.php
wp-config.php
wp-settings.php
// Load functions: wp_debug_mode(), timer_start(), require_wp_db() ...
// Constants functions: wp_initial_constants(), wp_cookie_constants() ...
// Plugin functions (hooks, activation): do_action(), plugin_dir_url(), register_activation_hook().
// Constants setup: WP_START_TIMESTAMP, WP_MEMORY_LIMIT, WP_MAX_MEMORY_LIMIT, WP_DEBUG, SCRIPT_DEBUG, WP_CONTENT_DIR, WP_CACHE.
// Standardize server variables: wp_fix_server_vars().
// maintenance mode check: wp_maintenance().
// Enables load speed timer: timer_start().
// Debug mode check: wp_debug_mode().
// Includes 'wp-content/advanced-cache.php' if it is exists and WP_CACHE is on.
// Data base. $wpdb. require_wp_db().
// Loads 'wp-content/db.php' if it is exists. Creates DB connection
// and setups all DB related variables: prefixes and so on...
// Object cache: 'wp-content/object-cache.php' if exists or 'wp-include/cache.php'.
// Base WP hooks (filters): default-filters.php.
// Multisite is enabled (if need)
// Loads 'wp-content/sunrise.php' if exists (for multisite only).
// register_shutdown_function( 'shutdown_action_hook' )
// SHORTINIT: stopping the download, where there is only the most basic.
if( SHORTINIT )
return false;
// The localization functions are connected.
// Checks whether WP is installed: wp_not_installed().
// Connects a bunch of files with the rest of the WordPress functions.
// Connects Must-use plugins and corresponding action is triggered:
do_action( 'muplugins_loaded' );
// cookie, ssl constants: COOKIEPATH, COOKIE_DOMAIN
// Common global vars: $pagenow, $is_apache, $is_nginx, $is_lynx
// Client global vars: $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone, $is_IE, $is_edge
// Active plugins are connected
// pluggable functions are connected: pluggable.php.
// triggers hook
do_action( 'plugins_loaded' );
// Force add slashes for $_POST, $_REQUEST ... values. See. wp_magic_quotes().
// Global vars:
// $wp_the_query — new WP_Query()
// $wp_query — $wp_the_query
// $wp_rewrite — new WP_Rewrite() — constants, functions, rewrite rules.
// $wp — new WP() — base WP query (runs later).
// $wp_widget_factory — new WP_Widget_Factory()
// $wp_roles — new WP_Roles()
// Current theme
do_action( 'setup_theme' );
// functions.php (child) - first connect functions.php of the child theme
// functions.php (parent) - then connect functions.php of the main theme
// WP translation file: load_default_textdomain()
do_action( 'after_setup_theme' ); // first hook allowed in the theme
// Sets the current user (creates an object).
// See. wp_get_current_user()
// The user is often already defined by plugins after the 'plugins_loaded' action.
$wp->init();
// init action. The time when WP environment, themes and plugins is already activated,
// but nothing has been displayed on the screen yet:
do_action( 'init' );
// Widget registration: 'widget_init' action
// Checking site status for multisite.
// The site may be: deleted, inactive, in the archive. See. ms_site_check()
// If the site failed the check, the drop file will be called and PHP is aborted via die().
// Same as `init` only after the status check.
// This line of code can be not reached. For example on REST request.
do_action( 'wp_loaded' );
När man laddar kärnan så körs alltid functions.php i aktivt tema, även i Wordpress admin. Detta är väldigt smidigt för oss utvecklare. Om inte wp-load.php hittar wp-config.php så kommer installations processen komma igång. Man kan säga att wp-settings.php är en stor del av Wordpress kärnan.
Efter att konfigurationen i wp-config.php är funnen så kopplar Wordpress upp sig mot databasserven.
Ska fundera lite på nästa section i denna tråd, man kan läsa mera här. 