Forum Replies Created

Viewing 15 replies - 1 through 15 (of 109 total)
  • [EDIT] I have found the culpirit, but not the line in cmb2’s code : it’s the parameter

    ‘default’ => ‘on’

    If you don’t set it or put in it an empty value, it works. If you put in it a default value of “on”, it will always stays ON even after a save change.

    • This reply was modified 8 months, 3 weeks ago by moxymore.

    Well, this is definitely a bug… I have installed an approved GitHub’s contribution that is listed on your GitHub CMB2 description page, and the same issue occur : “on” value is never saved.

    https://github.com/themevan/CMB2-Switch-Button

    moxymore

    (@moxymore)

    Unfortunately not. I’m unable to make it work. Have you test on your own? Have you the same issue?

    moxymore

    (@moxymore)

    Hi Joy and thanks for your answer. I need the post title because it’s a part of the new upload directory. Moreover, when you create a new post, and before it’s saved for the first time, there’s no way to get this title with a standard $_POST. That’s why I expected to cover this case, by taking in real time what is written in the title field.

    Regarding what you said, do you mean that there is an action hook for the upload button in which I can pass a parameter in it? Which one is this? I’m interested.

    Regards.

    @kezily Just tell to your dev (or do it yourself) to add this code in the proper file of your plugin location :

    function custom_is_plugin_active( $plugin ) {
        return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || custom_is_plugin_active_for_network( $plugin );
    }
    
    function custom_is_plugin_active_for_network( $plugin ) {
        if ( ! is_multisite() ) {
            return false;
        }
    
        $plugins = get_site_option( 'active_sitewide_plugins' );
        if ( isset( $plugins[ $plugin ] ) ) {
            return true;
        }
    
        return false;
    }
    
    if( !custom_is_plugin_active( WP_PLUGIN_DIR . '/cmb2/init.php' )) {
    	if ( file_exists( YOUR_PLUGIN_PATH . '/cmb2/init.php' ) ) {
    		require_once YOUR_PLUGIN_PATH . '/cmb2/init.php';
    	} elseif ( file_exists( YOUR_PLUGIN_PATH . '/CMB2/init.php' ) ) {
    		require_once YOUR_PLUGIN_PATH . '/CMB2/init.php';
    	}
    }

    This code is to adapt to the plugin itself.

    A more simple solution, to wrap under conditional statement, could be done with wp_deregister_style( 'cmb2-styles' ); :

    add_action( 'admin_enqueue_scripts', function () {
    // If statement made by you, needed to don't break CSS of your plugin
        wp_deregister_style( 'cmb2-styles' );
    // endif
    });

    This will deregister cmb2 stylesheet load and will solve your issue.

    • This reply was modified 9 months, 1 week ago by moxymore.

    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.

    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.

    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 9 months, 1 week ago by moxymore.

    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 9 months, 1 week ago by moxymore.

    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
    }

    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

    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 9 months, 1 week ago by moxymore.

    Allright, it seems that I have to do some sort of custom jQuery to do this. Like said previously, I never code in JS but this sounds not very complicated.

    What I’ll try is :
    1 – Désactivate the ‘remove_confirm’ option of the cmb2 repeater.
    2 – Append an HTML div to the form, which will have a negative margin (or opacity:0) and which will be displayed on.click() by JS
    3 – Append a click.to.action on the specified divs (the cross of my repeater, and the custom “delete item” at the end of each item.

    Another challenge here, but i’ll be able to do it. I worked hard yesterday to make a little function which conditionnally display fields (a kind of “show advanced parameters” which works in a repeater) and I had success. So why not continuing this JS/jQuery lesson further :p

    Thank you again.

    • This reply was modified 9 months, 1 week ago by moxymore.

    I’m speaking of the alerts generated by your parameters for group fields (repeater) : `[option] : array(
    ‘remove_confirm’ : esc_html__( ‘Are you sure you want to remove this item?’, ‘gia’ )
    )`

    Alerts box are generated each time I want to delete an item of a repeater. I expected to tweak the design a bit, cause the generated one is very very very generic 😉

    PS : what a shame, WordPress doesn’t allow me to put = and > in a code bloc, he sanitizes it and break the initial display.

    • This reply was modified 9 months, 1 week ago by moxymore.
    • This reply was modified 9 months, 1 week ago by moxymore.
    • This reply was modified 9 months, 1 week ago by moxymore.
    • This reply was modified 9 months, 1 week ago by moxymore.
    • This reply was modified 9 months, 1 week ago by moxymore.
    • This reply was modified 9 months, 1 week ago by moxymore.
    • This reply was modified 9 months, 1 week ago by moxymore.

    You’re the king. This solves the issue as you expected.

Viewing 15 replies - 1 through 15 (of 109 total)