プラグインWooCommerceのwoocommerce_registration_redirectアクションの使用方法・解説

概要

woocommerce_registration_redirectは、WooCommerceプラグイン内でユーザーが新規アカウントを登録した後にリダイレクトされるURLを変更するためのフックです。このアクションは、カスタムの登録処理を行いたい場合や、ユーザーエクスペリエンスを向上させるためにリダイレクト先を変更する際によく使用されます。具体的な利用シーンとしては次のようなケースが考えられます:

  1. 特定のカスタムページにリダイレクトしたい場合
  2. 登録後のメッセージが表示されるページにリダイレクトする場合
  3. 日本語の特定のURLにリダイレクトしたい場合
  4. セールやプロモーションのランディングページに誘導したい場合
  5. ユーザーを特定のカテゴリーページにリダイレクトしたい場合
  6. 他のプラグインと連携して異なる処理を行いたい場合

構文

add_action( 'woocommerce_registration_redirect', 'custom_redirect_after_registration' );

パラメータ

  • $redirect: リダイレクト先のURL(デフォルトで設定されているURL)

戻り値

  • 変更されたリダイレクト先のURLを返す。

使用可能なプラグイン及びバージョン

  • WooCommerce: 2.4.0以上
  • WordPress: 4.0以上

この関数のアクションでの使用可能性

アクション名 使用例
mu_plugin_loaded
registered_post_type
plugins_loaded
wp_roles_init
setup_theme
after_setup_theme
set_current_user
init
register_sidebar
wp_loaded
send_headers
parse_query
pre_get_posts
wp
template_redirect
get_header
wp_head

サンプルコード

サンプルコード1

add_action( 'woocommerce_registration_redirect', 'custom_redirect_to_custom_page' );

function custom_redirect_to_custom_page( $redirect ) {
    return home_url( '/welcome/' );
}

このサンプルコードは、ユーザー登録後に「/welcome/」ページへリダイレクトします。

サンプルコード2

add_action( 'woocommerce_registration_redirect', 'redirect_to_contact_page' );

function redirect_to_contact_page( $redirect ) {
    return home_url( '/contact/' );
}

このコードは、新規登録後に「/contact/」というコンタクトページへリダイレクトします。

サンプルコード3

add_action( 'woocommerce_registration_redirect', 'goto_registration_success_page' );

function goto_registration_success_page( $redirect ) {
    return home_url( '/registration-success/' );
}

このサンプルでは、「/registration-success/」という登録成功ページにリダイレクトします。

サンプルコード4

add_action( 'woocommerce_registration_redirect', 'redirect_to_promotions' );

function redirect_to_promotions( $redirect ) {
    return home_url( '/promotions/' );
}

このコードは、新規登録後に「/promotions/」というプロモーションページへリダイレクトします。

サンプルコード5

add_action( 'woocommerce_registration_redirect', 'goto_user_dashboard' );

function goto_user_dashboard( $redirect ) {
    return home_url( '/my-account/' );
}

このサンプルコードは、登録後にユーザーアカウントページ「/my-account/」へリダイレクトします。

この関数について質問する


上の計算式の答えを入力してください