WordPress как на ладони
Очень Удобный и Быстрый Хостинг для сайтов на 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 700
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 122
add_action( 'init', 'register_block_core_archives' );
wp-includes/blocks/avatar.php 167
add_action( 'init', 'register_block_core_avatar' );
wp-includes/blocks/block.php 113
add_action( 'init', 'register_block_core_block' );
wp-includes/blocks/button.php 80
add_action( 'init', 'register_block_core_button' );
wp-includes/blocks/calendar.php 96
add_action( 'init', 'register_block_core_calendar' );
wp-includes/blocks/categories.php 105
add_action( 'init', 'register_block_core_categories' );
wp-includes/blocks/comment-author-name.php 69
add_action( 'init', 'register_block_core_comment_author_name' );
wp-includes/blocks/comment-content.php 85
add_action( 'init', 'register_block_core_comment_content' );
wp-includes/blocks/comment-date.php 62
add_action( 'init', 'register_block_core_comment_date' );
wp-includes/blocks/comment-edit-link.php 65
add_action( 'init', 'register_block_core_comment_edit_link' );
wp-includes/blocks/comment-reply-link.php 87
add_action( 'init', 'register_block_core_comment_reply_link' );
wp-includes/blocks/comment-template.php 153
add_action( 'init', 'register_block_core_comment_template' );
wp-includes/blocks/comments-pagination-next.php 64
add_action( 'init', 'register_block_core_comments_pagination_next' );
wp-includes/blocks/comments-pagination-numbers.php 66
add_action( 'init', 'register_block_core_comments_pagination_numbers' );
wp-includes/blocks/comments-pagination-previous.php 56
add_action( 'init', 'register_block_core_comments_pagination_previous' );
wp-includes/blocks/comments-pagination.php 50
add_action( 'init', 'register_block_core_comments_pagination' );
wp-includes/blocks/comments-title.php 101
add_action( 'init', 'register_block_core_comments_title' );
wp-includes/blocks/comments.php 102
add_action( 'init', 'register_block_core_comments' );
wp-includes/blocks/comments.php 227
add_action( 'init', 'register_legacy_post_comments_block', 21 );
wp-includes/blocks/cover.php 82
add_action( 'init', 'register_block_core_cover' );
wp-includes/blocks/file.php 73
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 183
add_action( 'init', 'register_block_core_gallery' );
wp-includes/blocks/heading.php 56
add_action( 'init', 'register_block_core_heading' );
wp-includes/blocks/home-link.php 179
add_action( 'init', 'register_block_core_home_link' );
wp-includes/blocks/image.php 322
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 162
add_action( 'init', 'register_block_core_latest_comments' );
wp-includes/blocks/latest-posts.php 237
add_action( 'init', 'register_block_core_latest_posts' );
wp-includes/blocks/legacy-widget.php 73
add_action( 'init', 'register_block_core_legacy_widget' );
wp-includes/blocks/list.php 54
add_action( 'init', 'register_block_core_list' );
wp-includes/blocks/loginout.php 55
add_action( 'init', 'register_block_core_loginout' );
wp-includes/blocks/media-text.php 121
add_action( 'init', 'register_block_core_media_text' );
wp-includes/blocks/navigation-link.php 426
add_action( 'init', 'register_block_core_navigation_link' );
wp-includes/blocks/navigation-submenu.php 267
add_action( 'init', 'register_block_core_navigation_submenu' );
wp-includes/blocks/navigation.php 1183
add_action( 'init', 'register_block_core_navigation' );
wp-includes/blocks/page-list-item.php 16
add_action( 'init', 'register_block_core_page_list_item' );
wp-includes/blocks/page-list.php 369
add_action( 'init', 'register_block_core_page_list' );
wp-includes/blocks/pattern.php 79
add_action( 'init', 'register_block_core_pattern' );
wp-includes/blocks/post-author-biography.php 53
add_action( 'init', 'register_block_core_post_author_biography' );
wp-includes/blocks/post-author-name.php 59
add_action( 'init', 'register_block_core_post_author_name' );
wp-includes/blocks/post-author.php 77
add_action( 'init', 'register_block_core_post_author' );
wp-includes/blocks/post-comments-form.php 70
add_action( 'init', 'register_block_core_post_comments_form' );
wp-includes/blocks/post-content.php 79
add_action( 'init', 'register_block_core_post_content' );
wp-includes/blocks/post-date.php 76
add_action( 'init', 'register_block_core_post_date' );
wp-includes/blocks/post-excerpt.php 82
add_action( 'init', 'register_block_core_post_excerpt' );
wp-includes/blocks/post-featured-image.php 268
add_action( 'init', 'register_block_core_post_featured_image' );
wp-includes/blocks/post-navigation-link.php 139
add_action( 'init', 'register_block_core_post_navigation_link' );
wp-includes/blocks/post-template.php 162
add_action( 'init', 'register_block_core_post_template' );
wp-includes/blocks/post-terms.php 129
add_action( 'init', 'register_block_core_post_terms' );
wp-includes/blocks/post-title.php 74
add_action( 'init', 'register_block_core_post_title' );
wp-includes/blocks/query-no-results.php 65
add_action( 'init', 'register_block_core_query_no_results' );
wp-includes/blocks/query-pagination-next.php 102
add_action( 'init', 'register_block_core_query_pagination_next' );
wp-includes/blocks/query-pagination-numbers.php 133
add_action( 'init', 'register_block_core_query_pagination_numbers' );
wp-includes/blocks/query-pagination-previous.php 88
add_action( 'init', 'register_block_core_query_pagination_previous' );
wp-includes/blocks/query-pagination.php 51
add_action( 'init', 'register_block_core_query_pagination' );
wp-includes/blocks/query-title.php 76
add_action( 'init', 'register_block_core_query_title' );
wp-includes/blocks/query.php 91
add_action( 'init', 'register_block_core_query' );
wp-includes/blocks/read-more.php 63
add_action( 'init', 'register_block_core_read_more' );
wp-includes/blocks/rss.php 123
add_action( 'init', 'register_block_core_rss' );
wp-includes/blocks/search.php 221
add_action( 'init', 'register_block_core_search' );
wp-includes/blocks/shortcode.php 35
add_action( 'init', 'register_block_core_shortcode' );
wp-includes/blocks/site-logo.php 119
add_action( 'init', 'register_block_core_site_logo' );
wp-includes/blocks/site-tagline.php 53
add_action( 'init', 'register_block_core_site_tagline' );
wp-includes/blocks/site-title.php 69
add_action( 'init', 'register_block_core_site_title' );
wp-includes/blocks/social-link.php 89
add_action( 'init', 'register_block_core_social_link' );
wp-includes/blocks/tag-cloud.php 58
add_action( 'init', 'register_block_core_tag_cloud' );
wp-includes/blocks/template-part.php 301
add_action( 'init', 'register_block_core_template_part' );
wp-includes/blocks/term-description.php 53
add_action( 'init', 'register_block_core_term_description' );
wp-includes/blocks/widget-group.php 62
add_action( 'init', 'register_block_core_widget_group' );
wp-includes/class-wp-customize-manager.php 378
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 355
add_action( 'init', '_register_core_block_patterns_and_categories' );
wp-includes/default-filters.php 356
add_action( 'init', 'check_theme_switched', 99 );
wp-includes/default-filters.php 357
add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 );
wp-includes/default-filters.php 390
add_action( 'init', 'wp_cron' );
wp-includes/default-filters.php 410
add_action( 'init', 'smilies_init', 5 );
wp-includes/default-filters.php 429
add_action( 'init', 'wp_schedule_delete_old_privacy_export_files' );
wp-includes/default-filters.php 461
add_action( 'init', '_show_post_preview' );
wp-includes/default-filters.php 511
add_action( 'init', 'rest_api_init' );
wp-includes/default-filters.php 518
add_action( 'init', 'wp_sitemaps_get_server' );
wp-includes/default-filters.php 547
add_action( 'init', 'create_initial_post_types', 0 ); // Highest priority.
wp-includes/default-filters.php 561
add_action( 'init', 'kses_init' );
wp-includes/default-filters.php 621
add_action( 'init', '_wp_footnotes_kses_init' );
wp-includes/default-filters.php 633
add_action( 'init', 'create_initial_taxonomies', 0 ); // Highest priority.
wp-includes/default-filters.php 652
add_action( 'init', 'wp_widgets_init', 1 );
wp-includes/default-filters.php 733
add_action( 'init', 'wp_register_persisted_preferences_meta' );
wp-includes/default-filters.php 736
add_action( 'init', 'wp_create_initial_post_meta' );
wp-includes/default-filters.php 754
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 1175
add_action( 'init', 'wp_schedule_update_checks' );
2 комментария
    Войти