0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[WordPress]タームにカスタムフィールド追加

0
Posted at

最新のバージョンは対応したか知らんけど
Advance Custom Fieldsだとwp_termmetaに保存しなくて
まあまあ不便だったので自前でやる。

functions.php
//新規
add_action('{taxonomy}_add_form_fields', function() {
	$format = '<div class="form-field"><label for="%1$s">%2$s</label><input type="text" name="%1$s" id="%1$s" value="" size="40"></div>';
	printf($format, '{slug}', '{label}');
});
add_action('create_term', function ($term_id) {
	if ( array_key_exists('{slug}', $_POST) ) update_term_meta($term_id, '{slug}', esc_attr($_POST['{slug}']));
});

//編集
add_action('{taxonomy}_edit_form_fields', function ($term) {
	$format = '<tr class="form-field"><th><label for="%1$s">%2$s</label></th><td><input type="text" name="%1$s" id="%1$s" value="%3$s" size="40"></td></tr>';
	$value = get_term_meta($term->term_id, '{slug}', true);
	printf($format, '{slug}', '{label}', esc_attr($value));
});
add_action('edit_terms', function ($term_id) {
	if ( array_key_exists('{slug}', $_POST) ) update_term_meta($term_id, '{slug}', esc_attr($_POST['{slug}']));
});

inputのtypeやらsizeやらは適宜変更。
フォーマットはどこかしらから引っ張ってこれそうだけども。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?