Skip to content

Instantly share code, notes, and snippets.

@isotrope
Last active March 18, 2020 19:03
Show Gist options
  • Save isotrope/f1565df4f69f06714c03504b9389b7be to your computer and use it in GitHub Desktop.
Save isotrope/f1565df4f69f06714c03504b9389b7be to your computer and use it in GitHub Desktop.
Smalll script to quickly gather ACF field names
/*
* Stick me in your console and run
*/
(function ( $ ) {
var fields = '',
php = '',
letAcfFormatTheValues = true,
showTextAsComments = true,
addPhpPostId = true;
$( '.li-field-name' ).each( function ( i, el ) {
var $el = $( el ),
text = $el.text().trim();
// Skip the empty lines or fields like Tabs
if ( text === '' || $el.parents( '.acf-thead' ).length > 0 ) {
return true;
}
var deep = $el.parents( '.acf-field-setting-sub_fields' ).length,
isSubfield = deep > 0,
tabs = isSubfield ? ("\t").repeat( deep ) : '',
comment = showTextAsComments ? '* ' : '';
fields += comment + tabs + text + "\n";
if ( !isSubfield ) {
php += '$' + text + ' = get_post_meta( $post_id, \'' + text + '\', true );' + "\n";
} else {
php += tabs + '$' + text + ' = get_sub_field( \'' + text + '\', ' + letAcfFormatTheValues + ' );' + "\n";
}
} );
// text
console.log( '%c Text Names ', 'background: #222; color: #bada55' );
var commentsOpen = showTextAsComments ? '/*' + "\n" + '*' + "\n" : '',
commentsClose = '*' + "\n" + '*/' + "\n";
console.log( commentsOpen + fields + commentsClose );
// PHP
var postId = addPhpPostId ? '$post_id = get_the_ID();' + "\n\n" : '';
console.log( '%c PHP ', 'background: #222; color: #bada55' );
console.log( postId + php );
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment