在自定义主题或插件中包含 ACF(高级自定义字段):在分发时导出字段

ome*_*ooq 0 wordpress json wordpress-theming advanced-custom-fields

我正在开发一个自定义主题,并在该主题中包含了 acf pro,如文档中所述。主题工作正常,主题激活时激活了 acf。这是代码。

// customize ACF path
add_filter('acf/settings/path', 'my_acf_settings_path');
function my_acf_settings_path( $path ) {
    $path = get_stylesheet_directory() . '/inc/advanced-custom-fields-pro/';
    return $path;
}
// customize ACF dir
add_filter('acf/settings/dir', 'my_acf_settings_dir');
function my_acf_settings_dir( $dir ) {
    $dir = get_stylesheet_directory_uri() . '/inc/advanced-custom-fields-pro/';
    return $dir;
}
// Include ACF
include_once( get_stylesheet_directory() . '/inc/advanced-custom-fields-pro/acf.php' );
Run Code Online (Sandbox Code Playgroud)

我现在面临几个小时的问题是由于我想在 acf 中实例化的组/字段。我有一些想要在全新安装时显示的字段组。以下是我尝试过的方法:

方法 1: 我已将字段作为 json 导出到名为acf-json. ACF 确实识别它并显示为同步字段。但是当我尝试同步它时,它只会创建一个新的空字段。

方法 2: 我也尝试将字段组导出为 php 文件,然后将其包含在我的 functions.php 文件中,但 acf 无法识别此代码。

ome*_*ooq 5

由于文档中缺乏信息,在主题或插件中分发 ACF 有点棘手。棘手的部分是使用您的主题和插件导出您的字段,这样您的用户不必做任何与他们习惯使用任何其他主题或插件不同的事情。我将详细介绍该程序。

对于主题和插件开发:

参考官方文档,将代码复制并粘贴到您的functions.php 文件中进行主题开发应该很容易,而对于插件开发,您可以将其添加到主插件文件中。这将完成这 4 个任务。

  1. 添加 ACF 路径
  2. 添加 ACF 目录
  3. 对客户端隐藏 ACF(如果需要)
  4. 包括 ACF

Up till now what you have done actually doesn't do anything special. It just activates ACF whenever you activate your theme/plugin and similarly deactivates ACF on theme/plugin deactivation.

Exporting Fields: (via JSON sync)

At this stage if you distribute your theme/plugin it will just activate ACF, but it wont have any fields inside it. ACF uses JSON to keep track of all the fields and field groups. By default ACF will look for a folder called acf-json in the root of your theme. If you have this folder then ACF will automatically add/update a new json file for each field group you add or update.

You can change the location of this folder, if you want to keep it inside your includes folder. Somehow you cant change the default location on a theme, but for plugins you can by adding this code.

add_filter('acf/settings/save_json', 'set_acf_json_save_folder');
function set_acf_json_save_folder( $path ) {
    $path = dirname(__FILE__) . '/includes/acf-json';
    return $path;
}
add_filter('acf/settings/load_json', 'add_acf_json_load_folder');
function add_acf_json_load_folder( $paths ) {
    unset($paths[0]);
    $paths[] = dirname(__FILE__) . '/includes/acf-json';
    return $paths;
}
Run Code Online (Sandbox Code Playgroud)

Now if you share this theme/plugin with someone, when they go inside ACF they should see a new option for sync. On syncing all the files fields should be available to them.

Automatting the SYNC Process:

If you want to hide ACF completely then obviously you cant have your users go inside ACF and sync fields. So in this case you need a script that would automatically sync all the fields from the json folder. You can add this code inside your functions.php for themes or inside your main plugin file. You dont have to change any paths in this script because in the previous code you have already told ACF where to load the JSON files from.

add_action( 'admin_init', 'article_gamification_sync_acf_fields' );
function article_gamification_sync_acf_fields() {
    // vars
    $groups = acf_get_field_groups();
    $sync   = array();
    // bail early if no field groups
    if( empty( $groups ) )
        return;
    // find JSON field groups which have not yet been imported
    foreach( $groups as $group ) {

        // vars
        $local      = acf_maybe_get( $group, 'local', false );
        $modified   = acf_maybe_get( $group, 'modified', 0 );
        $private    = acf_maybe_get( $group, 'private', false );
        // ignore DB / PHP / private field groups
        if( $local !== 'json' || $private ) {

            // do nothing

        } elseif( ! $group[ 'ID' ] ) {

            $sync[ $group[ 'key' ] ] = $group;

        } elseif( $modified && $modified > get_post_modified_time( 'U', true, $group[ 'ID' ], true ) ) {

            $sync[ $group[ 'key' ] ]  = $group;
        }
    }
    // bail if no sync needed
    if( empty( $sync ) )
        return;
    if( ! empty( $sync ) ) { //if( ! empty( $keys ) ) {

        // vars
        $new_ids = array();

        foreach( $sync as $key => $v ) { //foreach( $keys as $key ) {

            // append fields
            if( acf_have_local_fields( $key ) ) {

                $sync[ $key ][ 'fields' ] = acf_get_local_fields( $key );

            }
            // import
            $field_group = acf_import_field_group( $sync[ $key ] );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

**现在,当您分发主题/插件时,激活时它也会激活 ACF,然后复制所有 json 文件并执行它们。这将自动同步所有字段组,现在您甚至可以隐藏您的 ACF 插件,并且您的用户都不必进入 ACF 来同步字段,事实上他们甚至不必知道 ACF 存在于他们的站点上。其次,即使您在 ACF 中进行了新的更改,它也应该自动更新对 json 文件的更改。您现在甚至可以对它们进行版本控制以获得更好的控制。**