jcchavezs / cmb2-conditionals

Plugin to relate fields in a CMB2 metabox
GNU General Public License v2.0
86 stars 61 forks source link

When creating a post does't conditionally display the fields #1

Open apokyro opened 9 years ago

apokyro commented 9 years ago

Hi, great addon for CMB2!

I just give it a try and it works well when you save a post, but before the first save it seems like doesn't. If I save the post then it's all good.

Also, is it possible to use the conditional display as a radio button? I tried it and it works only on select.

Thank you.

jcchavezs commented 9 years ago

Hi @apokyro. I have fixed this issue, now all the conditionals are available from the beginning.

Regarding the conditional in the radio, can you give a use case so I can decide the best approach?

apokyro commented 9 years ago

Hi again. I tested and it works perfect. Thanks

In my project I want to let the user select a layout in a radio form like the attach image: untitled-1

So, depending the radio selection I want to display fields. Is that possible?

Thanks

jcchavezs commented 9 years ago

Hi @apokyro. I have added support to radio input type. I also included an example in the example-functions.php file.

apokyro commented 9 years ago

Thanks @jcchavezs. I appreciate your help. I will tested and if I found something I will post back. Thanks again.

safiro commented 9 years ago

How to use this as a part of theme not plugin ? I'm using a Select with id "select_style" with options

'options' => array( 'first_style' => 'First Style', 'second_style' => 'Second Style', 'third_style' => 'Third Style', )

and I want when I select First Style, a text field with id "add_title" displayed, when I select Second Style, the text field disappeared and colorpicker with id "article_bg" displayed and so on....

How can I do this step by step?

apokyro commented 9 years ago

You can add to your theme folder the plugin like this: theme_name/folder_name/cmb2-conditionals/

then include this line to your functions.php require get_template_directory() . '/folder_name/cmb2-conditionals/cmb2-conditionals.php';

Last change this code from cmb2-conditionals.php wp_enqueue_script('cmb2-conditionals', plugins_url('/cmb2-conditionals.js', __FILE__ ), array('jquery'), '1.0.2', true); to this wp_enqueue_script( 'cmb2-conditionals', get_template_directory_uri(). '/folder_name/cmb2-conditionals/cmb2-conditionals.js', array( 'jquery' ), '1.0.2', true);

I think this could work.

safiro commented 9 years ago

Hi apokyro, I did all that as you say , and in cmb-functions.php which exists in main cmb folder "not cmb2-conditionals function file".

I added this code but it didn't work on changing selections: the text field is always displayed.

function cmbcond(array $cmbcond){ $cmbcond[] = array( 'title'=> 'Conditional Select', 'id'=> 'condsecy', 'object_types'=> array('page','post'), 'fields'=> array(

array(
    'name'=>'Select',
    'id'=>'mycondselect',
    'type'=> 'select',
    'options'=>array(
    'one'=>'One',
    'two'=>'Two',
    'three'=>'Three',
    ),

    ),

array( 'name'=> 'My Text', 'id'=> 'cmbcondtxt', 'type'=> 'text', 'attributes' => array(

        'data-conditional-id' =>  'mycondselect',
        'data-conditional-value' => 'two',
    )
    ),

)

);
return $cmbcond;

} add_filter('cmb2_meta_boxes','cmbcond');

What's wrong with my code?

Thanks.