Created
February 22, 2022 08:45
-
-
Save gtamborero/70bf1d11db35407e015fa543020f9508 to your computer and use it in GitHub Desktop.
ACF Custom Data Export (Advanced Custom Fields - WordPress)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The plugin bootstrap file | |
* | |
* @link iproject.cat | |
* @since 1.0.0 | |
* @package Export ACF fields to Excel | |
* | |
* @wordpress-plugin | |
* Plugin Name: ACF - Export Data | |
* Plugin URI: https://iproject.cat | |
* Description: Export user ACF fields to Excel | |
* Version: 1.0.0 | |
* Author: Iproject.cat | |
* Author URI: iproject.cat | |
* License: GPL-2.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
* Text Domain: acf-export | |
* Domain Path: /languages | |
*/ | |
// INSTALLATION: | |
// Create inside /plugins/ a folder named /acf-export | |
// Paste inside /acf-export this acf-export.php file | |
// We use composer to load the xlsx library. | |
// On your command line inside the /acf-export folder, write an run: composer require shuchkin/simplexlsxgen | |
// Now adjust the plugin to your needs with your custom data fields | |
// If you want to export posts instead of users just remove the 'user_' from the get_field: | |
// Example: get_field($field, $GLOBALS['postID'] ); | |
// There's no options on this plugin, just use this export URL when you want to export: | |
// yourdomain/wp-content/plugins/acf-export/acf-export.php?acfExport | |
if(isset($_GET["acfExport"])){ | |
// LOAD WORDPRESS info | |
require_once("../../../wp-load.php"); | |
// https://github.com/shuchkin/simplexlsxgen | |
require_once 'vendor/autoload.php'; | |
function export(){ | |
// GET ALL USERS | |
$getUsers = get_users(); | |
// THIS IS AN EXPORT EXAMPLE. | |
// You have to rewrite it using your desired field names: | |
$users = [ | |
[ | |
'<b>Name</b>', | |
'<b>Identifier</b>', | |
'<b>Movil</b>', | |
'<b>Phone</b>', | |
] | |
]; | |
//LOOP USERS AND SEARCH FOR FIELDS | |
foreach ($getUsers as $user){ | |
$GLOBALS['userID'] = $user->ID; | |
array_push($users, | |
[ | |
$user->first_name . " " . $user->last_name, | |
acfField('id'), | |
'<right>' . acfField('mobile') . '</right>', | |
'<left>' . acfField('phone') . '</left>', | |
acfField('country'), | |
] | |
); | |
} | |
$xlsx = SimpleXLSXGen::fromArray( $users ); | |
$fileName = "users_" . date('d-m-Y') . ".xlsx"; | |
$xlsx->downloadAs($fileName); // or saveAs('books.xlsx') or $xlsx_content = (string) $xlsx | |
} | |
function acfField($field){ | |
return get_field($field, 'user_' . $GLOBALS['userID'] ); | |
} | |
export(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment