I'm trying to add fields to an options page through PHP, and I can't get it to work, I've tried nearly everything by now, but it just won't work.
I hope you can help, my php looks like this:
if (function_exists('acf_add_options_page')) {
$option_page = acf_add_options_page(array(
'page_title' => 'Indstillinger',
'menu_title' => 'Indstillinger',
'menu_slug' => 'options',
'capability' => 'edit_posts',
'redirect' => false
));
}
function my_acf_add_local_field_groups() {
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' => 'options',
),
),
),
));
}
add_action('acf/init', 'my_acf_add_local_field_groups');
What am I doing wrong here?
I have found out.
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'theme-general-settings',
),
),
),
Here in the value field just add the "menu slug" that you have registered for the options page. Like 'theme-header-settings', 'theme-general-settings' are you requirement. I hope you understand.
I think you want
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'options',
),
),
),
The way you have added Option page, it seems that you have installed ACF plugin.
You can add any type of field on your option page using ACF plugin as well.
Check my attached screen shot, You need to set following condition for the field groups:
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
How do I get the settings to the frontend? I need to add classes to vc_row.
/**
* Ken Burns Effect for Row.
*/
add_action( 'vc_after_init', 'ken_burns_effect_add_option_to_vc_row' );
function ken_burns_effect_add_option_to_vc_row() {
// Ken Burns Effect Attributes
$ken_burns_effect_attributes =
array(
array(
'type' => 'checkbox',
'heading' => __( 'Ken Burns for image background', 'ken_burns_effect' ),
'param_name' => 'enable_ken_burns_effect',
'value' => array(
__( 'Yes', 'ken_burns_effect' ) => 'yes',
),
'description' => 'Check this box if you want to enable ken burns effect for this row.',
),
array(
'type' => 'dropdown',
'heading' => __( 'Direction', 'ken_burns_effect' ),
'param_name' => 'direction_ken_burns_effect',
'value' => array(
'Zoom In' => 'zoom_in',
'Zoom Out' => 'zoom_out',
),
'description' => __( '', 'ken_burns_effect' ),
'dependency' => array(
'element' => 'enable_ken_burns_effect',
'value' => array( 'yes' ),
),
),
array(
'type' => 'textfield',
'heading' => __( 'Transition speed', 'ken_burns_effect' ),
'param_name' => 'transition_speed_ken_burns_effect',
'value' => '',
'description' => __( '', 'ken_burns_effect' ),
'dependency' => array(
'element' => 'enable_ken_burns_effect',
'value' => array( 'yes' ),
),
),
);
vc_add_params( 'vc_row', $ken_burns_effect_attributes);
}
You will have to override the vc_row.php by copying it to your theme or plugin.
You can check their documentation on how to set the override directory: https://kb.wpbakery.com/docs/inner-api/vc_set_shortcodes_templates_dir/
Then you can use your custom params inside vc_row.php using their param_name eg:
$enable_ken_burns_effect;
$direction_ken_burns_effect;
$transition_speed_ken_burns_effect;
Keep in mind that you have to override vc_row_inner.php too if you want to use options there. Same goes for section and columns.
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.
I am using wordpress and 2 plugins called unyson and brizy.
Basically unyson provides me a shortcode I want to call trough the front page builder brizy on my homepage.
I want to call the portfolio shortcode with [portfolio]
This works.
Live website: https://xprs.ch/
Under "Our Work Portfolio"
Now I want to add options:
[portfolio page_link="On" categories="website"]
As long as I just define page_link it works.
But as soon as I try to define the categories I get the following error:
Warning: in_array() expects parameter 2 to be array, string given in /home/httpd/vhosts/xprs.ch/httpdocs/wp-content/themes/jevelin/framework-customizations/extensions/shortcodes/shortcodes/portfolio/views/view.php on line 100
I also get above error if just defining categories so I need to define this different but how?
The code from the shortcode:
NOTE: I cannot change this code.
I need to pass the parameter "website" correctly in the front end.
$options = array(
'id' => array( 'type' => 'unique' ),
'general' => array(
'title' => esc_html__( 'General', 'jevelin' ),
'type' => 'tab',
'options' => array(
'style' => array(
'type' => 'radio',
'label' => esc_html__('Style', 'jevelin'),
'desc' => esc_html__('Choose main style', 'jevelin'),
'choices' => array(
'default' => esc_html__('Standard', 'jevelin'),
'default-shadow' => esc_html__('Standard with Shadow', 'jevelin'),
'default2' => esc_html__('Trendy', 'jevelin'),
'masonry' => esc_html__('Gallery', 'jevelin'),
'masonry2' => esc_html__('Marginless Gallery', 'jevelin'),
'minimalistic' => esc_html__('Minimalistic', 'jevelin'),
),
'value' => 'default',
),
'categories' => array(
'type' => 'multi-select',
'label' => esc_html__('Categories', 'jevelin'),
'desc' => esc_html__('Select categories', 'jevelin'),
'population' => 'taxonomy',
'source' => 'fw-portfolio-category',
'prepopulate' => 200,
'limit' => 100,
),
'page_link' => array(
'type' => 'switch',
'label' => esc_html__( 'Page Link', 'jevelin' ),
'desc' => esc_html__( 'Enable or disable portfolio page link', 'jevelin' ),
'value' => true,
'left-choice' => array(
'value' => false,
'label' => esc_html__('Off', 'jevelin'),
),
'right-choice' => array(
'value' => true,
'label' => esc_html__('On', 'jevelin'),
),
),
),
),
);
How do I need to call categories?
Here:
$cats = array();
foreach( get_the_category() as $cat ) {
$cats[] = $cat->cat_ID;
}
$args = array(
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $cats,
'include_children' => false
)
)
);
print_r( WP_Query( $args ) as $post );
http://codex.wordpress.org/Class_Reference/WP_Query
I have a metabox in my post editor which allows me to pick a single category, I would like this changed to a checkbox where more than one can be picked. I have worked on the following, I would imagine it's a case of changing taxonomy_radio to something like taxonomy_checkbox, however that crashes the metabox completely:
$meta_boxes['test_metabox'] = array(
'id' => 'test_metabox',
'title' => __( 'TEST', 'cmb' ),
'pages' => array( 'post', ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'fields' => array(
array(
'name' => __( 'Category', 'cmb' ),
'desc' => __( 'field description (optional)', 'cmb' ),
'id' => 'test-cat',
'type' => 'taxonomy_radio',
'taxonomy' => 'category'
),
Any help would be great.
Just found out that it needs to be changed to taxonomy_multicheck