Related
Am trying to get the ID of single product page inside my function but it's returning bool(false) or NULL.
What am trying to achieve is to get the ID to filter the categories taxonomies terms of a specific product and display it in a tab using ACF but unfortunately am not able to get the ID.
I have the global $post code working in the meta box I added for the single products edit page so I'm not sure what am missing here.
Below is my code:
function my_acf_add_local_field_groups() {
global $post;
var_dump($post); // returns NULL and bool(false) when echo'd
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');
I want to programmatically add a tab with repeater inside but I can't seem to find a solution, I've googled all available resources but still not working.
I already tried using acf_add_local_field_group and acf_add_local_field but still no luck.
Well I can create a tab using acf_add_local_field but when I tried to add a child which in this case a repeater OR even a text field it still doesn't work.
Here's my code to create a tab and its child but the child doesn't work.
acf_add_local_field(array(
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => '',
'parent' => 'field_5bd14c9349930',
'fields' => array (
array(
'key' => 'field_2',
'label' => 'This is a test',
'name' => 'my_test',
'type' => 'text',
)
)
));
You should use acf_add_local_field_group to construct the whole field group.
Here's the proper code for adding a group and a custom tab with single repeater field inside:
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array (
'key' => 'group_1',
'title' => 'My Group',
'fields' => array (
array (
'key' => 'field_unique_key',
'label' => 'First Tab',
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'placement' => 'top',
'endpoint' => 0,
),
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 ( // Here you can add as many subfields for this repeater as you want
array (
'key' => 'field_unique_key',
'label' => 'Link',
'name' => 'link',
'type' => 'link', // example link type
'instructions' => 'Link name and URL',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'array',
),
),
),
),
'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' => '',
));
endif;
I just created a nested shortcode with vc_map for a wordpress website.
It works pretty well and is really simple.
My parent shortcode is "simple_table" and my chlidren shortcode are "simple_table_row".
[simple_table param="foo"]
[simple_tablerow param="another_foo"]
[simple_tablerow param="another_foo"]
[simple_tablerow param="another_foo"]
[/simple_table]
I can add my shortcode in the root of a page or in row.
However I am unable to add inside an another container like tabs, tour, accordion or Pageable Container. My nested shortcode doesn't appear in the list of elements. I already created several simples shortcodes which work properly in these specific cases.
Here are my vc_map :
vc_map( array(
"name" => "Simple_table",
"description" => "Simple_table",
"base" => "simple_table",
"class" => "simple_table",
"content_element" => true,
"is_container" => true,
'as_parent' => array('only' => 'simple_tablerow'),
"show_settings_on_create" => true,
"icon" => "simple_table_icon",
"category" => __('Content', 'js_composer'),
"js_view" => 'VcColumnView',
"params" => array(
array(
'type' => 'param_group',
'value' => '',
'param_name' => 'cols',
"heading" => "Cols",
'params' => array(
array(
'type' => 'textfield',
"holder" => "div",
'value' => '',
'heading' => 'Data',
'param_name' => 'data',
'admin_label' => true,
),
array(
'type' => 'textfield',
'value' => '',
'heading' => 'Style',
'param_name' => 'style',
),
array(
'type' => 'textfield',
'value' => '',
'heading' => 'Class',
'param_name' => 'class',
)
)
),
array(
"type" => "checkbox",
"class" => "",
"heading" => "hide_header",
"param_name" => "hide_header"
),
array(
"type" => "textfield",
"holder" => "",
"class" => "",
"heading" => "Class",
"param_name" => "class"
),
),
)
);
vc_map( array(
"name" => "Simple_tablerow",
"description" => "simple_tablerow",
"base" => "simple_tablerow",
"class" => "simple_tablerow",
"content_element" => true,
"as_child" => array('only' => 'simple_table'),
"show_settings_on_create" => true,
"icon" => "hide_header",
"category" => __('Content', 'js_composer'),
"params" => array(
array(
'type' => 'param_group',
'value' => '',
'param_name' => 'cols',
"heading" => "Cols",
'params' => array(
array(
'type' => 'textfield',
'value' => '',
'heading' => 'Data',
'param_name' => 'data',
'admin_label' => true,
),
array(
'type' => 'textfield',
'value' => '',
'heading' => 'Style',
'param_name' => 'style',
),
array(
'type' => 'textfield',
'value' => '',
'heading' => 'Class',
'param_name' => 'class',
)
),
),
array(
'type' => 'textfield',
'value' => '',
'heading' => 'Class',
'param_name' => 'class',
)
),
)
);
How can I add my nested shortcodes available in container like tabs, tour, accordion or Pageable Container ?
Note : The parameter "allowed_container_element" seems to be the cause, but how do I modify this value ?
Hopefully you found the answer to this already, since this is a bit old, but I was searching for an answer myself and this came up.
This documentation site might be of use to you https://kb.wpbakery.com/docs/developers-how-tos/nested-shortcodes-container/
If I had to guess, it seems you forgot the last bit of code at the bottom where you extend the WPBakeryShortCodesContainer
//Your "container" content element should extend WPBakeryShortCodesContainer class to inherit all required functionality
if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
class WPBakeryShortCode_Your_Gallery extends WPBakeryShortCodesContainer {
}
}
if ( class_exists( 'WPBakeryShortCode' ) ) {
class WPBakeryShortCode_Single_Img extends WPBakeryShortCode {
}
}
It's been over 3 years from original question, but i had similar problem. Removing the following line worked for me:
"is_container" => true,
I put this code into my functions.php
include_once('advanced-custom-fields/acf.php');
include_once('acf-options-page/acf-options-page.php');
include_once('acf-repeater/acf-repeater.php');
define( 'ACF_LITE', true );
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_options',
'title' => 'Options',
'fields' => array (
array (
'key' => 'field_525d1b6d49043',
'label' => 'lable',
'name' => 'homepage',
'type' => 'repeater',
'instructions' => 'Select which categories you want to display on the homepage.',
'sub_fields' => array (
array (
'key' => 'field_525d1b8a49044',
'label' => 'Category',
'name' => 'firsttext',
'type' => 'text',
'column_width' => '',
'field_type' => 'text',
'return_format' => 'id',
),
array (
'key' => 'field_525d2473de72c',
'label' => 'Number of Posts',
'name' => 'number-of-posts',
'type' => 'text',
'column_width' => '',
'default_value' => 4,
'placeholder' => '',
'prepend' => '',
'append' => '',
'min' => 2,
'max' => '',
'step' => 2,
),
),
'row_min' => 1,
'row_limit' => 1,
'layout' => 'row',
'button_label' => 'Add a category',
),
),
'location' => array (
array (
array (
'param' => 'options_page',
'operator' => '==',
'value' => 'acf-options',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
Then I enter values into fields in admin console
How I cat get this value from page file?
I try get_field('acf_options) and I try get_field('homepage), but this return null
Im not sure why you would want to put this in your function.php file instead of just do this this through the plugin.
But if you want to retrieve those field values, you should do this: get_field( 'homepage', 'option' );
instead of:
get_field( 'homepage' ) or get_field( 'acf_options' );
My best advice would be to look on the ACF website for information. These docs help out a lot!
I'm using Advanced Custom Fields (ACF) and trying to add a repeater programmatically to an existing group (group_5621b0871e1b1), but it doesn't work. The same code works for a text field but not for the repeater.
In my plugin:
add_action( 'acf/init', 'acf_add_field_royalties' );
function acf_add_field_royalties() {
if ( function_exists( 'acf_add_local_field_group' ) ) {
acf_add_local_field( array (
'key' => 'field_store_royalties',
'label' => 'Royalties',
'name' => 'store_royalties1',
'type' => 'repeater',
'parent' => 'group_5621b0871e1b1',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => '',
'max' => '',
'layout' => 'table',
'button_label' => 'Add new royalty period',
'sub_fields' => array (
array (
'key' => 'field_start_date',
'label' => 'Start Date',
'name' => 'start_date1',
'type' => 'date_picker',
'instructions' => '',
'required' => 1,
'display_format' => 'F j, Y',
'return_format' => 'd/m/Y',
'first_day' => 1,
),
array (
'key' => 'field_end_date',
'label' => 'End date',
'name' => 'end_date1',
'type' => 'date_picker',
'instructions' => '',
'display_format' => 'F j, Y',
'return_format' => 'd/m/Y',
'first_day' => 1,
),
array (
'key' => 'field_royalty_rate',
'label' => 'Royalty Rate',
'name' => 'royalty_rate1',
'type' => 'number',
'instructions' => '',
'required' => 1,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => 0,
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => 1,
'readonly' => 0,
'disabled' => 0,
)
)
));
}
}
It shows this error in the group_5621b0871e1b1 group:
Warning: Invalid argument supplied for foreach() in /usr/share/nginx/html/wordpress4/wp-content/plugins/advanced-custom-fields-pro/pro/fields/repeater.php on line 255
Warning: Invalid argument supplied for foreach() in /usr/share/nginx/html/wordpress4/wp-content/plugins/advanced-custom-fields-pro/pro/fields/repeater.php on line 320
Am I doing something wrong?
Is it possible to add a repeater programmatically.
Since the repeater field was added using acf_add_local_field, each subfield needs to be added with acf_add_local_field as well.
Delete all of the subfields, including the 'subfields' => array
line, from the repeater field.
Add each subfield in a new acf_add_local_field
When adding subfields, Make sure to add 'parent' => 'repeater_field_key' to the subfields.
Your code would now look like:
add_action( 'acf/init', 'acf_add_field_royalties' );
function acf_add_field_royalties() {
if ( function_exists( 'acf_add_local_field_group' ) ) {
/**
* Initial Repeater Field
*
*/
acf_add_local_field( array (
'key' => 'field_store_royalties',
'label' => 'Royalties',
'name' => 'store_royalties1',
'type' => 'repeater',
'parent' => 'group_5621b0871e1b1',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => '',
'max' => '',
'layout' => 'table',
'button_label' => 'Add new royalty period'
));
/**
* Add Start Date Subfield
*
*/
acf_add_local_field( array (
'key' => 'field_start_date',
'label' => 'Start Date',
'name' => 'start_date1',
'parent' => 'field_store_royalties', // key of parent repeater
'type' => 'date_picker',
'instructions' => '',
'required' => 1,
'display_format' => 'F j, Y',
'return_format' => 'd/m/Y',
'first_day' => 1,
));
/**
* Add End Date Subfield
*
*/
acf_add_local_field( array (
'key' => 'field_end_date',
'label' => 'End date',
'name' => 'end_date1',
'parent' => 'field_store_royalties', // key of parent repeater
'type' => 'date_picker',
'instructions' => '',
'display_format' => 'F j, Y',
'return_format' => 'd/m/Y',
'first_day' => 1,
));
/**
* Add Royalty Rate Subfield
*
*/
acf_add_local_field( array (
'key' => 'field_royalty_rate',
'label' => 'Royalty Rate',
'name' => 'royalty_rate1',
'parent' => 'field_store_royalties', // key of parent repeater
'type' => 'number',
'instructions' => '',
'required' => 1,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => 0,
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => 1,
'readonly' => 0,
'disabled' => 0,
));
}
}