メインコンテンツに移動
ホーム

古松

メインナビゲーション

  • ホーム
  • ビデオ
  • ご連絡

パンくず

  • ホーム
  • WordPressのプラグイン作成:カスタム投稿タイプの作成

WordPressのプラグイン作成:カスタム投稿タイプの作成

カスタム投稿タイプ(Custom Post Type)がテーマ(Theme)に依存しないように自作プラグイン(Plugin)に入れます

  • 前回紹介したカスタム投稿タイプはそのテーマのfunctions.phpに定義しています
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
  • WordPressのテーマを変えたら、カスタム投稿タイプが消えてしまう問題が発生します(そのテーマのみに使用する場合は問題はありません)
  • テーマを依存しないように、カスタム投稿タイプを自作のプラグインに入れます

自作プラグイン(Plugin)名とフォルダ名のほかのプラグインに重複してはいけません

  • プラグイン名はユニックで、インストールしたほかのプラグイン名に重複してはいけません
    • 将来プラグインをインストールすることもあるので、WordPressのプラグイン名をチェックし他方が良いでしょう
  • プラグインのフォルダ名もインストールしたほかのプラグインフォルダ名と衝突してはいけません
  • プラグインフォルダに最低一つのphpファイル(プラグインの定義、動作記述)が必要です
    • phpファイル名がよくフォルダ名と同じようにつけていますが、必ず一致する必要はありません
  • 自作プラグイン(例:product-custom-type)をWordPressのプラグインフォルダ(wp-content/plugins)の下に置きます
    新規プラグインをWPのプラグインフォルダの下に置きます
  • 目的に合わせて、プラグインのフォルダに子フォルダの作成ができます(例:UI翻訳ファイルを置くフォルダ:languages)
  • セキュリティ配慮:有効化されていないプラグインのファイルアクセスをブロックすべきです
    • 有効化されていないプラグインファイル(php)の直接アクセスが可能です(URLが直接にファイルを指定すればできます)
    • プラグインが有効化していないので、直接にアクセスされた、不意な動作が発生してしまう可能性があります
    • 有効化されていないプラグインファイルをアクセスブロックは以下のコードで防ぐことができます
      // プラグインファイルの先頭に置きます
      // ABSPATH 定数が定義されていない場合、プラグインが有効かされていない
      defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

プラグイン(Plugin)定義をコメントでphpファイルの上に置きます

  • プラグイン名、バージョン、説明、作者などの情報をプラグインファイル(例:product-post-type.php)の先頭にコメントをつけて置きます
    <?php
    /*
    Plugin Name: Product Post Type
    Plugin URI: http://old-pine.net
    Description: Product of the custom post type which created with a new custom plugin
    Version: 1.0
    Author: Old-pine
    Author URI: http://old-pine.net/
    License: GPLv2
    */

    プラグインの定義

カスタム投稿タイプの定義をプラグインファイルに書きます

  • カスタム投稿タイプの定義(前回紹介したもの同じ)プラグイン(phpファイル)に書き込みます
    <?php
    /*
    Plugin Name: Product Post Type
    Plugin URI: http://old-pine.net
    Description: Product of the custom post type which created with a new custom plugin
    Version: 1.0
    Author: Old-pine
    Author URI: http://old-pine.net/
    License: GPLv2
    */
    
    defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    
    load_plugin_textdomain("product",false ,  'product-post-type/languages');
    
    add_action( 'init', 'my_custom_post_product' );
    function my_custom_post_product() {
        $labels = array(
            'name'               => _x( 'Products', 'post type general name', 'product' ),
            'singular_name'      => _x( 'Product', 'post type singular name', 'product' ),
            'add_new'            => _x( 'Add New', 'post type singular name', 'product' ),
            'add_new_item'       => __( 'Add New Product', 'product' ),
            'edit_item'          => __( 'Edit Product', 'product' ),
            'new_item'           => __( 'New Product', 'product' ),
            'all_items'          => __( 'All Products', 'product' ),
            'view_item'          => __( 'View Product', 'product' ),
            'search_items'       => __( 'Search Products', 'product' ),
            'not_found'          => __( 'No products found', 'product' ),
            'not_found_in_trash' => __( 'No products found in the Trash', 'product' ),
            'parent_item_colon'  => '',
            'menu_name'          => __('Products', 'product'),
        );
        $args = array(
            'labels'        		=> $labels,
            'description'   		=> 'Holds our products and product specific data',
            'public'        		=> true,
            'menu_position' 		=> 5,
            'supports'      		=> array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
            'has_archive'   		=> true,
            'hierarchical'          => false,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'menu_position'         => 5,
            'show_in_admin_bar'     => true,
            'show_in_nav_menus'     => true,
            'can_export'            => true,
            'has_archive'           => true,
            'exclude_from_search'   => false,
            'publicly_queryable'    => true,
            'capability_type'       => 'page',
        );
        register_post_type( 'product', $args );
    }
    
  • 上記定義をすれば、プラグイン管理画面に「Product Post Type」プラグインが現れます
    • 通常にプラグイン有効化操作でよいです

プラグインのUI翻訳ファイル(PO/MO):{WordPressグロバル言語フォルダ} or {プラグインのフォルダ}

  • プラグインのUI翻訳ファイルは二か所に置くことができます
    • WordPressグロバル言語フォルダ: wp-content/languages
    • プラグインフォルダ内: wp-content/plugins/product-post-type
  • 前回紹介したカスタム投稿タイプの翻訳ファイル(PO/MO)の作成方法は同じです
    • WordPressの新規投稿タイプ2:ユーザーインターフェースの翻訳方法
  • 翻訳ファイルの名前: {domain}-{locale}.mo
    • 前回のテーマフォルダに言語ファイル:{locale}.mo と違います
  • 翻訳ファイルを取り入れhookは; load_plugin_textdomain() を使用します
    load_plugin_textdomain("product",false ,  'product-post-type/languages');
    • 前回のhookは:load_theme_textdomain() を使用しました

WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法

WordPressの新しい投稿タイプの追加ができます

  • WordPressの初期設定では一つの投稿タイプがあります
    • フィールド追加したり、レイアウトなどの変更が可能です
  • 目的に合わせて複数の投稿タイプが必要とることがあります
    • 投稿タイプより、表示の分類ができます
    • 投稿タイプより、違うカスタムフィールド/カテゴリ/タグなど用意ができます
  • 新しい投稿タイプの作成方法が大体二種類あります
    • テーマフォルダにあるfunctions.phpに直接に定義します
    • プラグイン導入で新しい投稿タイプの作成

WordPressのテーマフォルダにあるfunctions.phpに直接に投稿タイプの定義

  • WordPressのカスタマイズなので、子テーマを作成して新規投稿タイプ追加したほうがよいでしょう
    • 例: Wordpressの子テーマ(Zerif)の作成 
  • WordPressの本家の新規投稿タイプ定義説明を参考して、基本的な定義は比較的に簡単です
    add_action( 'init', 'create_post_type' );
    function create_post_type() {
      register_post_type( 'acme_product',
        array(
          'labels' => array(
            'name' => __( 'Products' ),
            'singular_name' => __( 'Product' )
          ),
          'public' => true,
          'has_archive' => true,
        )
      );
    }
  • この例のように、新しい投稿タイプ「Products」を定義してみました
    function my_custom_post_product() {
      $labels = array(
        'name'               => _x( 'Products', 'post type general name' ),
        'singular_name'      => _x( 'Product', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'book' ),
        'add_new_item'       => __( 'Add New Product' ),
        'edit_item'          => __( 'Edit Product' ),
        'new_item'           => __( 'New Product' ),
        'all_items'          => __( 'All Products' ),
        'view_item'          => __( 'View Product' ),
        'search_items'       => __( 'Search Products' ),
        'not_found'          => __( 'No products found' ),
        'not_found_in_trash' => __( 'No products found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Products'
      );
      $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our products and product specific data',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
        'has_archive'   => true,
    		'hierarchical'          => false,
    		'show_ui'               => true,
    		'show_in_menu'          => true,
    		'menu_position'         => 5,
    		'show_in_admin_bar'     => true,
    		'show_in_nav_menus'     => true,
    		'can_export'            => true,
    		'has_archive'           => true,		
    		'exclude_from_search'   => false,
    		'publicly_queryable'    => true,
    		'capability_type'       => 'page',
      );
      register_post_type( 'product', $args ); 
    }
    add_action( 'init', 'my_custom_post_product' );
  • この定義を子テーマのfunctions.phpにコピー/ペストします
  • テストサイトをリフレッシュして、管理メニューに「Products」投稿タイプが現れます
    WordPressに新しい投稿タイプ作成

検索フォーム

カテゴリ別

  • laravel
  • drupal
  • javascript
  • windows
  • html
  • mysql
  • php
  • apache
  • css
  • SEO
  • video
  • wordpress
  • linux
  • python
  • Electron
  • Visual Studio Code

google ads