サポート » プラグイン » プラグインが更新されても上書きされない 【プラグインのカスタマイズ】

  • ノンプログラマーの デザイン側の人間で、【 php の基礎的文法 】 も十分理解できておりません。
    都度 意味を調べている程度です。 時間の都合で、php をすぐ深く勉強もできません。

    WP にもあまり慣れていない者です。

    よって大変恐縮なのですが、WordPress の php の「本質」を 端的にご教授頂ける方がいらっしゃれば、大変幸いです。

    —————————

    更新されても上書きされない【テーマ】のカスタマイズに関しては、

    「子テーマ」内に 上書きする記述をした .php .css ファイルなどを作成しての(function.php のみ追記)、

    【元テーマが更新されても 都度上書きされないカスタマイズ】 のことが、ネット上でも多く書かれています。

    ただ、【プラグイン】に関しては、自分の探すダイレクトな情報 がすぐに見つからなかったので、教えを乞いたい次第です。

    ――――――――――――――

    「テーマを変更しても、元プラグインが更新されても、そのまま存続する【プラグイン】カスタマイズ」 ということで、「自作のプラグイン」 を作りアップロードして有効化しました。

    Custom Post Type UI を例に取ると(一応ツッコミのために思考経緯を書きます)

    このプラグインのデフォルトだと、WP 管理画面内において

    「新規カスタム分類(タクソノミー)」を作って その当該分類の「新規記事追加」画面をクリックで開くと 【 Add New 新規カスタム分類】 と最上部タイトルに表示されてしまいます。 この Add New 部分のみ、日本語に変えたいとします。

    しかし、Advanced Label Options より PO ファイルの登録を 「新規記事追加」などと日本語化すると、【新規記事追加】 と日本語しか最上部タイトルに表示されなくなり、

    「何のカスタム分類」 における新規記事投稿 なのか、一々「左メニュー内」を見ないと確認できない、ということになってしまいます。

    仮にここで Add New → 新規記事追加 だけを日本語化して、「カスタム分類名」 の表示は残したいときは、

    * Advanced Label Options の 3番目フィールド(input)の中に「変数」 を書き込むなどして、目的を達成することができるならカスタマイズも不要ですが(そんな書き方の方法はあるでしょうか?)、

    そのような方法ができないならば、当該部分を出力しているプラグイン php 内のメソッドを上書きする必要があるかと思います。

    「テーマ変更しても、プラグイン更新があっても、そのまま存続する形での上書きカスタマイズ」として、自作プラグイン php 内 でそれを行うとして。

    ―――――――――――――――

    そして、自作プラグイン内で、当該タイトル部分を表示させている
    custom-post-type-ui.php 内の cpt_create_custom_post_types() を部分アレンジした
    cpt_create_custom_post_types_jp () { ~ なるメソッドを作り、

    add_action( 'init', 'cpt_create_custom_post_types_jp', 0 );

    で読み込めば、一応 目的の結果 は達成できました。

    ただ、その内容は cpt_create_custom_post_types() { ~ の 全文をそのままコピペして、「 Add New 」を表示する部分のみを「新規記事追加(日本語)」に変更しただけのものです。 無駄な記述だらけのように思えますが。

    ——————————————

    ここで、WP と php の構造が良く分かっていない 自分の質問は、

    1)例えばこの場合

    $cpt_labels['add_new_item'] = ( !empty( $cpt_post_type[2]["add_new_item"] ) ) ? $cpt_post_type[2]["add_new_item"] : '[ 新規記事追加 ] ' .$cpt_singular;

    の 変数1つ(1行分)だけを書き換えたいのだが、
    前提となる変数定義 がその前にいろいろある場合、

    「その部分のみを短く他に置き換える」というような、自作プラグイン php の書き方はあるでしょうか?

    2)置き換えて上書きするには
    add_action( 'init', 'cpt_create_custom_post_types_jp', 0 );
    で正しいでしょうか?

    他のメソッド実行 の方が 普通でしょうか?

    3)仮に add_action で OKの場合
    自作プラグイン内にも、元プラグイン php 内にも $priority を 0 と書いていますが、一応目的である「当該部分日本語化」にはなっています。

    この【 プラグイン間 の $priority 0 の優劣 】 というのは、WP では どのように決まるのでしょうか?

    4)PO ファイルを上書きする 管理画面上の input の中に、「日本語」+「変数はそのまま出力するための記述」等を記述する方法、などはあるのでしょうか?

    のような点です。

    要は、「他のプラグインの1部だけ 自作プラグインでカスタマイズしたいときは、どういう記述方法で 自作プラグイン php を書くのがスマートなの? できるだけ短く」 という質問です。

    ——————————————-

    見当違いの質問をしていたら申し訳ありません。 WP と php の本質の理解不足ゆえ。

    ご教授頂ければ 大変幸いです。 宜しくお願い致します。

    * 一応、例に上げたメソッド全文を引用致します。

    function cpt_create_custom_post_types() {
    	//register custom post types
    	$cpt_post_types = get_option('cpt_custom_post_types');
    
    	//check if option value is an Array before proceeding
    	if ( is_array( $cpt_post_types ) ) {
    		foreach ($cpt_post_types as $cpt_post_type) {
    			//set post type values
    			$cpt_label              = ( !empty( $cpt_post_type["label"] ) ) ? esc_html( $cpt_post_type["label"] ) : esc_html( $cpt_post_type["name"] ) ;
    			$cpt_singular           = ( !empty( $cpt_post_type["singular_label"] ) ) ? esc_html( $cpt_post_type["singular_label"] ) : esc_html( $cpt_label );
    			$cpt_rewrite_slug       = ( !empty( $cpt_post_type["rewrite_slug"] ) ) ? esc_html( $cpt_post_type["rewrite_slug"] ) : esc_html( $cpt_post_type["name"] );
    			$cpt_rewrite_withfront  = ( !empty( $cpt_post_type["rewrite_withfront"] ) ) ? true : get_disp_boolean( $cpt_post_type["rewrite_withfront"] ); //reversed because false is empty
    			$cpt_menu_position      = ( !empty( $cpt_post_type["menu_position"] ) ) ? intval( $cpt_post_type["menu_position"] ) : null; //must be null
    			$cpt_menu_icon          = ( !empty( $cpt_post_type["menu_icon"] ) ) ? esc_attr( $cpt_post_type["menu_icon"] ) : null; //must be null
    			$cpt_taxonomies         = ( !empty( $cpt_post_type[1] ) ) ? $cpt_post_type[1] : array();
    			$cpt_supports           = ( !empty( $cpt_post_type[0] ) ) ? $cpt_post_type[0] : array();
    
    			//Show UI must be true
    			if ( true == get_disp_boolean( $cpt_post_type["show_ui"] ) ) {
    				//If the string is empty, we will need boolean, else use the string.
    				if ( empty( $cpt_post_type['show_in_menu_string'] ) ) {
    					$cpt_show_in_menu = ( $cpt_post_type["show_in_menu"] == 1 ) ? true : false;
    				} else {
    					$cpt_show_in_menu = $cpt_post_type['show_in_menu_string'];
    				}
    			} else {
    				$cpt_show_in_menu = false;
    			}
    
    			//set custom label values
    			$cpt_labels['name']             = $cpt_label;
    			$cpt_labels['singular_name']    = $cpt_post_type["singular_label"];
    
    			if ( isset ( $cpt_post_type[2]["menu_name"] ) ) {
    				$cpt_labels['menu_name'] = ( !empty( $cpt_post_type[2]["menu_name"] ) ) ? $cpt_post_type[2]["menu_name"] : $cpt_label;
    			}
    
    			$cpt_has_archive                    = ( !empty( $cpt_post_type["has_archive"] ) ) ? get_disp_boolean( $cpt_post_type["has_archive"] ) : '';
    			$cpt_exclude_from_search            = ( !empty( $cpt_post_type["exclude_from_search"] ) ) ? get_disp_boolean( $cpt_post_type["exclude_from_search"] ) : '';
    			$cpt_labels['add_new']              = ( !empty( $cpt_post_type[2]["add_new"] ) ) ? $cpt_post_type[2]["add_new"] : 'Add ' .$cpt_singular;
    			$cpt_labels['add_new_item']         = ( !empty( $cpt_post_type[2]["add_new_item"] ) ) ? $cpt_post_type[2]["add_new_item"] : 'Add New ' .$cpt_singular;
    			$cpt_labels['edit']                 = ( !empty( $cpt_post_type[2]["edit"] ) ) ? $cpt_post_type[2]["edit"] : 'Edit';
    			$cpt_labels['edit_item']            = ( !empty( $cpt_post_type[2]["edit_item"] ) ) ? $cpt_post_type[2]["edit_item"] : 'Edit ' .$cpt_singular;
    			$cpt_labels['new_item']             = ( !empty( $cpt_post_type[2]["new_item"] ) ) ? $cpt_post_type[2]["new_item"] : 'New ' .$cpt_singular;
    			$cpt_labels['view']                 = ( !empty( $cpt_post_type[2]["view"] ) ) ? $cpt_post_type[2]["view"] : 'View ' .$cpt_singular;
    			$cpt_labels['view_item']            = ( !empty( $cpt_post_type[2]["view_item"] ) ) ? $cpt_post_type[2]["view_item"] : 'View ' .$cpt_singular;
    			$cpt_labels['search_items']         = ( !empty( $cpt_post_type[2]["search_items"] ) ) ? $cpt_post_type[2]["search_items"] : 'Search ' .$cpt_label;
    			$cpt_labels['not_found']            = ( !empty( $cpt_post_type[2]["not_found"] ) ) ? $cpt_post_type[2]["not_found"] : 'No ' .$cpt_label. ' Found';
    			$cpt_labels['not_found_in_trash']   = ( !empty( $cpt_post_type[2]["not_found_in_trash"] ) ) ? $cpt_post_type[2]["not_found_in_trash"] : 'No ' .$cpt_label. ' Found in Trash';
    			$cpt_labels['parent']               = ( $cpt_post_type[2]["parent"] ) ? $cpt_post_type[2]["parent"] : 'Parent ' .$cpt_singular;
    
    			register_post_type( $cpt_post_type["name"], array(	'label' => __($cpt_label),
    				'public' => get_disp_boolean($cpt_post_type["public"]),
    				'singular_label' => $cpt_post_type["singular_label"],
    				'show_ui' => get_disp_boolean($cpt_post_type["show_ui"]),
    				'has_archive' => $cpt_has_archive,
    				'show_in_menu' => $cpt_show_in_menu,
    				'capability_type' => $cpt_post_type["capability_type"],
    				'map_meta_cap' => true,
    				'hierarchical' => get_disp_boolean($cpt_post_type["hierarchical"]),
    				'exclude_from_search' => $cpt_exclude_from_search,
    				'rewrite' => array( 'slug' => $cpt_rewrite_slug, 'with_front' => $cpt_rewrite_withfront ),
    				'query_var' => get_disp_boolean($cpt_post_type["query_var"]),
    				'description' => esc_html($cpt_post_type["description"]),
    				'menu_position' => $cpt_menu_position,
    				'menu_icon' => $cpt_menu_icon,
    				'supports' => $cpt_supports,
    				'taxonomies' => $cpt_taxonomies,
    				'labels' => $cpt_labels
    			) );
    		}
    	}
    }
  • トピック「プラグインが更新されても上書きされない 【プラグインのカスタマイズ】」には新たに返信することはできません。