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

概要

woocommerce_page_created アクションは、WooCommerceで新しいページが作成されたときに発火します。このアクションは、以下のような機能を実装する際によく使われます。

  1. 商品情報の初期値設定
  2. カスタムメタデータの追加
  3. ページに特別なスタイルやスクリプトを適用
  4. 外部サービスとのデータ同期
  5. 管理通知の送信
  6. ページのSEO設定の自動生成

構文

do_action( 'woocommerce_page_created', $post_id, $post_title, $post_content );

パラメータ

  • $post_id (int): 作成されたページのID。
  • $post_title (string): 作成されたページのタイトル。
  • $post_content (string): 作成されたページのコンテンツ。

戻り値

このアクションは値を戻しませんが、フックされた関数が何らかの処理を実行することが可能です。

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

  • WooCommerce: バージョン 2.0.0 以降
  • WordPress: バージョン 4.0 以降

サンプルコード

サンプルコード1: 商品ページのカスタムメタデータ追加

このコードは、新しい商品ページが作成されたときにカスタムメタデータを追加します。

add_action( 'woocommerce_page_created', 'add_custom_meta_to_product' );

function add_custom_meta_to_product( $post_id, $post_title, $post_content ) {
    if ( 'product' === get_post_type( $post_id ) ) {
        update_post_meta( $post_id, '_custom_meta_key', '初期値' );
    }
}
// 参考: https://developer.wordpress.org/reference/functions/update_post_meta/

サンプルコード2: 商品ページ用にカスタムスタイルを追加

新しい商品ページが作成された際に、特定のスタイルを追加するコードです。

add_action( 'woocommerce_page_created', 'add_custom_styles_for_product_page' );

function add_custom_styles_for_product_page( $post_id, $post_title, $post_content ) {
    if ( 'product' === get_post_type( $post_id ) ) {
        wp_enqueue_style( 'custom-product-styles', get_template_directory_uri() . '/css/custom-product.css' );
    }
}
// 参考: https://developer.wordpress.org/reference/functions/wp_enqueue_style/

サンプルコード3: ページ作成時に通知を送信

このコードは新しいページが作成されたときに、管理者に通知を送信します。

add_action( 'woocommerce_page_created', 'notify_admin_on_product_creation' );

function notify_admin_on_product_creation( $post_id, $post_title, $post_content ) {
    if ( 'product' === get_post_type( $post_id ) ) {
        $admin_email = get_option( 'admin_email' );
        wp_mail( $admin_email, '新しい商品が作成されました', '商品タイトル: ' . $post_title );
    }
}
// 参考: https://developer.wordpress.org/reference/functions/wp_mail/

サンプルコード4: SEO設定の自動生成

新しい商品ページが作成されたとき、そのSEO設定を自動てした相のコードです。

add_action( 'woocommerce_page_created', 'set_seo_meta_for_product' );

function set_seo_meta_for_product( $post_id, $post_title, $post_content ) {
    if ( 'product' === get_post_type( $post_id ) ) {
        update_post_meta( $post_id, '_seo_title', $post_title );
        update_post_meta( $post_id, '_seo_description', 'この商品は ' . $post_title . ' です。' );
    }
}
// 参考: https://developer.wordpress.org/reference/functions/update_post_meta/

サンプルコード5: 外部APIとのデータ同期

新しい商品ページが作成された際に、外部APIにデータを送信するコードです。

add_action( 'woocommerce_page_created', 'sync_product_with_external_api' );

function sync_product_with_external_api( $post_id, $post_title, $post_content ) {
    if ( 'product' === get_post_type( $post_id ) ) {
        $data = array(
            'id' => $post_id,
            'title' => $post_title,
        );
        wp_remote_post( 'https://example.com/api/sync', array(
            'method'    => 'POST',
            'body'      => json_encode( $data ),
            'headers'   => array( 'Content-Type' => 'application/json' ),
        ));
    }
}
// 参考: https://developer.wordpress.org/reference/functions/wp_remote_post/

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

アクション 使用例
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

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


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