Creating manual metaboxes is a difficult and time consuming task. And what about when you need to create these metaboxes (custom fields) automatically, when we activate the theme or plugin, this will be useful when developing custom themes.
ACF (Advanced Custom Fields) plugin which located in the WordPress repository, so there will be no problems with its connection.
Let’s connect a script TGM Plugin Activation:
2. Now connect the script to ours function.php file:
require_once get_template_directory() . '/inc/class-tgm-plugin-activation.php'; add_action( 'tgmpa_register', 'clean_register_required_plugins' ); function clean_register_required_plugins() { /* * Array of plugin arrays. Required keys are name and slug. * If the source is NOT from the .org repo, then source is also required. */ $plugins = array( // This is an example of how to include a plugin from the WordPress Plugin Repository. array( 'name' => 'Advanced Custom Fields', 'slug' => 'advanced-custom-fields', 'required' => true, ), ); $config = array( 'id' => 'clean', // Unique ID for hashing notices for multiple instances of TGMPA. 'default_path' => '', // Default absolute path to bundled plugins. 'menu' => 'tgmpa-install-plugins', // Menu slug. 'parent_slug' => 'themes.php', // Parent menu slug. 'capability' => 'edit_theme_options', // Capability needed to view plugin install page, should be a capability associated with the parent menu used. 'has_notices' => true, // Show admin notices or not. 'dismissable' => true, // If false, a user cannot dismiss the nag message. 'dismiss_msg' => '', // If 'dismissable' is false, this message will be output at top of nag. 'is_automatic' => false, // Automatically activate plugins after installation or not. 'message' => '', // Message to output right before the plugins table. ); tgmpa( $plugins, $config ); }
3. Next, create a file acf.php and place it in the directory /wp-content/themes/yourthemename/inc/.
4. Let’s connect it to ours function.php, by placing the code below:
require get_template_directory() . '/inc/acf.php';
5. Now you can open our acf.php file and add some meta boxes.
function mythemeacfmetaboxes() { acf_add_local_field_group(array( 'key' => 'acf_mysettings', 'title' => 'New Settings from ACF', 'fields' => array( array( 'key' => 'acf_custom_text', 'label' => 'Custom Text', 'name' => 'acf_custom_text', 'type' => 'text', ), array( 'key' => 'acf_custom_select', 'label' => 'Custom Select', 'name' => 'acf_custom_select', 'type' => 'select', 'choices' => array( 'first' => 'First', 'second' => 'Second', 'third' => 'Third' ) 'allow_null' => 1, ) ), 'location' = array( array( array( 'param' => 'post_type', 'operator' => '==', 'value' => 'post', ) ) ), 'menu_order' => 0, 'position' => 'normal', 'style' => 'default', 'label_placement' => '', 'instruction_placement' => 'label', 'hide_one_screen' => array(), )); } add_action( 'acf/init', 'mythemeacfmetaboxes')
Explanations of these fields are available in the official documentation for the plugin