プラグインWooCommerceのwoocommerce_file_download_filenameフィルタの使用方法・解説

概要

woocommerce_file_download_filename フィルタは、WooCommerceでデジタルファイルのダウンロード名をカスタマイズするために使用されます。このフィルタを使用することで、ファイル名に独自のプレフィックスやサフィックスを追加したり、よりわかりやすい名前に変更したりすることができます。これにより、ユーザーがダウンロードしたファイルをより簡単に特定できるようになります。

このフィルタは、以下のような用途でよく使われます:

  1. ファイル名に商品名を追加する
  2. ダウンロード日付をファイル名に含める
  3. ユーザー名やIDをファイル名に追加する
  4. ファイルのバージョン番号を含める
  5. カテゴリ名をファイル名に組み込む
  6. 特定のフォルダーにファイルを格納する際に整理しやすい名前を付ける

構文

add_filter( 'woocommerce_file_download_filename', 'custom_filename_function', 10, 2 );

パラメータ

  • woocommerce_file_download_filename: フィルターフックの名前
  • custom_filename_function: フィルターを適用するコールバック関数の名前
  • 第一引数: ダウンロードファイルの元のファイル名(文字列)
  • 第二引数: ダウンロードする商品のID(整数)

戻り値

カスタマイズされたファイル名(文字列)

使用可能なバージョン

  • WooCommerce: 2.6以降
  • 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_filter( 'woocommerce_file_download_filename', 'add_product_name_to_filename', 10, 2 );

function add_product_name_to_filename( $filename, $product_id ) {
    $product = wc_get_product( $product_id );
    return $product->get_name() . '-' . $filename;
}

このコードは、ダウンロードファイル名の先頭に商品名を追加します。

引用元: https://docs.woocommerce.com/wc-apidocs/class-WC_Product_Download.html

サンプル 2: ダウンロード日付をファイル名に含める

add_filter( 'woocommerce_file_download_filename', 'add_date_to_filename', 10, 2 );

function add_date_to_filename( $filename, $product_id ) {
    $date = date( 'Y-m-d' );
    return $date . '-' . $filename;
}

このコードは、ダウンロードファイル名の先頭に日付を追加します。

引用元: https://wordpress.org/support/article/using-filters/

サンプル 3: ユーザー名をファイル名に追加

add_filter( 'woocommerce_file_download_filename', 'add_username_to_filename', 10, 2 );

function add_username_to_filename( $filename, $product_id ) {
    $username = wp_get_current_user()->user_login;
    return $username . '-' . $filename;
}

このコードは、ダウンロードファイル名の先頭に現在のユーザー名を追加します。

引用元: https://developer.wordpress.org/reference/functions/wp_get_current_user/

サンプル 4: バージョン番号をファイル名に含める

add_filter( 'woocommerce_file_download_filename', 'add_version_to_filename', 10, 2 );

function add_version_to_filename( $filename, $product_id ) {
    return $filename . '-v1.0';
}

このコードは、ダウンロードファイル名の末尾にバージョン番号を追加します。

引用元: https://developer.wordpress.org/reference/functions/add_filter/

サンプル 5: 商品のカテゴリ名をファイル名に組み込む

add_filter( 'woocommerce_file_download_filename', 'add_category_to_filename', 10, 2 );

function add_category_to_filename( $filename, $product_id ) {
    $terms = get_the_terms( $product_id, 'product_cat' );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
        $category_name = $terms[0]->name;
        return $category_name . '-' . $filename;
    }
    return $filename;
}

このコードは、ダウンロードファイル名の先頭に商品カテゴリ名を追加します。

引用元: https://developer.wordpress.org/reference/functions/get_the_terms/

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


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