Support

Account

Home Forums Feature Requests Update_field to automatically use field key

Solving

Update_field to automatically use field key

  • Hello Elliot,

    First off, thank you very much for a fantastic product! I really hope you’ll keep developing it for many years to come.

    The issue
    Currently all fields has a field name as well as a field key. Although the field key is well documented, it’s obvious that many use the field name – myself included – instead of the field key. The field key is too obfuscated in my oppinion, so it makes the code harder to read. But it is the only option, if content is created from PHP instead of from WP-Admin.

    My wish
    Make update_field() automatically save to the correct field key, so we don’t have to bother with it and can rely on field name instead.

    Otherwise, a new function could be added and which works in the same way as update field, but which does the correct field key conversion underneath (insert_field perhaps?).

    Have a nice day!

  • I cant be sure but isn’t the reason why elliot uses a random field key string to avoid issues where the user has entered the same field name for multiple fields.. I don’t think there’s a check for duplicate field names when the user creates his fields and by using a random string instead ACF wont make the two clash.

    This is just my speculations tho 🙂

  • I’m quite sure that there’s a good reason for using the random string. Nonetheless, in a perfect world, such necessary work-arounds would be shielded fully away. By browsing old threads, it does seem that I’m not the only one who has found this extra hidden field a bit annoying to work with.

    I hope one day, the API will work consistently, even if you choose to use the field name and create posts from code and not through WP Admin 🙂

  • I agree of course.. from Elliots point of view that would probably mean that he’d have to make some sort of ajax validation check when the user saves a fieldgroup.. not impossible but tbh, I’d rather he focus on other areas of improvement since the use of the API and PHP is far less used than the admin interface and it still works as is.

    Perhaps something to develop on an github fork?

  • Here’s a function which grabs the field key from registered field groups given a name (note it returns the first key found, so it won’t work with multiple fields with the same name):

    
    function get_acf_field_key($name) {
        if( !empty($GLOBALS['acf_register_field_group']) ) {
            foreach( $GLOBALS['acf_register_field_group'] as $acf ) {
                foreach($acf['fields'] as $field) :
                    if ( $name === $field['name'] ) {
                        return $field['key'];
                    }
                endforeach;
            }
        }
    }
    
  • Nice one Wells!

  • Yes indeed, not a bad solution!

    In my personal case, I’ve decided to work around the ugly field keys by defining consts for all fields, grouped in classes which matches my post types.

    Basically, I have created classes with consts for each field. As I code in Netbeans, my solution has the added benefit of auto-completion for the ACF-fields. I find the added coding overhead is quite small with my solution, although there obviously is an overhead.

    class CAR {
    	// Car - Base information
    	const REGISTRATION_NUMBER = "field_51a66ab4a253c";
    	const MAKE = "field_51a66a92a2539";
    	const MODEL = "field_51a66a9aa253a";
    	const MODEL_YEAR = "field_51a66a9fa253b";
    	
    	// Car - Geographical details
    	const ADDRESS = "field_51a66dc064cbb";
    	const POSTAL = "field_51a66dcd64cbd";
    	const CITY = "field_51a66dc664cbc";
    	const COUNTRY = "field_51a66dd664cbe";
    	const GEOLOCATION = "field_51e1d1afb965c";
    
    // ... more fields ...
    }
    
    class USER {
    	// User
    	const AVATAR_URL = "field_51e306ce37798";
    	const FACEBOOK_USER_ID = "field_51e3070737799";
    	const NEWSLETTER = "field_51e47ad26a7a6";
    }
    

    So when I want to update or get a field, I now do:

    update_field(CAR::REGISTRATION_NUMBER, $registration_number, $postID);
    

    It works quite well for me. Nonetheless, I stand by the feature request. I would have preferred simply having to write the following and not worry about extra classes and consts:

    update_field('registration_number', $registration_number, $postID);
    
  • @mortenskyt

    I’m liking your use of classes for fields, which got me thinking about an abstracted static class for performing similar look-ups & related tasks.

    Using the function posted above, however, you could eliminate the required cut-and-paste:

     update_field( get_acf_field_key('registration_number'), $registration_number, $postID);
    

    @elliot – any interest in including something like this in core? Any suggestions?

    • Elliot

    • July 24, 2013 at 2:54 pm

    Hi @wells5609

    I like it, but this will only work for PHP registered fields….

    I’m not sure what the solution is for this one..

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.