add acf filed to admin_menu programmatically - php

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

Related

Advanced Custom Fields plugin User dropdown is not working

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.

How to programmatically add an ACF group to the backend of Wordpress?

I've tried a bunch of different functions and approaches but so far I haven't been able to get it working.
The goal is to add an Advanced Custom Field group to the backend of Wordpress with some PHP-code. In the best scenario we add the PHP-code to a method of a class.
public function create_group( $group_name ) {
if ( $this->does_group_already_exists( $group_name ) ) {
return false;
}
acf_add_local_field_group( array(
'key' => 'group_1',
'title' => 'My Group',
'fields' => array(
array(
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => 'text',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
) );
return true;
}
Nothing gets added with the code above. I also tried adding it to functions.php and it with a add_action() function like so:
add_action( 'acf/init', array( $this, 'create_group' ) );
But again, no results.
Hope some one can share a working solution.
Today I finally discovered a solution for adding a ACF group to the backend dynamically with PHP-code.
It can be done by adding a new post directly with the acf-field-group post type. Here is my implementation for those awesome people from the future that are interested:
public function create_form( $form_name ) {
$new_post = array(
'post_title' => $form_name,
'post_excerpt' => sanitize_title( $form_name ),
'post_name' => 'group_' . uniqid(),
'post_date' => date( 'Y-m-d H:i:s' ),
'comment_status' => 'closed',
'post_status' => 'publish',
'post_type' => 'acf-field-group',
);
$post_id = wp_insert_post( $new_post );
return $post_id;
}
Where $form_name is the name of the ACF group. It works. And there was no need for using a specific hook. I could just call this method directly.
Actually you can create such code over ACF in the WP-Backend itself (not sure if this only works in ACF Pro). Under Admin -> Custom Fields -> Tools -> Export -> Create PHP. The generated code is a great starting point for a programmatic ACF integration.
It should look something like this:
acf_add_local_field_group(array(
'key' => 'group_5d146d18eeb92',
'title' => 'My Group Title',
'fields' => array(
array(
'key' => 'field_5d146d1f27577',
'label' => 'My Field Title',
'name' => 'my_field_name',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => '',
'ui_off_text' => '',
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'my_custom_post_type',
),
),
),
'menu_order' => 0,
'position' => 'side',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
Check out the ACF page for registering fields via PHP.
Action acf/init is available for pro version only, maybe that was the reason it did not work at the first place..
For basic version you have to use acf/register_fields to register your custom fields.

WordPress: How to use active callbacks in the Customizer

In my theme, I set up 2 options use Theme Customization API, the code snippet below.
I want to display the radio option when the checkbox is true, when the checkbox is false, the radio hidden. I try to use active_callbackļ¼Œbut not working. So, How can achieve this function?
Thanks!
// Related Post.
$wp_customize->add_setting('_related_post', array(
'capability' => 'edit_theme_options',
'default' => 0,
'transport' => 'postMessage',
));
$wp_customize->add_control('_related_post', array(
'settings' => '_related_post',
'label' => __('Display Related Posts', 'typenow'),
'section' => '_theme_options',
'type' => 'checkbox',
'priority' => 30,
));
// Related Post Num.
$wp_customize->add_setting('_related_post_num', array(
'capability' => 'edit_theme_options',
'default' => '2',
'transport' => 'postMessage',
));
$wp_customize->add_control('_related_post_num', array(
'settings' => '_related_post_num',
'label' => __('Related Posts Number', 'typenow'),
'section' => '_theme_options',
'type' => 'radio',
'priority' => 35,
'choices' => array (
'2' => __('Two posts', 'typenow'),
'4' => __('Four posts', 'typenow'),
),
));
The solution:
$wp_customize->add_control('_related_post_num', array(
'settings' => '_related_post_num',
'label' => __('Related Posts Number', 'typenow'),
'section' => '_theme_options',
'type' => 'radio',
'priority' => 35,
'choices' => array (
'2' => __('Two posts', 'typenow'),
'4' => __('Four posts', 'typenow'),
),
'active_callback' => function(){
return get_theme_mod( '_related_post', false );
},
));

How to add acf form post categry field in acf form front end but category dropdown not showing

Category dropdown not showing on front end.Please suggest or let me know where i am wrong in my code.
<?php acf_form(array(
'post_id' => 'new_post',
'post_title' => true,
'post_content' => true,
'post_category' => true,
'field_groups' => array('group_57d9928ba5858'),
'new_post' => array(
'post_type' => 'festival',
'post_status' => 'draft'
),
'submit_value' => 'Submit Post',
'updated_message' => 'Saved!',
'uploader' => 'wp',
));?>
I can't see post_category as an option in https://www.advancedcustomfields.com/resources/acf_form/ I think it should be in
'new_post' => array(
'post_type' => 'foo',
'post_status' => 'publish',
'post_category' => array ('bar')
),
ACF doesn't automatically add the category option to front-end forms. The best solution is to setup a custom field referencing the category taxonomy (Taxonomy Field Type) and setting the Add Term, Save Term & Load Term values to true so the field acts like a native category field.
Alternatively, you can add the following code to your functions file that was generated from the ACF Generate PHP Tool for a field I set up with these settings.
if( function_exists('acf_add_local_field_group') ) {
acf_add_local_field_group(array(
'key' => 'group_5dc382838casdf',
'title' => 'Category',
'fields' => array(
array(
'key' => 'field_5ddcdd3232asD',
'label' => 'Category',
'name' => 'category',
'type' => 'taxonomy',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'taxonomy' => 'category',
// Accepts SINGLE VALUE: radio, select, MULTIPLE VALUES: multi_select, checkbox
'field_type' => 'multi-select',
'allow_null' => 0,
// Ensures taxonomy relationships are set on add, save, load
'add_term' => 1,
'save_terms' => 1,
'load_terms' => 1,
'return_format' => 'id',
'multiple' => 0,
// Form Placeholder
'placeholder' => 'Select Topics'
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
// ADD YOUR POST-TYPE HERE
'value' => 'post',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
}
This is working...
function acf_xyz_categories( $value, $post_id, $field ){
if($value != ''){
$valueint = array_map('intval', $value);
$set_taxonomy_ids = wp_set_object_terms( $post_id, $valueint, 'your_taxonomy_name', true );
}
return $value;
}
add_filter('acf/update_value/name=abc_category', 'acf_xyz_categories', 10, 3);

List posts in the wordpress theme customiser

Hey there. I am creating a Wordpress theme and was wondering if anyone knew of a way to list one of my custom post types in the theme customiser. I have a custom post type called "slideshow" that has custom meta boxes etc and is designed just for slideshows. I would like to be able to list these posts in a dropdown inside the customiser. Ideally ending up with them in the array like this...
'the_id' => 'Slideshow post title',
$wp_customize->add_setting(
'slideshow-homepage',
array(
'default' => 'none',
)
);
$wp_customize->add_control(
'slideshow-homepage',
array(
'type' => 'select',
'priority' => 3,
'label' => 'Slideshow',
'description' => '',
'section' => 'homepage',
'choices' => array(
'somehow' => 'somehow',
'list' => 'list',
'all' => 'all',
'custom' => 'custom',
'post' => 'post',
'types' => 'types',
'of' => 'of',
'type' => 'type',
'slideshow' => 'slideshow'
),
)
);
Many thanks guys and girls. Lewis
use array_reduce
$wp_customize->add_control(
'slideshow-homepage',
array(
'type' => 'select',
'priority' => 3,
'label' => 'Slideshow',
'description' => '',
'section' => 'homepage',
'choices' => array_reduce(
get_posts( 'post_type=slideshow&posts_per_page=-1' ),
function( $result, $item ) {
$result[$item->ID] = $item->post_title;
return $result;
}
),
)
);
To add an empty field as well:
$posts = array_reduce(
get_posts( 'post_type=slideshow&posts_per_page=-1' ),
function( $result, $item ) {
$result[$item->ID] = $item->post_title;
return $result;
}
);
$none = array('' => 'None');
$choices = $none + $posts;
$wp_customize->add_control('slideshow_control', array(
'label' => __('Choose Slideshow', 'themename'),
'section' => 'slideshow_option',
'settings' => 'slideshow_settings',
'type' => 'select',
'choices' => $choices
));

Categories