Skip to content

Instantly share code, notes, and snippets.

@ollietreend
Last active August 28, 2025 05:42
Show Gist options
  • Save ollietreend/df32c5cbe2914f6fc407332bf6cbfca5 to your computer and use it in GitHub Desktop.
Save ollietreend/df32c5cbe2914f6fc407332bf6cbfca5 to your computer and use it in GitHub Desktop.

Revisions

  1. ollietreend revised this gist Nov 17, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion acf-php-to-json.php
    Original file line number Diff line number Diff line change
    @@ -92,6 +92,6 @@ function get_groups_to_convert() {
    * @return bool
    */
    function convert_group($group) {
    $group['fields'] = acf_get_local_fields($group['key']);
    $group['fields'] = acf_get_fields($group['key']);
    return acf_write_json_field_group($group);
    }
  2. ollietreend revised this gist May 10, 2017. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions acf-php-to-json.php
    Original file line number Diff line number Diff line change
    @@ -60,10 +60,7 @@ function admin_page_convert() {

    echo '<ol>';
    foreach ($groups as $group) {
    $group['fields'] = acf_get_local_fields($group['key']);
    $converted = acf_write_json_field_group($group);

    if ($converted) {
    if (convert_group($group)) {
    echo sprintf('<li>Converted: <strong>%s</strong> (%s)</li>', $group['title'], $group['key']);
    }
    else {
  3. ollietreend created this gist May 9, 2017.
    100 changes: 100 additions & 0 deletions acf-php-to-json.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    <?php
    /**
    * Plugin Name: Convert ACF PHP to JSON
    * Description: Convert Advanced Custom Fields Pro configuration from PHP to JSON.
    */

    namespace ConvertAcfPhpToJson;

    /**
    * Add submenu item under 'Custom Fields'
    */
    function admin_menu() {
    add_submenu_page('edit.php?post_type=acf-field-group', 'Convert PHP fields to JSON', 'PHP to JSON', 'manage_options', 'acf-php-to-json', __NAMESPACE__ . '\\admin_page');
    }
    add_action('admin_menu', __NAMESPACE__ . '\\admin_menu', 20);

    /**
    * Output the admin page
    */
    function admin_page() {
    ?>
    <div class="wrap">
    <h1>Convert PHP fields to JSON</h1>
    <?php

    if (!isset($_GET['continue']) || $_GET['continue'] !== 'true') {
    admin_page_intro();
    }
    else {
    admin_page_convert();
    }
    ?>
    </div>
    <?php
    }

    /**
    * Output the introductory page
    */
    function admin_page_intro() {
    $groups = get_groups_to_convert();

    if (empty($groups)) {
    echo '<p>No PHP field group configuration found. Nothing to convert.</p>';
    return;
    }
    else {
    echo sprintf('<p>%d field groups will be converted from PHP to JSON configuration.</p>', count($groups));
    echo '<a href="edit.php?post_type=acf-field-group&page=acf-php-to-json&continue=true" class="button button-primary">Convert Field Groups</a>';
    }
    }

    /**
    * Convert the field groups and output the conversion page
    */
    function admin_page_convert() {
    $groups = get_groups_to_convert();

    echo sprintf('<p>Converting %d field groups from PHP to JSON configuration...</p>', count($groups));

    echo '<ol>';
    foreach ($groups as $group) {
    $group['fields'] = acf_get_local_fields($group['key']);
    $converted = acf_write_json_field_group($group);

    if ($converted) {
    echo sprintf('<li>Converted: <strong>%s</strong> (%s)</li>', $group['title'], $group['key']);
    }
    else {
    echo sprintf('<li><strong>Failed to convert: %s</strong> (%s)</li>', $group['title'], $group['key']);
    }
    }
    echo '</ol>';

    echo '<p>Done. Now remove the PHP field group configuration.</p>';
    }

    /**
    * Get the PHP field groups which will be converted.
    *
    * @return array
    */
    function get_groups_to_convert() {
    $groups = acf_get_local_field_groups();
    if (!$groups) return [];
    return array_filter($groups, function($group) {
    return $group['local'] == 'php';
    });
    }

    /**
    * Convert a field group to JSON
    *
    * @param array $group
    * @return bool
    */
    function convert_group($group) {
    $group['fields'] = acf_get_local_fields($group['key']);
    return acf_write_json_field_group($group);
    }