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...
}

Примеры

0

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

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

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

#2 Подключение к другим событиям из 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');
}

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

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

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

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

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

wp-includes/block-patterns.php 337
add_action( 'init', '_register_theme_block_patterns' );
wp-includes/blocks/archives.php 121
add_action( 'init', 'register_block_core_archives' );
wp-includes/blocks/avatar.php 146
add_action( 'init', 'register_block_core_avatar' );
wp-includes/blocks/block.php 66
add_action( 'init', 'register_block_core_block' );
wp-includes/blocks/calendar.php 70
add_action( 'init', 'register_block_core_calendar' );
wp-includes/blocks/categories.php 101
add_action( 'init', 'register_block_core_categories' );
wp-includes/blocks/comment-author-name.php 62
add_action( 'init', 'register_block_core_comment_author_name' );
wp-includes/blocks/comment-content.php 78
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 58
add_action( 'init', 'register_block_core_comment_edit_link' );
wp-includes/blocks/comment-reply-link.php 80
add_action( 'init', 'register_block_core_comment_reply_link' );
wp-includes/blocks/comment-template.php 133
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 43
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 101
add_action( 'init', 'register_block_core_comments' );
wp-includes/blocks/comments.php 220
add_action( 'init', 'register_legacy_post_comments_block', 21 );
wp-includes/blocks/cover.php 81
add_action( 'init', 'register_block_core_cover' );
wp-includes/blocks/file.php 58
add_action( 'init', 'register_block_core_file' );
wp-includes/blocks/gallery.php 122
add_action( 'init', 'register_block_core_gallery' );
wp-includes/blocks/home-link.php 153
add_action( 'init', 'register_block_core_home_link' );
wp-includes/blocks/image.php 42
add_action( 'init', 'register_block_core_image' );
wp-includes/blocks/index.php 29
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 214
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 346
add_action( 'init', 'register_block_core_navigation_link' );
wp-includes/blocks/navigation-submenu.php 283
add_action( 'init', 'register_block_core_navigation_submenu' );
wp-includes/blocks/navigation.php 677
add_action( 'init', 'register_block_core_navigation' );
wp-includes/blocks/page-list.php 344
add_action( 'init', 'register_block_core_page_list' );
wp-includes/blocks/pattern.php 44
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.php 61
add_action( 'init', 'register_block_core_post_author' );
wp-includes/blocks/post-comments-form.php 64
add_action( 'init', 'register_block_core_post_comments_form' );
wp-includes/blocks/post-content.php 82
add_action( 'init', 'register_block_core_post_content' );
wp-includes/blocks/post-date.php 56
add_action( 'init', 'register_block_core_post_date' );
wp-includes/blocks/post-excerpt.php 69
add_action( 'init', 'register_block_core_post_excerpt' );
wp-includes/blocks/post-featured-image.php 194
add_action( 'init', 'register_block_core_post_featured_image' );
wp-includes/blocks/post-navigation-link.php 96
add_action( 'init', 'register_block_core_post_navigation_link' );
wp-includes/blocks/post-template.php 128
add_action( 'init', 'register_block_core_post_template' );
wp-includes/blocks/post-terms.php 107
add_action( 'init', 'register_block_core_post_terms' );
wp-includes/blocks/post-title.php 61
add_action( 'init', 'register_block_core_post_title' );
wp-includes/blocks/query-no-results.php 61
add_action( 'init', 'register_block_core_query_no_results' );
wp-includes/blocks/query-pagination-next.php 73
add_action( 'init', 'register_block_core_query_pagination_next' );
wp-includes/blocks/query-pagination-numbers.php 101
add_action( 'init', 'register_block_core_query_pagination_numbers' );
wp-includes/blocks/query-pagination-previous.php 61
add_action( 'init', 'register_block_core_query_pagination_previous' );
wp-includes/blocks/query-pagination.php 45
add_action( 'init', 'register_block_core_query_pagination' );
wp-includes/blocks/query-title.php 75
add_action( 'init', 'register_block_core_query_title' );
wp-includes/blocks/query.php 16
add_action( 'init', 'register_block_core_query' );
wp-includes/blocks/read-more.php 45
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 164
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 104
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 62
add_action( 'init', 'register_block_core_site_title' );
wp-includes/blocks/social-link.php 80
add_action( 'init', 'register_block_core_social_link' );
wp-includes/blocks/tag-cloud.php 61
add_action( 'init', 'register_block_core_tag_cloud' );
wp-includes/blocks/template-part.php 261
add_action( 'init', 'register_block_core_template_part' );
wp-includes/blocks/term-description.php 45
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 371
remove_action( 'init', 'wp_cron' );
wp-includes/class-wp-scripts.php 143
add_action( 'init', array( $this, 'init' ), 0 );
wp-includes/default-filters.php 143
add_action( 'init', 'wp_init_targeted_link_rel_filters' );
wp-includes/default-filters.php 694
add_action( 'init', 'wp_register_persisted_preferences_meta' );
wp-includes/default-filters.php 623
add_action( 'init', 'wp_widgets_init', 1 );
wp-includes/default-filters.php 602
add_action( 'init', 'create_initial_taxonomies', 0 ); // Highest priority.
wp-includes/default-filters.php 551
add_action( 'init', 'kses_init' );
wp-includes/default-filters.php 537
add_action( 'init', 'create_initial_post_types', 0 ); // Highest priority.
wp-includes/default-filters.php 511
add_action( 'init', 'wp_sitemaps_get_server' );
wp-includes/default-filters.php 504
add_action( 'init', 'rest_api_init' );
wp-includes/default-filters.php 454
add_action( 'init', '_show_post_preview' );
wp-includes/default-filters.php 424
add_action( 'init', 'wp_schedule_delete_old_privacy_export_files' );
wp-includes/default-filters.php 406
add_action( 'init', 'smilies_init', 5 );
wp-includes/default-filters.php 385
add_action( 'init', 'wp_schedule_https_detection' );
wp-includes/default-filters.php 381
add_action( 'init', 'wp_cron' );
wp-includes/default-filters.php 348
add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 );
wp-includes/default-filters.php 347
add_action( 'init', 'check_theme_switched', 99 );
wp-includes/default-filters.php 346
add_action( 'init', '_register_core_block_patterns_and_categories' );
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/update.php 1103
add_action( 'init', 'wp_schedule_update_checks' );
2 комментария
    Войти