I am using the ACF plugin in one of the WordPress websites and I have the following configuration for a user dropdown. This is added as a PHP code.
acf_add_local_field_group(array (
'key' => 'group_557ad1ef2b8a1',
'title' => 'Authors',
'fields' => array (
array (
'key' => 'field_557ad24ba99e5',
'label' => 'Featured',
'name' => 'featured_authors',
'type' => 'repeater',
'instructions' => 'Choose a maximum of 6 authors',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'min' => '',
'max' => 6,
'layout' => 'table',
'button_label' => 'Add Author',
'sub_fields' => array (
array (
'key' => 'field_5582bc82f8419',
'label' => 'Author',
'name' => 'featured_authors_author',
'type' => 'user',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'role' => '',
'allow_null' => 0,
'multiple' => 0,
),
),
)
'menu_order' => 0,
'position' => 'normal',
'style' => 'seamless',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => array (
0 => 'the_content',
1 => 'author',
2 => 'featured_image',
),
));
Now from the admin section, I am trying to add this field to the page, but it is not permitting the admin to select users. The dropdown is showing without any values. I am using the following versions:
Wordpress - 5.7.3
Advanced Custom Fields - 5.4.5
You have a syntax error in your code. You didn't close the first array() inside the 'fields' parameter in your main list of fields. Before this line:
'menu_order' => 0,
you need to add this:
),
This will close the 'fields' parameter, and allow it to process the parameters that appear after it.
I ended up recreating your field group in the ACF User Interface in the WP Admin, then exported the php, stripped out all the unnecessary/default parameters, and it's working great for me on a test environment. See the code below. I stripped out all of the settings that were just set to the default settings, to help make things a bit more readable.
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_6148d6a5d8172',
'title' => 'Authors',
'fields' => array(
array(
'key' => 'field_6148d6afde7ec',
'label' => 'Featured',
'name' => 'featured_authors',
'type' => 'repeater',
'instructions' => 'Choose a maximum of 6 authors',
'max' => 6,
'button_label' => 'Add Author',
'sub_fields' => array(
array(
'key' => 'field_6148d6d5de7ed',
'label' => 'Author',
'name' => 'featured_authors_author',
'type' => 'user',
'required' => 1,
),
),
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
'style' => 'seamless',
'hide_on_screen' => array(
0 => 'the_content',
1 => 'author',
2 => 'featured_image',
),
));
endif;
One thing I noticed is that your code doesn't have any Location settings to dictate what post type or other element the field group should appear on, so I set my example to appear on every Post. You may need to adjust this to match your needs.
I also included the if function_exists conditional wrapper so that if you turn off the ACF plugin, it won't generate errors when it cannot find the acf_add_local_field_group() function.
Related
can we add ACF fields to custom admin_menu page?
so I have created an admin page as shown
add_action( 'admin_menu', 'opt_add_admin_menu' );
function opt_add_admin_menu( ) {
add_menu_page( 'opt', 'opt', 'manage_options', 'opt', 'opt_options_page' );
}
so next I need to add an ACF field to this options page
I got this code from ACF but the location still makes a problem for me
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_6335ef4ab316e',
'title' => 'justfortest',
'fields' => array(
array(
'key' => 'field_6335ef5351c1c',
'label' => 'text',
'name' => 'text',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
endif;
'location' => array(
array(
array(
'param' => 'post_type', /* here we need to target the menu options page */
'operator' => '==',
'value' => 'post', /* here to */
),
),
),
I know how to add fields programmatically without ACF but if there is a way to add through ACF it will be so helpful
Create new admin page using acf option page
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'opt',
'menu_title' => 'opt',
'menu_slug' => 'opt-general-settings',
'capability' => 'edit_posts',
'redirect' => false
));
}
and select option page ->opt while adding new group
--- You need acf pro version to use this feature
After I included ACF into my Plugin fields of type 'user' and 'post_object' don't work any more. They worked perfectly fine before I included ACF. They are displayed but they don't show me any options any more. Any clue what this might have caused? I guess that some link might have changed but I can't find any hint to that in the code:
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_5f65057cd8301',
'title' => 'Employees Details',
'fields' => array(
array(
'key' => 'field_5f8a0c6792e45',
'label' => 'Display Name',
'name' => 'display_name',
'type' => 'user',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'role' => array(
0 => 'employee',
1 => 'administrator',
),
'allow_null' => 0,
'multiple' => 0,
'return_format' => 'array',
),
array(
'key' => 'field_5f65089dbbe1d',
'label' => 'Occupation',
'name' => 'occupation',
'type' => 'post_object',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'post_type' => array(
0 => 'occupations',
),
'taxonomy' => '',
'allow_null' => 0,
'multiple' => 0,
'return_format' => 'object',
'ui' => 1,
),
array(
'key' => 'field_5f8b42ca6c4c6',
'label' => 'Costs per Hour',
'name' => 'costs_per_hour',
'type' => 'number',
'instructions' => 'Enter a number with max 2 decimals, separated by ".", e.g. "35.46"',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'min' => '',
'max' => '',
'step' => '0.01',
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'employees',
),
),
),
'menu_order' => 0,
'position' => 'acf_after_title',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
endif;
In the meantime I found this:
In the console there are errors as these:
Failed to load resource: the server responded .../advanced-custom-fields/assets/css/acf-global.css?ver=5.9.1
ReferenceError:Can't find variable: acf Global Code post-new.php 1972
(actually my post-new.php doesn't even have so many lines)
When I click on the last one the debugger gives me this:
acf.data = {"postboxes":[{...
acf.doAction( 'prepare' )
And when I finally look into the apache_error.log there are errors like:
File does not exist: /Applications/MAMP/htdocs/myPlugin/Applications, referrer: http://localhost:8888/myPlugin/wp-admin/post-new.php?post_type=jobs
I want to separate some part of my arrays so I can make the details dynamic and different in my WordPress website.
I'm using ACF to get my custom post type taxonomy and trying to generate some attributes and make them tabs using the acf_add_local_field_group function but unfortunately am not able to make it work.
The reason why I want to separate the array is because am not able to call global $post; and I have a function which is a meta box for that to retrieve the taxonomy terms.
Here is my code:
function my_acf_add_local_field_groups()
{
acf_add_local_field_group(array(
'key' => 'tab_group_1',
'title' => 'Product Sizes and Prices',
'name' => 'group_sizes_tab',
'fields' => array(
array(
'key' => 'field_tab_size_1',
'label' => 'First Size',
'name' => 'store_sizes_',
'type' => 'tab',
'parent' => 'tab_group_1',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
) ,
'collapsed' => '',
'min' => '',
'max' => '',
) ,
array(
'key' => 'field_unique_key',
'label' => 'Simple Repeater',
'name' => 'simple_repeater',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
) ,
'collapsed' => '',
'min' => 0,
'max' => 10,
'layout' => 'table',
'button_label' => 'Add row',
'sub_fields' => array(
array(
'key' => 'field_unique_key_1',
'label' => 'Total Products',
'name' => 'total_products',
'type' => 'text',
) ,
array(
'key' => 'field_unique_key_2',
'label' => 'Total Prices',
'name' => 'total_prices',
'type' => 'text',
) ,
) ,
) ,
array(
'key' => 'field_tab_size_2',
'label' => 'Second Size',
'name' => 'store_sizes_2',
'type' => 'tab',
'parent' => 'tab_group_1',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
) ,
'collapsed' => '',
'min' => '',
'max' => '',
) ,
) ,
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'products',
) ,
) ,
) ,
));
}
add_action('acf/init', 'my_acf_add_local_field_groups');
As you can see in the group_sizes_tab the fields value is the one I want to separate. Am thinking storing it in a variable but when I tested it it doesn't work.
Just to make it short, basically outside the function it'll be like this:
$fields = array(
'key' => 'field_tab_size_1',
'label' => 'First Size',
'name' => 'store_sizes_',
'type' => 'tab',
'parent' => 'tab_group_1',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
) ,
'collapsed' => '',
'min' => '',
'max' => '',
);
Then I replace this value inside the function but it is not working.
How can I wrap the array and replace it with a variable in the function?
I am using wordpress for a News site.
There is place called Headlines with following ACF code. Which adds posts to headline of the website.
array (
'key' => 'field_53e3e2fc67dc4',
'label' => 'Headlines',
'name' => 'hp_headlines',
'prefix' => '',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'min' => '',
'max' => '',
'layout' => 'row',
'button_label' => 'Add Headline',
'sub_fields' => array (
array (
'key' => 'field_54621f720bfdc',
'label' => 'Headline Type',
'name' => 'hp_headline_type',
'prefix' => '',
'type' => 'radio',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array (
'url' => 'URL',
'article' => 'Article',
),
'other_choice' => 0,
'save_other_choice' => 0,
'default_value' => 'url',
'layout' => 'horizontal',
),
array (
'key' => 'field_54621fa20bfdd',
'label' => 'URL',
'name' => 'hp_headline_url',
'prefix' => '',
'type' => 'url',
'instructions' => '',
'required' => 1,
'conditional_logic' => array (
array (
array (
'field' => 'field_54621f720bfdc',
'operator' => '==',
'value' => 'url',
),
),
),
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => 'http://',
),
array (
'key' => 'field_53e3e34067dc5',
'label' => 'Article',
'name' => 'hp_headline_article',
'prefix' => '',
'type' => 'post_object',
'instructions' => '',
'required' => 1,
'conditional_logic' => array (
array (
array (
'field' => 'field_54621f720bfdc',
'operator' => '==',
'value' => 'article',
),
),
),
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'post_type' => array (
0 => 'post',
),
'taxonomy' => '',
'allow_null' => 0,
'multiple' => 0,
'return_format' => 'id',
'ui' => 1,
),
),
Which creates a field to add already added posts to a list of headlines.
Now I want these fields to be added to category "xyz" automatically as I press the update button. And I have no Idea which file to edit.
You need use save_post action, add this code in your functions.php change cat ID wit your cat id's
function set_my_categories($post_ID){
if(wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) {
return $post_ID;
}
wp_set_post_categories( $post_ID, array(49,13) );
}
add_action('save_post', 'set_my_categories');
I'm using the Advanced Custom Field (ACF) Plugin for my Wordpress blogs. Each blog has the same general heading/logo to the page.
Immigration Law
Health Law
The two elements that generally change are the standard colors associated with the blog's name and the name of the blog (i.e. Health vs Immigration).
I've added a few ACF fields into a new group (blog_type, blog_name, blog_color) and inserted them into the header.php file where I want them. I've also assigned them to the following location rules:
User, is equal to, Administrator (and)
Page, is equal to, Home
When I choose Update and proceed to the home page, the new fields should be there but are not.
I did a little digging and found that I needed to add the exported info that it generates to my functions.php page in my child theme. But when I do that and go back to my Group details in the plugin menu, all but one of the fields are gone.
Not sure what I'm doing wrong.
Wordpress version: 4.2.2
Parent Theme: Elegant Themes - Divi (version 2.3.1)
ACF Plugin Version: 4.4.2
EDIT:
Here's the code I added to my header.php
<h1>
knowing<span style="color: <?php the_field('blog_color');?>;"><?php the_field('blog_name');?></span><?php the_field('blog_type');?>
</h1>
This is the code that the export function gave me for my functions.php file:
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_blog-details',
'title' => 'Blog Details',
'fields' => array (
array (
'key' => 'field_55a40afe0bf59',
'label' => 'Blog Color',
'name' => 'blog_color',
'type' => 'color_picker',
'required' => 1,
'default_value' => 'rgb(210,120,40)',
),
array (
'key' => 'field_55a41857b2d0f',
'label' => 'Blog Name',
'name' => 'blog_name',
'type' => 'text',
'required' => 1,
'default_value' => 'Blog',
'placeholder' => 'Blog Name',
'prepend' => '',
'append' => '',
'formatting' => 'html',
'maxlength' => '',
),
array (
'key' => 'field_55a41859b2d10',
'label' => 'Blog Type',
'name' => 'blog_type',
'type' => 'radio',
'instructions' => 'Choose a type of Blog',
'required' => 1,
'choices' => array (
'blog' => 'blog',
'relations' => 'relations',
),
'other_choice' => 0,
'save_other_choice' => 0,
'default_value' => '',
'layout' => 'vertical',
),
),
'location' => array (
array (
array (
'param' => 'ef_user',
'operator' => '==',
'value' => 'administrator',
'order_no' => 0,
'group_no' => 0,
),
array (
'param' => 'page',
'operator' => '==',
'value' => '23',
'order_no' => 1,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array (
0 => 'custom_fields',
),
),
'menu_order' => 0,
));
}
EDIT 2:
Okay I found something strange out, when I removed the User = Admin out from the Location Rules, it showed up. But I only want Admins to see that. Not sure what to do.