プラグインElementorのelementor/template_library/sources/local/register_category_argsアクションの使用方法・解説

概要

elementor/template_library/sources/local/register_category_argsは、Elementorプラグインのフックの一部で、ローカルテンプレートライブラリにカテゴリーを追加する際の引数を変更するために使用されます。このアクションは、ユーザーがテンプレートのカテゴリを管理し、整理するのに役立つ機能を提供します。

このアクションは、以下のような機能を実装する際によく使われます:

  1. 新しいテンプレートカテゴリーの追加
  2. テンプレートカテゴリーの表示名の変更
  3. テンプレートのフィルタリングオプションの提供
  4. テンプレートカテゴリーの順序を変更
  5. カスタムアイコンの追加
  6. 特定の条件に基づいてカテゴリーを非表示にする

構文

add_action( 'elementor/template_library/sources/local/register_category_args', 'your_function_name' );

パラメータ

  • $args: カテゴリーの引数を含む配列

戻り値

特に戻り値はありませんが、引数として渡されたカテゴリー情報が変わる可能性があります。

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

  • Elementor: バージョン3.0以降
  • WordPress: バージョン5.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( 'elementor/template_library/sources/local/register_category_args', function( $args ) {
    $args['new_category'] = [
        'title'  => __( '新しいカテゴリー', 'text-domain' ),
        'icon'   => 'icon-new-category',
        'order'  => 1,
    ];
    return $args;
});

このサンプルコードは、新しいカテゴリー「新しいカテゴリー」をテンプレートライブラリに追加します。アイコンと表示順も設定されています。

サンプル2: 見出し名のカスタマイズ

add_action( 'elementor/template_library/sources/local/register_category_args', function( $args ) {
    if( isset( $args['existing_category'] ) ) {
        $args['existing_category']['title'] = __( '変更された見出し名', 'text-domain' ); 
    }
    return $args;
});

このサンプルでは、既存のカテゴリーの見出し名を「変更された見出し名」に変更しています。

サンプル3: カテゴリーの順序変更

add_action( 'elementor/template_library/sources/local/register_category_args', function( $args ) {
    if( isset( $args['some_category'] ) ) {
        $args['some_category']['order'] = 5; 
    }
    return $args;
});

ここでは、指定されたカテゴリーの表示順を5に変更しています。

サンプル4: カテゴリーの非表示

add_action( 'elementor/template_library/sources/local/register_category_args', function( $args ) {
    unset( $args['unwanted_category'] ); 
    return $args;
});

このコードは、「unwanted_category」という名前のカテゴリーをテンプレートライブラリから削除します。

サンプル5: 特定の条件でカテゴリーを設定

add_action( 'elementor/template_library/sources/local/register_category_args', function( $args ) {
    if ( is_user_logged_in() ) {
        $args['premium_category'] = [
            'title' => __( 'プレミアムカテゴリー', 'text-domain' ),
            'icon'  => 'icon-premium',
        ];
    }
    return $args;
});

このサンプルは、ユーザーがログインしている場合にのみ「プレミアムカテゴリー」を追加します。

各サンプルはElementorの機能拡張を行うために役立ちますが、著作権がフリーであることに注意しながら活用してください。

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


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