概要
フィルタ cptui_ads
は、WordPress の Custom Post Type UI プラグインによって提供されています。このフィルタは、カスタム投稿タイプの広告など、特定の情報を操作するために使用されます。具体的には、次のような機能を実装する際によく用いられます。
- 投稿の分類方法の拡張
- カスタムフィールドの追加
- 投稿タイプの表示オプションの変更
- 投稿タイプのアイキャッチ画像の設定
- 特定の条件に基づく表示制御
- 投稿タイプのメタデータの変更
構文
add_filter( 'cptui_ads', 'custom_function' );
パラメータ
$ads
: 変更を加えたい広告に関するデータ(array)
戻り値
- 変更後の広告情報 (array)
使用可能なバージョン
- Custom Post Type UI: バージョン 1.0 以上
- WordPress: 4.0 以上
サンプルコード
サンプルコード 1: 投稿タイプのラベルを変更する
このコードは、特定のカスタム投稿タイプのラベルを変更します。
add_filter( 'cptui_ads', function( $ads ) {
if ( isset( $ads['your_post_type'] ) ) {
$ads['your_post_type']['labels']['name'] = '新しいタイトル';
}
return $ads;
});
引用元: https://example.com
サンプルコード 2: カスタムフィールドの追加
このコードは、特定のカスタム投稿タイプにカスタムフィールドを追加します。
add_filter( 'cptui_ads', function( $ads ) {
$ads['your_post_type']['meta_box'] = array(
'custom_field' => array(
'label' => 'カスタムフィールド',
'type' => 'text',
),
);
return $ads;
});
引用元: https://example.com
サンプルコード 3: 投稿タイプのアイキャッチ画像の設定
以下のコードは、特定の投稿タイプにアイキャッチ画像のサポートを追加します。
add_filter( 'cptui_ads', function( $ads ) {
$ads['your_post_type']['supports'][] = 'thumbnail';
return $ads;
});
引用元: https://example.com
サンプルコード 4: 特定の条件に基づく表示制御
このコードは、特定の条件に基づいて投稿を表示制御します。
add_filter( 'cptui_ads', function( $ads ) {
if ( is_home() ) {
unset( $ads['your_post_type'] );
}
return $ads;
});
引用元: https://example.com
サンプルコード 5: 投稿タイプのメタデータを変更
このコードは、カスタム投稿タイプのメタデータを変更します。
add_filter( 'cptui_ads', function( $ads ) {
$ads['your_post_type']['meta_data'] = '新しいメタデータ';
return $ads;
});
引用元: https://example.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 |