メインコンテンツに移動

メインナビゲーション

  • ホーム
  • サイトマップ
  • ビデオ
  • ご連絡

パンくず

  • ホーム
  • WordPressの新規投稿タイプ2:ユーザーインターフェースの翻訳方法

WordPressの新規投稿タイプ2:ユーザーインターフェースの翻訳方法

wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
wordpress
plugin
posts

カスタム投稿タイプ(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
posts

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に新しい投稿タイプ作成
Embedded thumbnail for WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
wordpress
system

WordPressのユーザーインターフェース言語翻訳はLocalization/Internationalizationと言います

  • Localizationはユーザーインターフェース言語翻訳以外にいろいろな意味があります
    • ローカル関連の日付表示
    • 通貨などの単位
  • Internationalizationは文字セットなどを管理しています
    • 読み順序(左から右、右から左)
    • 文言/書式など
  • Localizationの略: l10n
    • 英語の綴りの最初(l)から最後(n)までの間に10文字があったので、"l10n"に簡略
  • Intanationalizationの略:i18n
    • 英語の綴りの最初(i)から最後(n)までの間に18文字があったので、"i18n" に簡略

WordPressのユーザーインターフェース翻訳はGUNのgettext翻訳システムを採用しています

  • WordPressのユーザーインターフェース翻訳システムを開発していないので、GUNのgettext翻訳システムを採用しています
  • GUNのgettext翻訳システムは複数タイプのファイル(POT、PO、MO)があります
  • POT(Portable Object Template): テキストファイル。基本言語(英語)文言が入っていますが、翻訳は空白です
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(空白)。例:msgstr: ""
  • PO(Potable Object):テキストファイル。一つ言語に一つのPOファイルが作成します
    • msgid: 基本言語の文言(英語)。例: msgid: "test message"
    • msgstr: 翻訳文言(日本語)。例:msgstr: "テストメッセージ"
  • MO(Machine Object):バイナリファイル。POファイルからコンパイルして生成されます
    GUNのgettext翻訳システム紹介

POTファイル:テキストエディタで作成、PO/MOファイルがPoeditで作成(例)

  • POTファイルが開発ソースファイルにあるユーザーインターフェースの文言の集約です
    • 翻訳はそれぞれの国の人に任せて、POTファイルと関係ない
    • POTファイルがテキストエディタで作成されます
  • POファイルの作成/編集/コンパイルがいろいろな方法があります(gettextがオーペンソースなので、いろいろなフリーツールがあります)
    • ここでは、一般的によく利用されているPoeditを紹介します
  • POファイルがPoeditよりPOTファイルを読み込んで、変換し、翻訳文言を入力ます
    • 文字セットの決定(どこの国の文言に翻訳か)
    • 文言の翻訳をします(例: msgid:"test message" => msgstr:"テストメッセージ")
  • MOファイルがPoeditより翻訳した内容(POファイル)をコンパイルして、バイナリフォーマットとして保存します
    • テキストエディタで編集などはできません
    • テキスト文言変更の場合にPOファイルの翻訳文言を変更して、再度コンパイルします
    • テキスト文言の追加/削除/元文言変更はPOTファイル⇒POファイルまで

POTファイルが必須ではない(POファイルだけでもよい)

  • POTファイルが英文字通りに、ほかの言語への翻訳テンプレート(翻訳内容はない)
  • テキストエディタでPOファイル作成してもよいです
    • いくつかの定義をファイルの前に置きます
      msgid ""
      msgstr ""
      "MIME-Version: 1.0\n"
      "Content-Type: text/plain; charset=ISO-8859-1\n"
      "Content-Transfer-Encoding: 8bit\n"
      "Language: ja\n"
    • UTF-8フォーマットでファイル保存
  • 作成したPOファイルをPoeditで開く、編集して、保存するとMOファイルも自動的に作成されます
  • 複数の言語に翻訳しなければ、POファイルだけで十分です
    • 複数の言語に翻訳する場合、POTファイルがテンプレートとして利用されます
Embedded thumbnail for WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
wordpress
posts

やりたいこと:カスタム投稿タイプ(Custom Post Type)のUIを日本語化

  • 前回紹介したカスタム投稿タイプ、多言語翻訳用のPOT/PO/MOファイル
    • WordPressの新規投稿タイプ1:functions.phpファイルに定義する方法
    • WordPressの多言語翻訳1:POT,PO,MOファイルの理解と作成
  • 作成したカスタム投稿タイプのユーザーインターファイルを日本語化します
    カスタム投稿タイプのUIを日本語に翻訳

カスタム投稿タイプUIの日本語翻訳ファイル(PO/MO)を作成します

  • Poeditで日本語翻訳ファイル(PO)を作成します
    msgid ""
    msgstr ""
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=1; plural=0;\n"
    "X-Generator: Poedit 2.0.3\n"
    "Language: ja_JP\n"
    "Project-Id-Version: \n"
    "POT-Creation-Date: \n"
    "PO-Revision-Date: \n"
    "Last-Translator: \n"
    "Language-Team: \n"
    
    msgid "Products"
    msgstr "製品投稿"
    
    msgid "Product"
    msgstr "製品"
    
    msgid "Add New"
    msgstr "新規作成"
    
    msgid "Add New Product"
    msgstr "新規製品作成"
    
    msgid "Edit Product"
    msgstr "製品編集"
    
    msgid "New Product"
    msgstr "新規製品"
    
    msgid "All Products"
    msgstr "全製品"
    
    msgid "View Product"
    msgstr "製品表示"
    
    msgid "Search Products"
    msgstr "製品検"
    
    msgid "No products found"
    msgstr "製品を見つからない"
    
    msgid "No products found in the Trash"
    msgstr "ゴミ箱から製品を見つからない"
    
  • 上記POファイルをPoeditでコンパイルしてMOファイルに変換します

翻訳ファイルの配置は:{所属しているテーマフォルダ内} or {WPの言語フォルダ内}

  • 翻訳ファイルは所属しているテーマのフォルダ内に配置することができます
    • 今回の例では twentyseventeen-child (twentyseventeenの子テーマ)
    • 翻訳ファイルの命名規則: {locale}.po、{locale}.mo
      • localeは日本語の場合: ja.po、ja.mo となります
    • テーマフィールドに「languages」フォルダを作成、その下に配置します
      所属テーマのフォルダに翻訳ファイルの配置
  • WPの言語フォルダに配置することもできます
    • WPの言語フォルダ:wp-content/languages
    • 翻訳ファイルの命名規則: {domain}-{locale}.po、{domain}-{locale}.mo
      • 今回の例のテキストドメイン(text domain)名: product
      • 翻訳ファイル名:product-ja.po、product-ja.mo
    • 翻訳ファイル名を変更して、WPの言語フォルダに配置します
      翻訳ファイルWPの言語フォルダに配置

カスタム投稿タイプ登録する前に作成した翻訳ファイルをロードします

  • WPの言語フォルダに配置する場合、翻訳フォイルのロード記述する必要はありません(自動ロードとなります)
  • 所属しているテーマのフォルダに配置する場合、翻訳ファイルをロードする記述は必要となります
    • カスタム投稿タイプ登録(functions.php)する前にロードが必要です
      // 翻訳ファイルをロード
      load_theme_textdomain("product", get_stylesheet_directory() . '/languages');
      
      // カスタム投稿タイプ登録
      add_action( 'init', 'my_custom_post_product' );
      
    • 翻訳ファイルのディレクト取得関数:get_stylesheet_directory() なので、get_stylesheet_directory_url()ではありません(後者の場合、翻訳ファイルのロードはできません)。また、get_template_directory()関数の使用はできません(これは親テーマのディレクトリを取得する)
    • load_child_theme_textdomain()で翻訳ファイルのロードもできます
ホーム

古松

検索

Article Category

  • apache(7)
  • css(19)
  • drupal(295)
  • Electron(4)
  • html(34)
  • javascript(27)
  • laravel(4)
  • linux(5)
  • macOS(2)
  • mysql(13)
  • php(19)
  • python(4)
  • SEO(12)
  • video(72)
  • Visual Studio Code(4)
  • windows(13)
  • wordpress(32)