WordPress как на ладони

initхук-событиеWP 1.5.0

Событие срабатывает после того, как WordPress полностью загружен, но до того, как любые header заголовки были отправлены.

init - это популярное событие. Обычно используется плагинами для инициализации себя. Этот хук удобен по многим причинам: например, нужно определять пользователя, нужны таксономии, нужны функции установленные в теме (файл functions.php или другой).

К моменту срабатывания init текущий пользователь авторизован (global $current_user уже определена) и установлены все основные глобальные переменные и функции WordPress. Также функции темы уже подключены и все установки установлены: размеры картинок миниатюр, поддержка меню и т.д.

Вместо init можно использовать wp_loaded - это практически одинаковые события. wp_loaded срабатывает сразу после init и после проверки ms_site_check() (проверяет текущий блог в мультисайтовой сборке). wp_loaded не сработает, если текущий блог не пройдет проверку на работоспособность (не активен, удален, заспамлен, в архиве).

События которые срабатывают до init:
muplugins_loaded
registered_taxonomy
registered_post_type
plugins_loaded
sanitize_comment_cookies
setup_theme
load_textdomain
after_setup_theme
auth_cookie_malformed
auth_cookie_valid
set_current_user
init

Использование

add_action( 'init', 'wp_kama_init_action' );

/**
 * Function for `init` action-hook.
 * 
 * @return void
 */
function wp_kama_init_action(){

	// action...
}

Примеры

-1

#1 Подключение к другим событиям из init

add_action('init','all_my_hooks');
function all_my_hooks(){
	// подключение более поздних событий
	add_action('admin_init', 'my_function_name');
	add_action('admin_menu', 'my_function_name');
}
-2

#2 Обработка $_GET запроса

Допустим нам нужно перенаправить ссылку на страницу регистрации, если в запросе указана переменная register:

add_action('init', 'redirect_to_register');
function redirect_to_register(){
	if( isset( $_GET['register'] ) ) {
		wp_redirect( site_url() . 'wp-register.php');
		exit;
	}
}

Список изменений

С версии 1.5.0 Введена.

Где вызывается хук

В файле: /wp-settings.php
init
wp-settings.php 695
do_action( 'init' );

Где используется хук в WordPress

wp-includes/block-bindings/pattern-overrides.php 47
add_action( 'init', '_register_block_bindings_pattern_overrides_source' );
wp-includes/block-bindings/post-meta.php 70
add_action( 'init', '_register_block_bindings_post_meta_source' );
wp-includes/block-patterns.php 408
add_action( 'init', '_register_theme_block_patterns' );
wp-includes/blocks/archives.php 118
add_action( 'init', 'register_block_core_archives' );
wp-includes/blocks/avatar.php 150
add_action( 'init', 'register_block_core_avatar' );
wp-includes/blocks/block.php 107
add_action( 'init', 'register_block_core_block' );
wp-includes/blocks/calendar.php 89
add_action( 'init', 'register_block_core_calendar' );
wp-includes/blocks/categories.php 99
add_action( 'init', 'register_block_core_categories' );
wp-includes/blocks/comment-author-name.php 65
add_action( 'init', 'register_block_core_comment_author_name' );
wp-includes/blocks/comment-content.php 81
add_action( 'init', 'register_block_core_comment_content' );
wp-includes/blocks/comment-date.php 58
add_action( 'init', 'register_block_core_comment_date' );
wp-includes/blocks/comment-edit-link.php 61
add_action( 'init', 'register_block_core_comment_edit_link' );
wp-includes/blocks/comment-reply-link.php 83
add_action( 'init', 'register_block_core_comment_reply_link' );
wp-includes/blocks/comment-template.php 149
add_action( 'init', 'register_block_core_comment_template' );
wp-includes/blocks/comments-pagination-next.php 60
add_action( 'init', 'register_block_core_comments_pagination_next' );
wp-includes/blocks/comments-pagination-numbers.php 62
add_action( 'init', 'register_block_core_comments_pagination_numbers' );
wp-includes/blocks/comments-pagination-previous.php 52
add_action( 'init', 'register_block_core_comments_pagination_previous' );
wp-includes/blocks/comments-pagination.php 46
add_action( 'init', 'register_block_core_comments_pagination' );
wp-includes/blocks/comments-title.php 97
add_action( 'init', 'register_block_core_comments_title' );
wp-includes/blocks/comments.php 215
add_action( 'init', 'register_legacy_post_comments_block', 21 );
wp-includes/blocks/comments.php 96
add_action( 'init', 'register_block_core_comments' );
wp-includes/blocks/cover.php 78
add_action( 'init', 'register_block_core_cover' );
wp-includes/blocks/file.php 77
add_action( 'init', 'register_block_core_file' );
wp-includes/blocks/footnotes.php 112
add_action( 'init', 'register_block_core_footnotes_post_meta', 20 );
wp-includes/blocks/footnotes.php 78
add_action( 'init', 'register_block_core_footnotes' );
wp-includes/blocks/gallery.php 177
add_action( 'init', 'register_block_core_gallery' );
wp-includes/blocks/heading.php 52
add_action( 'init', 'register_block_core_heading' );
wp-includes/blocks/home-link.php 169
add_action( 'init', 'register_block_core_home_link' );
wp-includes/blocks/image.php 290
add_action( 'init', 'register_block_core_image' );
wp-includes/blocks/index.php 141
add_action( 'init', 'register_core_block_style_handles', 9 );
wp-includes/blocks/index.php 157
add_action( 'init', 'register_core_block_types_from_metadata' );
wp-includes/blocks/latest-comments.php 158
add_action( 'init', 'register_block_core_latest_comments' );
wp-includes/blocks/latest-posts.php 230
add_action( 'init', 'register_block_core_latest_posts' );
wp-includes/blocks/legacy-widget.php 67
add_action( 'init', 'register_block_core_legacy_widget' );
wp-includes/blocks/loginout.php 51
add_action( 'init', 'register_block_core_loginout' );
wp-includes/blocks/navigation-link.php 405
add_action( 'init', 'register_block_core_navigation_link' );
wp-includes/blocks/navigation-submenu.php 252
add_action( 'init', 'register_block_core_navigation_submenu' );
wp-includes/blocks/navigation.php 1104
add_action( 'init', 'register_block_core_navigation' );
wp-includes/blocks/page-list-item.php 14
add_action( 'init', 'register_block_core_page_list_item' );
wp-includes/blocks/page-list.php 357
add_action( 'init', 'register_block_core_page_list' );
wp-includes/blocks/pattern.php 77
add_action( 'init', 'register_block_core_pattern' );
wp-includes/blocks/post-author-biography.php 48
add_action( 'init', 'register_block_core_post_author_biography' );
wp-includes/blocks/post-author-name.php 54
add_action( 'init', 'register_block_core_post_author_name' );
wp-includes/blocks/post-author.php 73
add_action( 'init', 'register_block_core_post_author' );
wp-includes/blocks/post-comments-form.php 66
add_action( 'init', 'register_block_core_post_comments_form' );
wp-includes/blocks/post-content.php 75
add_action( 'init', 'register_block_core_post_content' );
wp-includes/blocks/post-date.php 72
add_action( 'init', 'register_block_core_post_date' );
wp-includes/blocks/post-excerpt.php 78
add_action( 'init', 'register_block_core_post_excerpt' );
wp-includes/blocks/post-featured-image.php 253
add_action( 'init', 'register_block_core_post_featured_image' );
wp-includes/blocks/post-navigation-link.php 135
add_action( 'init', 'register_block_core_post_navigation_link' );
wp-includes/blocks/post-template.php 156
add_action( 'init', 'register_block_core_post_template' );
wp-includes/blocks/post-terms.php 123
add_action( 'init', 'register_block_core_post_terms' );
wp-includes/blocks/post-title.php 72
add_action( 'init', 'register_block_core_post_title' );
wp-includes/blocks/query-no-results.php 59
add_action( 'init', 'register_block_core_query_no_results' );
wp-includes/blocks/query-pagination-next.php 96
add_action( 'init', 'register_block_core_query_pagination_next' );
wp-includes/blocks/query-pagination-numbers.php 127
add_action( 'init', 'register_block_core_query_pagination_numbers' );
wp-includes/blocks/query-pagination-previous.php 84
add_action( 'init', 'register_block_core_query_pagination_previous' );
wp-includes/blocks/query-pagination.php 47
add_action( 'init', 'register_block_core_query_pagination' );
wp-includes/blocks/query-title.php 72
add_action( 'init', 'register_block_core_query_title' );
wp-includes/blocks/query.php 89
add_action( 'init', 'register_block_core_query' );
wp-includes/blocks/read-more.php 59
add_action( 'init', 'register_block_core_read_more' );
wp-includes/blocks/rss.php 119
add_action( 'init', 'register_block_core_rss' );
wp-includes/blocks/search.php 219
add_action( 'init', 'register_block_core_search' );
wp-includes/blocks/shortcode.php 31
add_action( 'init', 'register_block_core_shortcode' );
wp-includes/blocks/site-logo.php 109
add_action( 'init', 'register_block_core_site_logo' );
wp-includes/blocks/site-tagline.php 41
add_action( 'init', 'register_block_core_site_tagline' );
wp-includes/blocks/site-title.php 65
add_action( 'init', 'register_block_core_site_title' );
wp-includes/blocks/social-link.php 84
add_action( 'init', 'register_block_core_social_link' );
wp-includes/blocks/tag-cloud.php 54
add_action( 'init', 'register_block_core_tag_cloud' );
wp-includes/blocks/template-part.php 289
add_action( 'init', 'register_block_core_template_part' );
wp-includes/blocks/term-description.php 49
add_action( 'init', 'register_block_core_term_description' );
wp-includes/blocks/widget-group.php 55
add_action( 'init', 'register_block_core_widget_group' );
wp-includes/class-wp-customize-manager.php 373
remove_action( 'init', 'wp_cron' );
wp-includes/class-wp-scripts.php 150
add_action( 'init', array( $this, 'init' ), 0 );
wp-includes/default-filters.php 151
add_action( 'init', 'wp_init_targeted_link_rel_filters' );
wp-includes/default-filters.php 354
add_action( 'init', '_register_core_block_patterns_and_categories' );
wp-includes/default-filters.php 355
add_action( 'init', 'check_theme_switched', 99 );
wp-includes/default-filters.php 356
add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 );
wp-includes/default-filters.php 389
add_action( 'init', 'wp_cron' );
wp-includes/default-filters.php 409
add_action( 'init', 'smilies_init', 5 );
wp-includes/default-filters.php 428
add_action( 'init', 'wp_schedule_delete_old_privacy_export_files' );
wp-includes/default-filters.php 460
add_action( 'init', '_show_post_preview' );
wp-includes/default-filters.php 510
add_action( 'init', 'rest_api_init' );
wp-includes/default-filters.php 517
add_action( 'init', 'wp_sitemaps_get_server' );
wp-includes/default-filters.php 546
add_action( 'init', 'create_initial_post_types', 0 ); // Highest priority.
wp-includes/default-filters.php 560
add_action( 'init', 'kses_init' );
wp-includes/default-filters.php 620
add_action( 'init', '_wp_footnotes_kses_init' );
wp-includes/default-filters.php 632
add_action( 'init', 'create_initial_taxonomies', 0 ); // Highest priority.
wp-includes/default-filters.php 651
add_action( 'init', 'wp_widgets_init', 1 );
wp-includes/default-filters.php 732
add_action( 'init', 'wp_register_persisted_preferences_meta' );
wp-includes/default-filters.php 735
add_action( 'init', 'wp_create_initial_post_meta' );
wp-includes/default-filters.php 753
add_action( 'init', '_wp_register_default_font_collections' );
wp-includes/ms-default-filters.php 16
add_action( 'init', 'ms_subdomain_constants' );
wp-includes/ms-default-filters.php 25
add_action( 'init', 'maybe_add_existing_user_to_blog' );
wp-includes/theme-previews.php 90
add_action( 'init', 'wp_attach_theme_preview_middleware' );
wp-includes/update.php 1172
add_action( 'init', 'wp_schedule_update_checks' );
2 комментария
    Войти