如何以编程方式将 ACF 组添加到 Wordpress 的后端?
How to programmatically add an ACF group to the backend of Wordpress?
我已经尝试了很多不同的功能和方法,但到目前为止我还没有能够让它发挥作用。
目标是使用一些 PHP 代码将高级自定义字段组添加到 Wordpress 的后端。在最好的情况下,我们将 PHP 代码添加到 class.
的方法中
public function create_group( $group_name ) {
if ( $this->does_group_already_exists( $group_name ) ) {
return false;
}
acf_add_local_field_group( array(
'key' => 'group_1',
'title' => 'My Group',
'fields' => array(
array(
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => 'text',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
) );
return true;
}
上面的代码没有添加任何内容。我还尝试将它添加到 functions.php
并使用 add_action()
函数,如下所示:
add_action( 'acf/init', array( $this, 'create_group' ) );
但是还是没有结果。
希望有人能分享一个可行的解决方案。
实际上,您可以在 WP-Backend 本身中通过 ACF 创建此类代码(不确定这是否仅适用于 ACF Pro)。在管理 -> 自定义字段 -> 工具 -> 导出 -> 创建 PHP。生成的代码是编程 ACF 集成的一个很好的起点。
它应该看起来像这样:
acf_add_local_field_group(array(
'key' => 'group_5d146d18eeb92',
'title' => 'My Group Title',
'fields' => array(
array(
'key' => 'field_5d146d1f27577',
'label' => 'My Field Title',
'name' => 'my_field_name',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => '',
'ui_off_text' => '',
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'my_custom_post_type',
),
),
),
'menu_order' => 0,
'position' => 'side',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
查看 registering fields via PHP 的 ACF 页面。
今天我终于找到了一个用PHP代码动态添加ACF组到后端的解决方案。
直接用acf-field-group
post类型添加一个新的post即可。以下是我为那些感兴趣的未来优秀人士所做的实施:
public function create_form( $form_name ) {
$new_post = array(
'post_title' => $form_name,
'post_excerpt' => sanitize_title( $form_name ),
'post_name' => 'group_' . uniqid(),
'post_date' => date( 'Y-m-d H:i:s' ),
'comment_status' => 'closed',
'post_status' => 'publish',
'post_type' => 'acf-field-group',
);
$post_id = wp_insert_post( $new_post );
return $post_id;
}
其中 $form_name
是 ACF 组的名称。有用。并且不需要使用特定的钩子。我可以直接调用这个方法。
操作 acf/init
仅适用于专业版,也许这就是它一开始不起作用的原因..
对于基本版本,您必须使用 acf/register_fields
来注册您的自定义字段。
我已经尝试了很多不同的功能和方法,但到目前为止我还没有能够让它发挥作用。 目标是使用一些 PHP 代码将高级自定义字段组添加到 Wordpress 的后端。在最好的情况下,我们将 PHP 代码添加到 class.
的方法中public function create_group( $group_name ) {
if ( $this->does_group_already_exists( $group_name ) ) {
return false;
}
acf_add_local_field_group( array(
'key' => 'group_1',
'title' => 'My Group',
'fields' => array(
array(
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => 'text',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
) );
return true;
}
上面的代码没有添加任何内容。我还尝试将它添加到 functions.php
并使用 add_action()
函数,如下所示:
add_action( 'acf/init', array( $this, 'create_group' ) );
但是还是没有结果。
希望有人能分享一个可行的解决方案。
实际上,您可以在 WP-Backend 本身中通过 ACF 创建此类代码(不确定这是否仅适用于 ACF Pro)。在管理 -> 自定义字段 -> 工具 -> 导出 -> 创建 PHP。生成的代码是编程 ACF 集成的一个很好的起点。
它应该看起来像这样:
acf_add_local_field_group(array(
'key' => 'group_5d146d18eeb92',
'title' => 'My Group Title',
'fields' => array(
array(
'key' => 'field_5d146d1f27577',
'label' => 'My Field Title',
'name' => 'my_field_name',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => '',
'ui_off_text' => '',
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'my_custom_post_type',
),
),
),
'menu_order' => 0,
'position' => 'side',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
查看 registering fields via PHP 的 ACF 页面。
今天我终于找到了一个用PHP代码动态添加ACF组到后端的解决方案。
直接用acf-field-group
post类型添加一个新的post即可。以下是我为那些感兴趣的未来优秀人士所做的实施:
public function create_form( $form_name ) {
$new_post = array(
'post_title' => $form_name,
'post_excerpt' => sanitize_title( $form_name ),
'post_name' => 'group_' . uniqid(),
'post_date' => date( 'Y-m-d H:i:s' ),
'comment_status' => 'closed',
'post_status' => 'publish',
'post_type' => 'acf-field-group',
);
$post_id = wp_insert_post( $new_post );
return $post_id;
}
其中 $form_name
是 ACF 组的名称。有用。并且不需要使用特定的钩子。我可以直接调用这个方法。
操作 acf/init
仅适用于专业版,也许这就是它一开始不起作用的原因..
对于基本版本,您必须使用 acf/register_fields
来注册您的自定义字段。