I’ve checked the documentation but I didn’t see any examples or topics talking about programatically creating fields or field group for a specific page template. (I see only just an example to use the plugin admin UI to create a group field)
Is it possible to do it by coding?
May I ask, why do you need this / what are you trying to accomplish that you can’t use the admin UI?
Basically the plugin uses wordpress custom fields functionality (https://codex.wordpress.org/Custom_Fields) and if you wanted to manually create similar custom fields with your own metaboxes, you can find tutorials for that through google. I think you would be better off creating your own custom fields than trying to programatically add news ones through ACF system passing it’s UI. I’m sure if you start digging into the source code of ACF, you can find out how it builds the fields etc and from there you could call the functions etc. yourself…
https://codex.wordpress.org/Function_Reference/add_meta_box this should get you started on creating your own fields…
Let me explain. I’m creating a theme for sell and i create a custom page template.
When the buyer creates a page with my custom template, i would like to automatically show custom options specific for that template. That’s why i want to register option fields programmatically.
Yes, you are right. I can wordpress api to do it but i just think that this plugin may create an on-top api which i can use easily with less coding. I see this plugin provides api for developers for only getting values. It’s a bit strange (only to me 😉 ) for not having api to create fields
So i just come to ask. ;P
I don’t see why that doesn’t happen anyway if you pre-define them for your client? You can restrict the users role from accessing the Custom Fields in the admin?
Oh but now I see if you are selling a theme you want it to have predefined metaboxes for templates already in place when the customer installs the theme themselves.
Hmm I still think it’s easier for you build that not using ACF and approach this with the idea of adding custom metaboxes. As ACF is clearly meant for UI use I think this approach is the right for you. The other metabox solutions in the following link approach this programmatically and not the user friendly UI way.
This should get you going really well as you seem to be able to figure things out:
I was looking for this too. In the end I discovered on the ACF → Export page:
Export to PHP
ACF will create the PHP code to include in your theme.
Registered field groups will not appear in the list of editable field groups. This is useful for including fields in themes.
Please note that if you export and register field groups within the same WP, you will see duplicate fields on your edit screens. To fix this, please move the original field group to the trash or remove the code from your functions.php file.
Select field group(s) from the list and click “Create PHP”
Copy the PHP code generated
Paste into your functions.php file
To activate any Add-ons, edit and use the code in the first few lines.
Hope that helps!
The topic ‘How to programatically create meta fields for custom page template?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!