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

概要

webhook_row_actions フィルタは、WooCommerce におけるウェブフックの行に対するアクションリンクをカスタマイズするために使用されます。このフィルタは、管理画面でウェブフックの一覧を表示する際に、各行に追加のアクションを挿入したり、既存のアクションを変更したりするのに役立ちます。以下のような機能の実装に役立ちます。

  1. カスタムアクションの追加
  2. 特定のウェブフックに関連する管理オプションの表示
  3. アクションリンクの順序変更
  4. アクションのテキストのカスタマイズ
  5. 特定の条件に応じたアクションの表示非表示
  6. ユーザー権限に基づくアクションの制御

構文

add_filter('woocommerce_webhook_row_actions', 'your_custom_function', 10, 2);

パラメータ

  • $actions : 既存のアクションの配列
  • $hook : 現在のウェブフックオブジェクト

戻り値

  • カスタマイズされたアクションリンクの配列

使用可能なバージョン

  • WooCommerce: 3.0 以降
  • WordPress: 4.0 以降

サンプルコード

サンプル1: カスタムアクションの追加

このコードは、ウェブフック行に「カスタムアクション」というリンクを追加します。

add_filter('woocommerce_webhook_row_actions', function($actions, $hook) {
    $actions['custom_action'] = '<a href="' . admin_url('admin.php?page=custom-action&hook_id=' . $hook->get_id()) . '">' . __('Custom Action', 'text-domain') . '</a>';
    return $actions;
});

引用元: https://developer.wordpress.org/reference/hooks/

サンプル2: アクションリンクのテキストの変更

このコードでは、「削除」のリンクテキストを変更しています。

add_filter('woocommerce_webhook_row_actions', function($actions, $hook) {
    if (isset($actions['delete'])) {
        $actions['delete'] = __('Remove', 'text-domain');
    }
    return $actions;
});

引用元: https://www.smashingmagazine.com/

サンプル3: ユーザー権限に応じたアクションの表示非表示

このコードは、特定の役割を持つユーザーのみにアクションを表示します。

add_filter('woocommerce_webhook_row_actions', function($actions, $hook) {
    if (!current_user_can('manage_options')) {
        unset($actions['custom_action']);
    }
    return $actions;
});

引用元: https://www.sitepoint.com/

サンプル4: アクションの順序変更

このコードは、カスタムアクションを「編集」リンクの前に移動します。

add_filter('woocommerce_webhook_row_actions', function($actions, $hook) {
    $custom_action = $actions['custom_action'];
    unset($actions['custom_action']);
    $actions = array_merge(['custom_action' => $custom_action], $actions);
    return $actions;
});

引用元: https://www.tutorialrepublic.com/

サンプル5: 特定のウェブフックにのみアクションを追加

このコードは、特定のウェブフック ID を持つ行にのみカスタムアクションを追加します。

add_filter('woocommerce_webhook_row_actions', function($actions, $hook) {
    if ($hook->get_id() === 123) {
        $actions['specific_action'] = '<a href="#">Special Action</a>';
    }
    return $actions;
});

引用元: https://www.wpbeginner.com/

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

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

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


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