Development‎ > ‎Wordpress関連‎ > ‎

Custom Field GUI Utilityの使用上の注意

概要

Custom Field GUI Utility はWordPress のカスタムフィールドを使いやすくするプラグインです。
しかし、
マルチブログ化している場合には、注意点があります。
ここでは、その注意点をご説明致します。


対象

本記事は、Wordpress3で且つ、Custom Field GUI Utilityが3.2.7での検証となります。

注意点

Custom Field GUI Utilityは単一サイトでプラグインとして使用する場合には、管理者にとって非常に便利なプラグインなのですが、下記特徴があります。
  • 自身のフォルダー内のINIファイルを参照する。
つまり、
マルチブログの場合には、「サイトによってカスタムフィールドの内容を分けることができない」と言うことです。

対応方法

重要なことは、
マルチブログの場合で且つ、サイト毎にカスタムフィールドの内容を変えたい場合に発生します。

この場合、Custom Field GUI Utilityの複製を作成して、プラグインをアクティブにする必要があるのですが、
(「自身のフォルダー内のINIファイルを参照する。」という特性から)
単純に複製しただけでは、プラグインをアクティベートする段系で、エラーとなってしまいます。
それは、提供されている「cfg-utility.php」中に、関数が固定で定義されているからです。
これを回避させるには、下記対応が必要となります。
  • 既にローディングされている関数を排除。
  • 必須の関数名を変更し、新しい関数を定義する。
  • プラグインフォルダも変更。
    下記では「Site1」としていますので、フォルダ名も「custom-field-gui-utility
    -Site1」にします。
    しかし、このフォルダ名は衝突しなければ何でも構いません。
下記の赤色部分を追加することで、対応できます。「cfg-utility.php」
この「cfg-utility.php」を入れ替えるだけです。
対応した内容は、
  • 下記で出現している関数以外を削除する。
    既にローディングされているはずだからです。
  • 下記赤色部分を追加する。
    これらは、サイト固有の対応を行うために必要ですので、既にローディングされている関数との衝突を防ぐ目的で、関数名称を変更しています。
    これ自体新しいプラグインとなりますので、どのサイトのプラグインになったのか?判断材料にもなります。

<?php
/*
-- This Plugin's Information --------------------------------
  Plugin Name: Custom Field Gui Utility - Site1
  Plugin URI: http://www.tinybeans.net/blog/download/wp-plugin/cfg-utility-3.html
  Description: WordPress 3.3x用。カスタムフィールドを使いやすくするプラグイン「Custom Field GUI」のカスタマイズ版。オリジナルプラグインの作者は、 <a href="http://rhymedcode.net">Joshua Sigar氏</a>。
  Author: Tomohiro Okuwaki
  Author URI: http://www.tinybeans.net/blog/
  Version: 3.2.7
  Customize: Tomohiro Okuwaki (http://www.tinybeans.net/blog/)
  Thanks: @hadakadenkyu <http://twitter.com/hadakadenkyu>
-- This Plugin's Information --------------------------------

-- Original Plugin's Information --------------------------------
  Original Plugin's Name: rc:custom_field_gui
  Original Plugin's URI: http://rhymedcode.net/projects/custom-field-gui
  Original Plugin's Description: Automatically adds form element(s) in Write Post panel, which act as a Post's custom field(s). Configuration is thru conf.ini. Instruction is on readme.txt.
  Original Plugin's Author: Joshua Sigar
  Original Plugin's Version: 1.5
  Original Plugin's Author URI: http://rhymedcode.net
-- /Original Plugin's Information --------------------------------
*/ 

/*
rc:custom_field_gui
Licensed under the MIT License
Copyright (c)  2005 Joshua Sigar

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

require_once (ABSPATH . 'wp-admin/includes/template.php');

add_action ('admin_head','insert_head_Site1');
add_action ('add_meta_boxes', 'isert_custom_field_gui_Site1');

/* edit_post : 投稿記事またはページが更新・編集された際に実行する。これには、コメントが追加・更新された場合(投稿またはページのコメント数が更新される)も含む。 */
add_action('edit_post', 'edit_meta_value_Site1');

/* save_post : インポート機能の利用、記事・ページ編集フォームの利用、XMLRPCでの投稿、メールでの投稿のうちいずれかの方法で記事・ページが作成・更新された際に実行する。 */
add_action('save_post', 'edit_meta_value_Site1');

/* publish_post : 投稿記事が公開された際、または公開済みの記事の情報が編集された際に実行する。 */
add_action('publish_post', 'edit_meta_value_Site1');

/* transition_post_status : 記事・ページが公開された際、またはステータスが「公開」に変更された場合に実行する。 */
add_action('transition_post_status', 'edit_meta_value_Site1');

/******************
   Functions(main)
 ******************/

/* 管理画面のhead要素でCSSとJavaScriptファイルの読み込み */
function insert_head_Site1() {
    $plugin_url = get_bloginfo('wpurl') . '/wp-content/plugins/custom-field-gui-utility-Site1/';
    $head = <<< EOD
    <link rel="stylesheet" href="{$plugin_url}facebox/facebox.css" type="text/css" media="all" />
    <link rel="stylesheet" href="{$plugin_url}cfg-utility.css" type="text/css" media="all" />
    <link rel="stylesheet" href="{$plugin_url}exValidation/css/exvalidation.css" type="text/css" />
    <script type="text/javascript" src="{$plugin_url}facebox/facebox.js"></script>
    <script type="text/javascript" src="{$plugin_url}exValidation/js/exvalidation.js"></script>
    <script type="text/javascript" src="{$plugin_url}exValidation/js/exchecker-ja.js"></script>
    <script type="text/javascript" src="{$plugin_url}cfg-utility.js"></script>
    <script type="text/javascript">
    jQuery(function($){
        $("form#post").exValidation();
    });
    </script>
EOD;
    echo $head;
}

/* add_meta_boxesで実行する関数 */
function isert_custom_field_gui_Site1 ($post_type = 'post', $post = NULL) {
    add_meta_box('cfg_utility', get_field_title($post_type), 'insert_gui', $post_type, 'normal', 'high');
}

function edit_meta_value_Site1($post_id) {
    if ($post_id == 0) {
        return $post_id;
    }
    global $wpdb;
    if (!current_user_can('edit_post', $post_id)) {
        return $post_id;
    }
    $nonce = isset($_REQUEST['custom-field-gui-verify-key']) ? $_REQUEST['custom-field-gui-verify-key']: '';
    if (!wp_verify_nonce($nonce, 'custom-field-gui')) {
        return $post_id;
    }
    global $post;
    $fields = get_conf_ini($post->post_type);
    if (!$fields) {
        return $post_id;
    }
    foreach($fields as $meta_key => $data) {
        $name = 'cfg_' . sanitize_name($meta_key);
        $data_type = isset($data['type']) ? $data['type']: '';
        if ($data_type == 'hr' or $meta_key == 'cfgu_setting') {
            continue;
        }
        $meta_value = isset($_REQUEST["$name"]) ? stripslashes(trim($_REQUEST["$name"])): '';
        if (isset($meta_value) && !empty($meta_value)) {
            delete_post_meta($post_id, $meta_key);
            if ($data_type == 'textfield' || 
                $data_type == 'imagefield' || 
                $data_type == 'filefield' || 
                $data_type == 'multi_checkbox' ||
                $data_type == 'radio'  ||
                $data_type == 'select' || 
                $data_type == 'textarea') {
                add_post_meta($post_id, $meta_key, $meta_value);
            } elseif ($data['type'] == 'checkbox') {
                add_post_meta($post_id, $meta_key, 'true');
            }
        } elseif (isset($meta_value) && strval($meta_value) === '0') {
            add_post_meta($post_id, $meta_key, '0');
        } else {
            delete_post_meta($post_id, $meta_key);
        }
    }
}
?>


本来このような対応よりも、実行されるサイトを判断し、オリジナルのinsert_head()のプラグインURLを指定する箇所
$plugin_url = get_bloginfo('wpurl') . '/wp-content/plugins/custom-field-gui-utility-Site1/';
を変更することが最適なのかもしれませんが、
基本となるプラグインを尊重して、上記のような対応をとる方法をご紹介しています。

SelectionFile type iconFile nameDescriptionSizeRevisionTimeUser
ċ

表示
WordPress のカスタムフィールドを使いやすくするプラグイン。自由にカスタムフィールドを作成出来るが、定義自体はINIファイルに記載して、サーバーにアップロードする必要がある。マルチブログ化している場合には、プラグインの修正が必要となるので要注意。  2012/11/11 16:18 浅野利博