[WordPress] ACFのカスタムフィールドグループをphp形式で書き出す際の注意点

作成日:

Advanced Custom Fieldsで設定したカスタムフィールドグループを、直接テーマのfunctions.phpに書き加えるphp形式で書き出すことが出来ますが、その際に1点注意が必要です。

普段、フィールドグループ名はわかりやすく日本語で書いていても特に問題は無いのですが、functions.phpに書き加える際には、「半角英数字」にしておかないと、

  • 目的とした箇所以外にフィールドグループが表示される
  • 記事の書き込み中にフィールドグループが非表示になる

のようなエラーが起きます。

これは、フィールドグループ名を日本語にしていると、下のようにidがURLエンコード(?)されることによって生じるようです(例:フィールドグループ名:カスタムフィールドグループ)。

PHP
if(function_exists("register_field_group"))
{
	register_field_group(array (
		'id' => 'acf_%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e3%83%95%e3%82%a3%e3%83%bc%e3%83%ab%e3%83%89%e3%82%b0%e3%83%ab%e3%83%bc%e3%83%97',
		'title' => 'カスタムフィールドグループ',
		'fields' => array (
			array (
				'key' => 'field_53041a7b5fc42',
				'label' => '入力欄',
				'name' => 'text_input',
				'type' => 'text',
				'default_value' => '',
				'placeholder' => '',
				'prepend' => '',
				'append' => '',
				'formatting' => 'html',
				'maxlength' => '',
			),
		),
		'location' => array (
			array (
				array (
					'param' => 'post_type',
					'operator' => '==',
					'value' => 'post',
					'order_no' => 0,
					'group_no' => 0,
				),
			),
		),
		'options' => array (
			'position' => 'normal',
			'layout' => 'no_box',
			'hide_on_screen' => array (
			),
		),
		'menu_order' => 0,
	));
}

これを、Custom Fields Groupと書き換えてやればエラーは止まりました。

PHP
if(function_exists("register_field_group"))
{
	register_field_group(array (
		'id' => 'acf_custom-fields-group',
		'title' => 'Custom Fields Group',
		'fields' => array (
			array (
				'key' => 'field_53041a7b5fc42',
				'label' => '入力欄',
				'name' => 'text_input',
				'type' => 'text',
				'default_value' => '',
				'placeholder' => '',
				'prepend' => '',
				'append' => '',
				'formatting' => 'html',
				'maxlength' => '',
			),
		),
		'location' => array (
			array (
				array (
					'param' => 'post_type',
					'operator' => '==',
					'value' => 'post',
					'order_no' => 0,
					'group_no' => 0,
				),
			),
		),
		'options' => array (
			'position' => 'normal',
			'layout' => 'no_box',
			'hide_on_screen' => array (
			),
		),
		'menu_order' => 0,
	));
}

既に日本語のままで使用している場合は、「wp_usermeta」のテーブルに保存されているので、ここを書き換えてやれば良いのではなかろうかと思います(ちゃんと確認はしていませんので要注意です)。

物草 灸太郎
物草 灸太郎

WordPressでホームページを制作しつつ、休日は畑を耕したりDIYを楽しんでいます。

Loading...

コメント

yamada さん
記事に残して頂きありがとうございます。 助かりました。
2018年7月3日 9:39 PM 返信

コメントをどうぞ

  • メールアドレスが公開されることはありません。
  • コメント欄にURLは入力できません。
  • このサイトはreCAPTCHAによって保護されており、Googleのプライバシーポリシー利用規約が適用されます。