WordPress.org

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Action Reference/wp loaded

Description

This action hook is fired once WordPress, all plugins, and the theme are fully loaded and instantiated.

Usage

add_action( 'wp_loaded', 'your callback function' );

Example

Minify HTML codes when page is output

add_action( 'wp_loaded','my_minify_html' );
function my_minify_html() {
	// Use html_compress($html) function to minify html codes.
	ob_start('html_compress');
}

function html_compress( $html ) {
	// Some minify codes here...
	return $html;
}

If you only want to load a function only in the front end.

// If u want to load a function only in the front end.
add_action( 'wp_loaded', 'my_front_end_function');
function my_front_end_function() {
    if ( !is_admin() ) { 
        // Only target the front end
        // Do what you need to do
    }
}

Same as above, but using anonymous function (PHP 5.3 or higher).

add_action( 'wp_loaded', function () {
    if ( !is_admin() ) {
        // Only target the front end
        // Do what you need to do
    }
});

Notes

AJAX requests should use wp-admin/admin-ajax.php. admin-ajax.php can handle requests for users not logged in.

The wp_loaded action fires after init but before admin_init.

  • Front-End: init -> widgets_init -> wp_loaded
  • Admin: init -> widgets_init -> wp_loaded -> admin_menu -> admin_init

Change Log

Since: 3.0

Source File

The wp_loaded action is located in wp-settings.php