概要
woocommerce_share
は、WooCommerceが提供するフィルターフックであり、商品や特定のコンテンツが共有される際のURLやメッセージのカスタマイズを可能にします。このフィルタは、オンライン店舗のソーシャルメディア共有機能や、他のマーケティング活動を強化するために使用されます。以下のような多様な機能に役立てられます。
- ソーシャルメディアシェアボタンのカスタマイズ
- 商品ページに特定のメッセージを追加
- URLの短縮やトラッキング機能の追加
- 共有時に異なる画像を指定
- 共有用のカスタムリンクを生成
- 他のプラグインとの統合によるシェア機能の拡張
構文
add_filter( 'woocommerce_share', 'your_function_name', 10, 2 );
パラメータ
$content
: デフォルトの共有リンク。通常は商品のURL。$id
: 共有対象となる商品のID。
戻り値
- カスタマイズされた共有リンクやメッセージ。
対応バージョン
- WooCommerce: バージョン 2.1 以降
- WordPress: バージョン 4.0 以降
サンプルコード
サンプルコード1: シェアリンクにクエリパラメータを追加
add_filter( 'woocommerce_share', 'custom_add_query_param', 10, 2 );
function custom_add_query_param( $content, $id ) {
$content .= '?utm_source=social&utm_medium=share';
return $content;
}
このコードは、シェアリンクにUTMパラメータを追加し、どのソーシャルメディアからのトラフィックかを追跡するのに役立ちます。
サンプルコード2: シェアボタンのカスタムメッセージ
add_filter( 'woocommerce_share', 'custom_share_message', 10, 2 );
function custom_share_message( $content, $id ) {
return 'Check out this product: ' . get_permalink( $id );
}
このコードは、商品を共有する際にカスタマイズされたメッセージを表示します。
サンプルコード3: 特定の画像を共有時に使用
add_filter( 'woocommerce_share', 'custom_share_image', 10, 2 );
function custom_share_image( $content, $id ) {
$image_url = wp_get_attachment_url( get_post_thumbnail_id( $id ) );
$content .= '&image=' . urlencode( $image_url );
return $content;
}
このコードでは、商品のサムネイル画像をシェアリンクに含めることができます。
サンプルコード4: シェアリンクを特定のURLにリダイレクト
add_filter( 'woocommerce_share', 'redirect_share_link', 10, 2 );
function redirect_share_link( $content, $id ) {
return 'https://example.com/redirect?product_id=' . $id;
}
このコードは、商品のシェアリンクが特定のリダイレクトURLに誘導されるように設定します。
サンプルコード5: 商品の価格をシェアメッセージに追加
add_filter( 'woocommerce_share', 'add_price_to_share_message', 10, 2 );
function add_price_to_share_message( $content, $id ) {
$product = wc_get_product( $id );
$price = $product->get_price();
return $content . ' - Price: ' . $price;
}
このコードは、商品のシェアメッセージにその商品の価格を加えます。
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
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 |
この表は、woocommerce_share
フィルタがWooCommerceやWordPressの各アクションで使用される可能性を示しています。使用例がない場合は空欄にしています。