wordpress - What are the valid values of 'std' for 'List Item' option tree type? - wordpress

I want to have a default value ('std') for a list-item type. I know how to set std for types other than list-item. Here's an example:
array(
'id' => 'logo',
'label' => 'Logo',
'std' => OT_URL.'others/logo#2x.png', // This is std for upload type.
'type' => 'upload',
),
array(
'id' => 'regions-page',
'label' => 'Default Page Widgets Area',
'std' => 'right', // This is std for radio-image type.
'type' => 'radio',
'choices' => array(
array(
'value' => 'right',
'label' => 'Region 2',
),
array(
'value' => 'left',
'label' => 'Region 3',
),
)
For types like radio and select there are some choices which std may refer to one of their values. list-item type has something like choices called settings. Here's an example of list-item type:
array(
'id' => 'home-ads-items',
'label' => 'Add ADS. banner under Featured Products',
'std' => '', // I want to set this in order to Image setting would be its default.
'type' => 'list-item',
'settings' => array(
array(
'id' => 'img',
'label' => 'Image',
'std' => '',
'type' => 'upload',
'class' => '',
'choices' => array()
),
...
I want to set std in order to have Image as default value. But I don't know how.

It turns out to be fairly simple actually. I'm assuming you have the latest version (2.2.0), I don't know if it works in lower versions.
So, consider this configuration:
'id' => 'colors',
'section' => 'colors',
'label' => 'Colors',
'type' => 'list-item',
'settings' => array(
array(
'id' => 'color',
'label' => 'Color',
'type' => 'colorpicker',
),
),
Now, we want to have two default colors, white and black. Simply add two arrays in the std option, and use id's as keys. So, we add:
'std' => array(
array('title' => 'White', 'color' => '#ffffff'),
array('title' => 'Black', 'color' => '#000000'),
),
That's it. You'll have two predefined colors. As you can see, we don't have a configured list-item item with id title, but it's still present, because that's a default entry available to each option.

Related

Meta box tabs - in a custom post type

Is it possible to insert meta box tabs in a specific custom post type.
I've been looking at the example :
add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
// 1st Meta Box
$meta_boxes[] = array(
'title' => 'Meta Box Tabs Demo',
// List of tabs, in one of the following formats:
// 1) key => label
// 2) key => array( 'label' => Tab label, 'icon' => Tab icon )
'tabs' => array(
'contact' => array(
'label' => 'Contact',
'icon' => 'dashicons-email', // Dashicon
),
'social' => array(
'label' => 'Social Media',
'icon' => 'dashicons-share', // Dashicon
),
'note' => array(
'label' => 'Note',
'icon' => 'https://i.imgur.com/nJtag1q.png', // Custom icon, using image
),
),
// Tab style: 'default', 'box' or 'left'. Optional
'tab_style' => 'default',
// Show meta box wrapper around tabs? true (default) or false. Optional
'tab_wrapper' => true,
'fields' => array(
array(
'name' => 'Name',
'id' => 'name',
'type' => 'text',
// Which tab this field belongs to? Put tab key here
'tab' => 'contact',
),
array(
'name' => 'Email',
'id' => 'email',
'type' => 'email',
'tab' => 'contact',
),
array(
'name' => 'Facebook',
'id' => 'facebook',
'type' => 'text',
'tab' => 'social',
),
array(
'name' => 'Note',
'id' => 'note',
'type' => 'textarea',
'tab' => 'note',
),
),
);
This will work in post if added to functions.php ... but if we try to use it in a custom post type the metabox will appear but the fields will not.
Any help would be appreciated.

Wordpress - Trying to display date from custom form two different ways

I am trying to create an events page in Wordpress. I am using the Advanced Custom Fields plugin to generate my forms. I would like to style the month and year differently from the day in the listing section. However, I don't want to make the user have to input the same date twice in the back-end just for this. Is there a way to display the content from the one form in two different calibrations on the front-end? For example, can I display it once as month and year then once again as just the day? Is there any other workaround? Any advice would be appreciated.
Here is my custom form code:
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_5fec83c0a2f7b',
'title' => 'Event Particulars',
'fields' => array(
array(
'key' => 'field_5fec83ca0dc8b',
'label' => 'Date of event',
'name' => 'date_of_event',
'type' => 'date_picker',
'instructions' => '',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_5fec84a00dc8c',
'operator' => '!=empty',
),
),
),
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'display_format' => 'Y F j',
'return_format' => 'Y F j',
'first_day' => 1,
),
array(
'key' => 'field_5fec84a00dc8c',
'label' => '',
'name' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
'location' => array(
array(
array(
'param' => 'page_type',
'operator' => '==',
'value' => 'front_page',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
endif;
You can load the value of the date field, create a date object and then format that date object as you prefer:
// Load field value.
$date_string = get_field('date_of_event');
// Create DateTime object from value.
// Here you must use the format that you specified as the "return format" of the date field in ACF.
$date = DateTime::createFromFormat('Y F j', $date_string);
// Now you can use the date object to print the date in different formats:
echo $date->format('M Y');
echo $date->format('j M Y');
Have a look at the "Modifiy value" section of the docs:
https://www.advancedcustomfields.com/resources/date-picker/

Input type number in HelperForm PrestaShop

I am unable to set input type number in HelperForm PrestaShop.
Only text and password types are working. When set to number type, the input box disappears.
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
),
'input' => array(
array(
'type' => 'number',
'label' => $this->l('Number Input'),
'name' => 'NumberInput',
'required' => true
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
)
);
You can set the type as follows:
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
),
'input' => array(
array(
'type' => 'html',
'label' => $this->l('Number Input'),
'name' => 'NumberInput',
'required' => true,
'html_content' => '<input type="number" name="NumberInput">'
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
)
);
You can use for examplemin="1" max="5"in the input.
According to the form.tpl in admin theme, this should work.
There is no number type in PrestaShop helper forms, that is the only reason the input box disappears when you put type as number.
What you can do to achieve this is to create a simple input field and add help text or tooltip to indicate that this field only accepts numeric values. Following code can be used to add help text or tooltip to the HelperForm.
array(
'type' => 'text',
'label' => 'Custom Label',
'required' => true,
'hint' => 'Custom Tool Tip',
'desc' => 'Custom Help Text'
)
In the validation part, you can use the put the validation for numbers only and return an error if the value entered is not as expected.

How to make conditional field on cmb2 plugin?

I want to make a condition for my members info page.
$biometabox[] = array(
'id' => 'first-section',
'title' => 'Member Data',
'object_types' => array('dausfmembers'),
'fields' => array(
array(
'name' => 'Gender',
'type' => 'radio',
'id' => $dausf.'gender',
'options' => array(
'Male' => 'Male',
'Female' => 'Female'
)
),
array(
'name' => 'Gender',
'type' => 'radio',
'id' => $dausf.'mstatus',
'options' => array(
'Married' => 'Married',
'Single' => 'Single'
)
),
i want to make if female and married show this fileds in admin panel.
array(
'name' => 'Husband Name',
'type' => 'text',
'id' => $dausf.'hname',
),
can anyone help me out from this ??
"Conditional fields" seem not to be integrated within CMB2 core yet. However, there's a plugin called CMB2 Conditionals which might help you achieve the functionality you want.
After installing and setting up the plugin, it'd be simply achieved by setting up your fields as follows:
A special attention to the 'attributes' key, you can play with it as per the plugin's instructions.
$biometabox[] = array(
'id' => 'first-section',
'title' => 'Member Data',
'object_types' => array('dausfmembers'),
'fields' => array(
array(
'name' => 'Gender',
'type' => 'radio',
'id' => $dausf.'gender',
'options' => array(
'Male' => 'Male',
'Female' => 'Female',
),
'attributes' => array(
'required' => 'required',
)
),
array(
'name' => 'Gender',
'type' => 'radio',
'id' => $dausf.'mstatus',
'options' => array(
'Married' => 'Married',
'Single' => 'Single',
),
'attributes' => array(
'required' => 'required',
)
),
array(
'name' => 'Husband Name',
'type' => 'text',
'id' => $dausf.'hname',
'required' => true,
),
'attributes' => array(
'required' => true, // Will be required only if visible.
'data-conditional-id' => $prefix . 'gender',
'data-conditional-value' => 'Female',
),
'attributes' => array(
'required' => true, // Will be required only if visible.
'data-conditional-id' => $prefix . 'mstatus',
'data-conditional-value' => 'Married',
),
...
) );
You'll want to check the plugin's example functions here: https://github.com/jcchavezs/cmb2-conditionals/blob/master/example-functions.php
I hope you manage to make it you work. Good luck.
Although this might be a bit convoluted you can also write a custom jQuery script to show and hide fields based on the options selected:
In your theme directory add two folders called "js" and "css" if they aren't already there.
Then create a file in /js called "admin_scripts.js". And create a file in /css called "admin.css".
So you'll have:
theme_directory/css/admin.css
theme_directory/js/admin_scripts.js
In your functions.php add the following:
function admin_scripts() {
// Adding custom admin scripts file
wp_enqueue_script( 'admin-js', get_template_directory_uri() . '/js/admin_scripts.js', array( 'jquery' ));
// Registering and adding custom admin css
wp_register_style( 'custom_wp_admin_css', get_template_directory_uri() . '/css/admin.css', false, '1.0.0' );
wp_enqueue_style( 'custom_wp_admin_css' );
}
Then just below this function add:
add_action( 'admin_enqueue_scripts', 'admin_scripts' );
In js/admin_scripts.js add the following (remember to change the ids and classes to your field's ids and classes)
jQuery(document).ready( function() {
if( jQuery('#cmb2_select_field_id').val() == 'conditional_option') {
jQuery('.cmb2-field-to-display-on-select').show();
}
jQuery('#cmb2_select_field_id').bind('change', function (e) {
if( jQuery('#cmb2_select_field_id').val() == 'conditional_option') {
jQuery('.cmb2-field-to-display-on-select').show();
}
else{
jQuery('.cmb2-field-to-display-on-select').hide();
}
});
});
And in css/admin.css add the following:
.cmb2-field-to-display-on-select {
display:none;
}

make CMB text_datetime_timestamp field repeatable

I'm using CMB to creat custom fields for wordpress custom post
https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress
I'm using text_datetime_timestamp to set date and time and I need to make this field repeatable which is not
as I go through the documentation we can make new fields but I can't figure out how it make "text_datetime_timestamp" field repeatable
any body can show me the way to accomplish that ?
thank you
I did not find a solution to make the field repeatable so I put the field inside group and made it repeatable
array(
'id' => $prefix . 'repeat_date_group',
'type' => 'group',
'description' => '',
'options' => array(
'group_title' => __( 'Date/Time {#}', 'cmb' ),
'add_button' => __( 'Add Another Date/Time', 'cmb' ),
'remove_button' => __( 'Remove Date/Time', 'cmb' ),
'sortable' => true, // beta
),
'fields' => array(
array(
'name' => 'Date/Time',
'desc' => '',
'id' => $prefix . 'course_date',
'type' => 'text_datetime_timestamp'
),
),
),
I hope this answer will help somebody
Well here is my code, you may try this
$cmb= new_cmb2_box( array(
'id' => $prefix.'testing',
'title' => _('Testing'),
'object_types' => array('post'),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
));
$cmb->add_field( array(
'name' => 'Test Date/Time Picker Combo (UNIX timestamp)',
'id' => 'wiki_test_datetime_timestamp',
'type' => 'text_datetime_timestamp',
'repeatable' => true,
) );

Resources