template_redirect
Opis
Odpala się po ustaleniu, który szablon zostanie użyty. Idealne miejsce na przekierowania i blokowanie dostępu do treści.
Przykład użycia
add_action( 'template_redirect', 'wpdocs_require_login_for_books' );
function wpdocs_require_login_for_books() {
if ( is_singular( 'book' ) && ! is_user_logged_in() ) {
wp_safe_redirect( wp_login_url( get_permalink() ) );
exit;
}
}This action hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.
Note: Loading a different template is not a good use of this hook. If you include another template and then use exit() or die(), no subsequent template_redirect hooks will be run, which could break the site’s functionality. Instead, use the {@see 'template_include'} filter hook to return the path to the new template you want to use. This will allow an alternative template to be used without interfering with the WordPress loading process.
Wywołanie
// rejestracja funkcji zwrotnej
add_action( 'template_redirect', 'twoja_funkcja' );
function twoja_funkcja($param_1) {
// Twój kod
}Popularne w tej kategorii