A wordpress repository question
-
Hi guys,
I wonder if the WordPress repository will allows us to deposit a plugin with cmb2 embeded in it. I still ask the question because you are yet an official plugin of this repository, so maybe have I wrong by doubting of this possibility.
Regards.
- This topic was modified 5 years, 8 months ago by moxymore.
-
Hey @moxymore
All in all, tt’s my understanding that the plugin review team appreciates these types of questions to be run through the plugin team either in the Making WordPress slack channel or via email.
Yes there are a decent handful out there, but it’s currently frowned upon to directly bundle, with something like TGM Plugin Activation being the preferred method of handling a dependency like this https://tgmpluginactivation.com/
Allright, thanks again. i’ll take a deep look into it.
One more question (not related) : is there a way to add additionnal html output in only one choice (option) of a radio select field?
I have tried with the “label_cb” parameter, but it targets the field’s label, not the option label.
The “option_cb” parameter seems to only target available option on a request, it didn’t act on the html output which wraps these choices.
I can then probably use “render_row_cb”, but I have 2 problems. The first one is that the only example of GitHub is for a text field, I can’t find an example to reproduce the standard cmb2 radio field. And my second issue is even if I had this example, I’ll encounter another problem because I have to put a double span only after a specific option (not on every single options).
Do you think what I expect to do is doable? One more time, this is for design pupose, I just expect to change this default radio button into a more designed switch button.
Regards.
- This reply was modified 5 years, 8 months ago by moxymore.
I wonder if the information below would allow for conditionally modifying, or return original markup.
https://github.com/CMB2/CMB2/wiki/Field-Parameters#render_row_cb
“The callback function gets passed the field $args array, and the $field object.”
I’m hopeful you could use those variables to determine if things should be affected, or if you just want to return early.
Worth an exploratory shot at least. I would also recommend reading through all of https://github.com/CMB2/CMB2/wiki/Field-Parameters in case others stand out as possible solutions.
It’s ok, worked as expected with conditional statements inside the “render_row_cb” function’s parameter.
At the starting point, I just wondered if I had missed a “before_input” or “after_input” parameter, but it seems it doesn’t exists yet. If you have the time one day, you can maybe add it, even if it’s for very specific use cases.
Thanks again, for the 5th times :p
Just in case, wouldn’t some of the items listed at https://github.com/CMB2/CMB2/wiki/Field-Parameters#before-after-before_row-after_row-before_field-after_field cover that? Specifically the before/after field parts? Or is that almost but not quite where you were thinking?
Nope, I tested them all. But I expected to use one of these, but none was good for my case. I don’t remember if it was because of the generated ul/li or something else.
Well, can I ask you a final question before closing this thread please? I wish to know if I missed something when generating my custom output for my radio checkbox… because value aren’t saved right now, and I don’t why. Is your “data-hash” html data-attribute mandatory to save the data? Here’s my code :
function gia_test_render_row_cb( $field_args, $field ) { $id = $field->args( 'id' ); $label = $field->args( 'name' ); $name = $field->args( '_name' ); $value = $field->escaped_value(); $description = $field->args( 'desc' ); $options = $field->args( 'options' ); ?> <div class="switch switch--horizontal"> <?php foreach( $options as $option ) { if( $option == 'No' ) { ?> <label for="<?php echo $id; ?>"><?php echo $option; ?></label> <input id="<?php echo $id; ?>" type="radio" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/><span class="toggle-outside"><span class="toggle-inside"></span></span> <? } else { ?> <label for="<?php echo $id; ?>"><?php echo $option; ?></label> <input id="<?php echo $id; ?>" type="radio" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/> <?php } } ?> <p class="description"><?php echo $description; ?> </div><?php }
The problem seems to be with this :
$field->escaped_value
, it returns nothing. This part is missing from my exemple code, but I have just added a line like this$value = $field->escaped_value();
I confirm that nothing is saved, I have inspected the $field object.
- This reply was modified 5 years, 8 months ago by moxymore.
Curious if that’s one that needs to have a value passed into it first. I would need to double check though. May be one that just acts on an existing property.
Can you provide your entire CMB2 config code for this, assuming you’re still having some issues.
Allright, here we go. But first of all, please note that that when removing the ‘render_row_cb’ parameter, everything is properly save.
Description : I use cmb2 to generate an admin menu with submenus. The generation of this submenu is splitted into multiple files for code clarity. In my main menu plugin just generate the toplevel menu, and then I require_once the other files for submenus that are appended to the toplevel menu.
I’m giving you here the necessary code, because pasting all my fields here is useless imo :
$cpt_options = new_cmb2_box( array( 'id' => 'gia_cpt_options_page', 'title' => esc_html__( 'Custom post types', 'gia' ), 'object_types' => array( 'options-page' ), 'option_key' => 'gia_cpt_options', 'parent_slug' => 'gia_overview_options', 'save_button' => esc_html__( 'Save changes', 'gia' ), 'description' => 'My description goes here', 'display_cb' => 'gia_custom_cpt_page_output', // This is the callback for the custom HTML output of the form ) ); $cpt_options->add_field( array( 'name' => esc_html__( 'Module\'s activation', 'gia' ), 'desc' => esc_html__( 'Warning: disabling this option will deregister all your custom post types made with "Give It All". But don\'t worry, it will not delete anything. By reactivating this option later, all your datas will be back again.', 'gia' ), 'id' => 'gia_activate_cpt', 'type' => 'checkbox', 'classes' => 'gia-radio switch switch--horizontal', 'attributes' => array( 'id' => 'activate_cpt_field', ), 'before_row' => 'gia_open_custom_field_row_wrapper', 'after_row' => 'gia_close_custom_field_row_wrapper', 'render_row_cb' => 'gia_test_render_row_cb' ) ); function gia_test_render_row_cb( $field_args, $field ) { $id = $field->args( 'id' ); $label = $field->args( 'name' ); $name = $field->args( '_name' ); $value = $field->escaped_value(); $description = $field->args( 'desc' ); $cpt_datas = cmb2_get_option( 'gia_cpt_options', // option_key of the CPT submenu 'gia_add_cpt' // field id); $valuetest = $field; var_dump($valuetest); ?> <div class="switch switch--horizontal"> <ul class="tg-list"> <li class="tg-list-item"> <input id="<?php echo $id; ?>" class="tgl tgl-flip" name="<?php echo $name; ?>" type="checkbox" value="<?php echo $value; ?>"/> <label for="<?php echo $id; ?>" class="tgl-btn" data-tg-off="Nope" data-tg-on="Yeah!"></label> </li> </ul> <p class="description"><?php echo $description; ?> </div><?php }
Here is the function to generate the HTML output of the form :
function gia_custom_cpt_page_output( $hookup ) { ?> <div class="gia-wrap wrap cmb2-options-page option-<?php echo $hookup->option_key; ?>"> <?php if ( $hookup->cmb->prop( 'title' ) ) : ?> <h2 class="gia-title"><?php echo wp_kses_post( $hookup->cmb->prop( 'title' ) ); ?></h2> <?php endif; ?> <?php if ( $hookup->cmb->prop( 'description' ) ) : ?> <h3 class="gia-desc"><?php echo wp_kses_post( $hookup->cmb->prop( 'description' ) ); ?></h3> <?php endif; ?> <form class="gia-form gia-cpt-form cmb-form" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="POST" id="<?php echo $hookup->cmb->cmb_id; ?>" enctype="multipart/form-data" encoding="multipart/form-data"> <input type="hidden" name="action" value="<?php echo esc_attr( $hookup->option_key ); ?>"> <?php $hookup->options_page_metabox(); ?> <?php submit_button( esc_attr( $hookup->cmb->prop( 'save_button' ) ), 'primary', 'submit-cmb' ); ?> </form> </div> <?php }
- This reply was modified 5 years, 8 months ago by moxymore.
I have nearly tested everything. My last try showed me something interesting : the saved value can only be ON or empty. I have seen this by modifying the checkbox’s generated HTML output (render_row_cb) for a text field. I was then able to see directly what happened after I hit the save button. Every single value different than “on” will return an empty string (checkbox disabled). If the value is “on”, it will be checked.
That’s said, if a checkbox can only accept an “on” or empty value, this means that the value itself is saved with something different than “on” when the checkbox is checked. Something is missing in my render_row_cb function … or something is wrong in cmb2 itself in the way he has to save datas.
Do you had the time to check it maybe? This issue seems not related on my side, but on how cmb2 handle saved data with custom html markup. You’re probably busy, i’ll wait.
Have a nice day.
Last i knew, “on” was what got saved with checked/radio values, in the database, and then when nothing is checked, it simply got removed instead of trying to save an empty value.
Beyond that, I haven’t had a chance to really dive in with the code graciously provided above, but I am keeping the tab open in my browser so that I can return to it later when I have a bit more time.
Just wanted to check in and see if you at least had any progress made on your own with this since the last posting, or if you’re still in limbo needing guidance?
Unfortunately not. I’m unable to make it work. Have you test on your own? Have you the same issue?
- The topic ‘A wordpress repository question’ is closed to new replies.