$cmb->add_field( array(
'name' => 'Select Video or Image',
'desc' => 'Select an option',
'id' => 'the_wiki_id_one',
'type' => 'select',
'show_option_none' => true,
'default' => 'custom',
'options' => array(
'standard' => __( 'Option One', 'cmb2' ),
'custom' => __( 'Option Two', 'cmb2' ),
'none' => __( 'Option Three', 'cmb2' ),
),
) );
Above is the code. Now suppose If I have chosen Option two, and I want to execute a logic Like this →
If (Option 2 selected) {
<?php Then execute some PHP code ?>
}
Option 2 selected → layman Language. How can we do this in terms of programming one?
Above is a code from CMB2 Wordpress Plugin
If i get it right, then you just need to get that value from post meta, and make condition.
$the_wiki_id_one = get_post_meta($post_id, 'the_wiki_id_one', true);
// Option 2 is selected
if( 'custom' === $the_wiki_id_one ){
//Then execute some PHP code
}
Related
I'll try to be short:
Multilingual Wordpress site with custom post type registered in functions.php and CMB2 for creating custom forms. The plugin for multilingual is q-translate-x and also using CMB2-qTranslate.
My problem is that I'm losing all line breaks and all <p> tags when I switch the language but it only fails if the editor is contained in a repeatable group. If I add the wysiwyg edit as normal field It works fine.
-Relevant code for the normal field (this works fine):
$cmb_tb->add_field( array(
'name' => esc_html__( 'Historia', 'cmb2' ),
'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'historia',
'type' => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, 'editor_class' => 'cmb2-qtranslate'),
) );
-Relevant code for repeatable field (this loses line breaks and <p> tags on language switching):
<pre><code>
$group_field_id = $cmb_tb->add_field( array(
'id' => 'Fincas',
'type' => 'group',
'description' => __( 'Fincas', 'cmb2' ),
// 'repeatable' => false, // use false if you want non-repeatable group
'options' => array(
'group_title' => __( 'Finca {#}', 'cmb2' ), // since version 1.1.4, {#} gets replaced by row number
'add_button' => __( 'Añadir otra Finca', 'cmb2' ),
'remove_button' => __( 'Eliminar Finca', 'cmb2' ),
// 'sortable' => true, // beta
'closed' => false, // true to have the groups closed by default
),
) );
$cmb_tb->add_group_field($group_field_id, array(
'name' => esc_html__( 'Nombre Finca', 'cmb2' ),
'desc' => esc_html__( '', 'cmb2' ),
'id' => $prefix . 'nombre_finca',
'type' => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, 'editor_class' => 'cmb2-qtranslate')
) );
</code></pre>
This is driving me crazy. I've created a sample website with this problem for testing. I can give access to anyone who feels that can help.
Many thanks in advance.
Finally, I figured out.
It was as easy to add 'wpautop' => false to options array
I would like to use an Array returned by a Woocommerce filter as a value in a Redux Framework field. I do understand the filter returns after the Woocommerce plugin has loaded. I've tried to move the Redux::setSection method inside the filter function but it appears the section method is not called in that case. Any suggestions?
$statuses;
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses' );
function custom_wc_order_statuses( $order_statuses ) {
$statuses = $order_statuses;
return $order_statuses;
}
/*Need to pass $statuses to the Redux::setSection*/
Redux::setSection( $opt_name, array(
'title' => __( 'Text Options', 'redux-framework-demo' ),
'desc' => __( 'For full documentation on this field, visit: ', 'redux-framework-demo' ) . 'http://docs.reduxframework.com/core/fields/text/',
'id' => 'opt-text-subsection',
'icon' => 'el el-home',
'subsection' => false,
'fields' => array(
array(
'id'=>'multi-text',
'type' => 'multi_text',
'title' => __('Multi Text Option - Color Validated', 'redux-framework-demo'),
'validate' => 'color',
'subtitle' => __('If you enter an invalid color it will be removed. Try using the text "blue" as a color. ;)', 'redux-framework-demo'),
'desc' => __('This is the description field, again good for additional info.', 'redux-framework-demo')
),
array(
'id' => 'opt-select',
'type' => 'select',
'title' => __('Select Option', 'redux-framework-demo'),
'subtitle' => __('No validation can be done on this field type', 'redux-framework-demo'),
'desc' => __('This is the description field, again good for additional info.', 'redux-framework-demo'),
// Must provide key => value pairs for select options
'options' => $statuses,
'default' => '2',
)
)
) );
Use the Redux data field. You can make a custom callback, and the data will always be up to date.
https://docs.redux.io/configuration/argument-data.html#using-a-custom-callback
Also if you need to update the option from outside of Redux (say on WooCommerce update of that value) you can set it this way:
https://docs.redux.io/guides/advanced-updating-an-option-manually.html
I am trying to use Redux inside the wordpress theme and not as a plugin. In functions.php I included both redux-framework.php and sample-config.php .
Now I need to create a repeater field.
From Redux doc, I got the following code to use in order to create a repeater field:
$this->sections[] = array(
'title' => __('Repeater Field', 'redux-framework-demo' ),
'icon' => 'el-icon-thumbs-up',
'fields' => array(
array(
'id' => 'repeater-field-id',
'type' => 'repeater',
'title' => __( 'Title', 'redux-framework-demo' ),
'subtitle' => __( '', 'redux-framework-demo' ),
'desc' => __( '', 'redux-framework-demo' ),
//'group_values' => true, // Group all fields below within the repeater ID
//'item_name' => '', // Add a repeater block name to the Add and Delete buttons
//'bind_title' => '', // Bind the repeater block title to this field ID
//'static' => 2, // Set the number of repeater blocks to be output
//'limit' => 2, // Limit the number of repeater blocks a user can create
//'sortable' => false, // Allow the users to sort the repeater blocks or not
'fields' => array(
array(
'id' => 'title_field',
'type' => 'text',
'placeholder' => __( 'Title', 'redux-framework-demo' ),
),
array(
'id' => 'text_field',
'type' => 'text',
'placeholder' => __( 'Text Field', 'redux-framework-demo' ),
),
array(
'id' => 'select_field',
'type' => 'select',
'title' => __( 'Select Field', 'redux-framework-demo' ),
'options' => array(
'1' => __( 'Option 1', 'redux-framework-demo' ),
'2' => __( 'Option 2', 'redux-framework-demo' ),
'3' => __( 'Option 3', 'redux-framework-demo' ),
),
'placeholder' => __( 'Listing Field', 'redux-framework-demo' ),
),
)
)
)
);
but if I place the code inside functions.php, what will the $this variable refer to? It'll produce errors. So how to use the snippet so that I can retrieve the values from template files as well?
You have to tried an old version system to create a section. You can try the new version system, I'm not sure if this works or not, but you can try this way:
Redux::setSection($opt_name, array(
'title' => __('Ads Sections', 'cbnews'),
'id' => 'ads-sections',
'desc' => __('You can manage your ads', 'cbnews'),
'icon' => 'dashicons dashicons-dashboard',
'fields' => array(
array(
'id' => 'repeater-field-id',
'type' => 'repeater',
'title' => __( 'Title', 'redux-framework-demo' ),
'subtitle' => __( '', 'redux-framework-demo' ),
'desc' => __( '', 'redux-framework-demo' ),
//'group_values' => true, // Group all fields below within the repeater ID
//'item_name' => '', // Add a repeater block name to the Add and Delete buttons
//'bind_title' => '', // Bind the repeater block title to this field ID
//'static' => 2, // Set the number of repeater blocks to be output
//'limit' => 2, // Limit the number of repeater blocks a user can create
//'sortable' => false, // Allow the users to sort the repeater blocks or not
'fields' => array(
array(
'id' => 'title_field',
'type' => 'text',
'placeholder' => __( 'Title', 'redux-framework-demo' ),
),
array(
'id' => 'text_field',
'type' => 'text',
'placeholder' => __( 'Text Field', 'redux-framework-demo' ),
),
array(
'id' => 'select_field',
'type' => 'select',
'title' => __( 'Select Field', 'redux-framework-demo' ),
'options' => array(
'1' => __( 'Option 1', 'redux-framework-demo' ),
'2' => __( 'Option 2', 'redux-framework-demo' ),
'3' => __( 'Option 3', 'redux-framework-demo' ),
),
'placeholder' => __( 'Listing Field', 'redux-framework-demo' ),
),
)
)
)
));
Im trying to add variables instead of custom field IDs in my metaboxes file using this script
I added some options in redux framework to give possibility to change the custom fields.
<?php
/*global from framework*/
global $redux;
/*custom fields options retrieved from redux framework*/
$custom_videourl = $redux['mytheme_videourl'];
$custom_duration = $redux['mytheme_duration'];
$custom_description = $redux['mytheme_desc'];
$fields = array(
array(
'label' => __( 'MP4/FLV & Youtube Url', 'framework' ),
'desc' => __( 'Here you can add videos with mp4 format', 'framework' ),
'id' => $custom_videourl,
'type' => 'text'
),
array(
'label' => __( 'Video Duration', 'framework' ),
'desc' => __( 'Example: 5:20', 'framework' ),
'id' => $custom_duration,
'type' => 'text'
),
array(
'label' => __( 'Video Description', 'framework' ),
'id' => $custom_description,
'desc' => __( 'Here you can write a description', 'framework' ),
'type' => 'editor'
)
);
$my_metaboxes = new custom_add_meta_box( 'mytheme_metaboxes', __( 'Video - Additional Information', 'framework' ), $fields, 'post', true );
But with the above example I got Notice: Array to string conversion in /var/www/html/mytheme/wp-includes/formatting.php on line 1025
So if I add the custom field without variable metaboxes are working fine like below content:
$fields = array(
array(
'label' => __( 'MP4/FLV & Youtube Url', 'framework' ),
'desc' => __( 'Here you can add videos with mp4 format', 'framework' ),
'id' => 'mytheme_videourl',
'type' => 'text'
),
array(
'label' => __( 'Video Duration', 'framework' ),
'desc' => __( 'Example: 5:20', 'framework' ),
'id' => 'mytheme_duration',
'type' => 'text'
),
array(
'label' => __( 'Video Description', 'framework' ),
'id' => 'mytheme_desc',
'desc' => __( 'Here you can write a description', 'framework' ),
'type' => 'editor'
)
);
$my_metaboxes = new custom_add_meta_box( 'mytheme_metaboxes', __( 'Video - Additional Information', 'framework' ), $fields, 'post', true );
I have tried using print_r but metaboxes don't save than. Is there any way to make the first code working? Using variables instead of custom field IDs?
It seems really likely that one of your redux variables contains an Array instead of a String. Knowing that, you just need to figure out which one it is and figure out where the actual piece of data that you are looking for is.
One way you could debug this is to do an explicit conversion of all three of the redux variables to strings. (e.g. 'id' => implode("***", $custom_videourl)). Then, once you've figured out which one (or more than one) is an array, you'll probably know how to access the piece of data you actually want.
If that doesn't do it for you, I would suggest adding this to your wp-config.php file: define( 'WP_DEBUG_LOG', true );
This will create a debug log for you. You can then log out to it (e.g. error_log( print_r( $custom_videourl ) );
I believe it usually stores the debug.log file in the wp-content folder.
Hello guys i have a trouble!
i would like to ask you how can i make 1 condition code for each option of the bellow function field_cliente :
function field_cliente( $fields) {
$fields['billing']['billing_cliente'] = array (
'label' => __('Tipo di Cliente', 'woocommerce'),
'id' => '_select',
'type' => 'select',
'options' => array (
'default' => __( 'Seleziona', 'woocommerce' ),
'one' => __( 'Privato', 'woocommerce' ),
'two' => __( 'Azienda', 'woocommerce' ),
'three' => __( 'Impresa Individuale', 'woocommerce' )
)
);
return $fields;
}
For example:
If "One" {
add_filter(' ..');
etc...
}
if "Two" {
add_filter(' ..');
}
I need to know the way of do it because i'm trying to make a show off of filters for each condition choose by the client in the web page checkout