Support » Plugin: CMB2 » WYSIWYG Field Options Setting Issue

  • Resolved Vikas Khunteta

    (@vikas_khunteta)


    There is one small issue when we set textarea_name inside options argument of WYSIWYG field, it doesn’t work, the value of textarea_name is always set to _name argument. So it should be changed according below function inside file includes/CMB2_Field.php

    protected function set_field_defaults_wysiwyg( $args ) {
    		$args['id'] = strtolower( str_ireplace( '-', '_', $args['id'] ) );
    		$args['options']['textarea_name'] = $args['options']['textarea_name'] ?: $args['_name'];
    
    		return $args;
    	}

    I also noticed that fields that are not of array type in basic nature doesn’t save their values in serialized array when name attribute changed to array using attributes argument

    for e.g.

    I have created different-2 meta boxes using loop of some data. So basically they all are different meta boxes but I changed their name attribute using attributes argument and inside DOM name attribute value changes to array type but when I save the post/page, the values of each field save as string with different meta_key, I think this happens due to internal _name argument used by the plugin which value is based on the id argument

    It would be great if you save the values of field based on the name attribute when added using attributes argument

    see below code which I added to create field

    $meta_box->add_field( array(
    				'name'    => __( 'URL', APP_NAME ),
    				'id'      => 'url_' . $post->ID,
    				'type'    => 'text_url',
    				'default' => get_post_meta( $post->ID, 'url', true ),
    				'attributes' => array(
    					'name' => 'url[' . $post->ID . ']'
    				)
    			) );

    Let me know if you have question on this support thread.

    • This topic was modified 7 months, 1 week ago by Vikas Khunteta. Reason: code
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    My biggest question is what are you trying to accomplish compared to what you’re getting.

    Also your CMB2 config code, as is, would help me visualize on my local install as well.

    Thread Starter Vikas Khunteta

    (@vikas_khunteta)

    It would not be easy to explain you this but let me try

    I have many posts of one custom post type suppose A and each post has fixed custom field, now what I want to is have all those posts data inside post of another custom post type suppose B so basically each post of type A will be a meta box of post of type B, the scenario is to keep default data different if required and merge posts according to some filters.

    Let me explain it in hierarchical way

    Custom post type A has posts AP1 AP2 AP3 AP4 AP5 ....
    AP1 has custom fields C1 C2 C3 C4 C5 as meta box fields ( only one meta box )
    AP2 ……………………………
    ………………………………..

    Custom post type B has posts BP1 BP2 ....
    BP1 has meta boxes AP1 AP2 AP3 AP4 AP5 and each meta box will have same custom fields C1 C2 C3 C4 C5
    same goes for other posts of type B

    In this scenario group field type is not best suited because I have two WYSIWYG fields which id can not be array and also if I create one meta box of type group than I am not able to populate all posts inside that group as a repeatable field, when I tried it, I have repeat and remove button after each post data and I want it only once in the end.

    Let me know if you get me or not

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Sounds complex, and I’m not going to call that a bad or a good thing, just a complex need. Which also leads me to wonder if it’s potentially above what CMB2 can handle.

    One way that may help me visualize and explain more, would be your current CMB2 configuration, regardless of if it’s presently working or not. I could at least see everything being attempted at the moment, paired up with your explanations above.

    Thread Starter Vikas Khunteta

    (@vikas_khunteta)

    Here is my code

    
    	/**
    	 * Create new instance of cmb2 meta box for each bethouse fields
    	 */
    	// Get all bethouses
    	$bethouses = get_posts( array(
    		'post_type'      => APP_POSTTYPE_BETHOUSE,
    		'posts_per_page' => -1,
    	) );
    
    	if ( $bethouses ):
    		$i = 1;
    		foreach( $bethouses as $bethouse ):
    
    			$for_countries = (array) get_post_meta( $bethouse->ID, APP_POSTMETA_BETHOUSE_FOR_COUNTRIES , true );
    			$for_countries = implode( ', ', $for_countries );
    
    			$terms = get_the_terms( $bethouse->ID, APP_TAXONOMY_BETHOUSE );
    			$cats = array();
    			if ( $terms ) {
    				foreach( $terms as $term ) {
    					$cats[] = $term->name;
    				}
    			}
    
    			$taxonomies = implode( ', ', $cats );
    
    			$meta_box = new_cmb2_box( array(
    				'id'           => 'bethouse_' . $bethouse->ID,
    				'title'        => $bethouse->post_title,
    				'object_types' => array( APP_POSTTYPE_TOP_BETHOUSE ), // Post type
    				'context'      => 'normal',
    				'priority'     => 'high',
    				'show_names'   => true,
    				'save_fields'  => false 
    			) );
    
    			$meta_box->add_field( array(
    				'id'         => 'ct_of_' . $bethouse->ID,
    				'type'       => 'hidden',
    				'save_field' => false,
    				'attributes' => array(
    					'data-countries'  => $for_countries,
    					'data-taxonomies' => $taxonomies
    				)
    			) );
    
    			$meta_box->add_field( array(
    				'name'    => __( 'Bethouse Active', APP_NAME ),
    				'id'      => 'active_' . $bethouse->ID,
    				'type'    => 'checkbox',
    				'attributes' => array(
    					'name'    => 'active[' . $bethouse->ID . ']',
    					'checked' => ( get_post_meta( $post_id, 'active', true )[$bethouse->ID] === 'on' ? 'checked' : false )
    				)
    			) );
    
    			$meta_box->add_field( array(
    				'name'    => __( 'Bethouse Featured Image', APP_NAME ),
    				'id'      => 'thumbnail_' . $bethouse->ID,
    				'type'    => 'file',
    				'default' => get_the_post_thumbnail_url( $bethouse->ID ),
    				// Optional:
    				'options' => array(
    					'url' => false,
    				),
    				'query_args' => array(
    					// only allow gif, jpg, or png images
    					'type' => array(
    						'image/gif',
    						'image/jpg',
    						'image/jpeg',
    						'image/png',
    					),
    				),
    				'preview_size' => array( 50, 50 ),
    				'attributes' => array(
    					'name'  => 'thumbnail[' . $bethouse->ID . ']',
    					'value' => ( get_post_meta( $post_id, 'thumbnail', true )[$bethouse->ID] ?: get_the_post_thumbnail_url( $bethouse->ID ) )
    				)
    			) );
    
    			$meta_box->add_field( array(
    				'name'    => __( 'Bethouse Logo', APP_NAME ),
    				'id'      => 'logo_' . $bethouse->ID,
    				'type'    => 'file',
    				'default' => get_post_meta( $bethouse->ID, APP_POSTMETA_BETHOUSE_LOGO, true ),
    				// Optional:
    				'options' => array(
    					'url' => false,
    				),
    				'query_args' => array(
    					// only allow gif, jpg, or png images
    					'type' => array(
    						'image/gif',
    						'image/jpg',
    						'image/jpeg',
    						'image/png',
    					),
    				),
    				'preview_size' => 'full',
    				'attributes' => array(
    					'name'  => 'logo[' . $bethouse->ID . ']',
    					'value' => ( get_post_meta( $post_id, 'logo', true )[$bethouse->ID] ?: get_post_meta( $bethouse->ID, APP_POSTMETA_BETHOUSE_LOGO, true ) )
    				)
    			) );
    
    			$meta_box->add_field( array(
    				'name'    => __( 'Bethouse URL', APP_NAME ),
    				'id'      => 'url_' . $bethouse->ID,
    				'type'    => 'text_url',
    				'default' => get_post_meta( $bethouse->ID, APP_POSTMETA_BETHOUSE_URL, true ),
    				'attributes' => array(
    					'name'  => 'url[' . $bethouse->ID . ']',
    					'value' => ( get_post_meta( $post_id, 'url', true )[$bethouse->ID] ?: get_post_meta( $bethouse->ID, APP_POSTMETA_BETHOUSE_URL, true ) )
    				)
    			) );
    
    			$meta_box->add_field( array(
    				'name'    => __( 'Post Content', APP_NAME ),
    				'id'      => 'post_content_' . $bethouse->ID,
    				'type'    => 'wysiwyg',
    				'default' => get_the_content( null, false, $bethouse->ID ),
    				'options' => array(
    					'wpautop'       => true,
    					'textarea_name' => "post_content[$bethouse->ID]",
    					'textarea_rows' => 5,
    				),
    				'attributes' => array(
    					'value' => ( get_post_meta( $post_id, 'post_content', true )[$bethouse->ID] ?: get_the_content( null, false, $bethouse->ID ) )
    				)
    			) );
    
    			$meta_box->add_field( array(
    				'name'    => __( 'Side Column', APP_NAME ),
    				'id'      => 'column_content_' . $bethouse->ID ,
    				'type'    => 'wysiwyg',
    				'default' => get_post_meta( $bethouse->ID, 'column_content', true ),
    				'options' => array(
    					'wpautop'       => true,
    					'textarea_name' => "column_content[$bethouse->ID]",
    					'textarea_rows' => 5,
    				),
    				'attributes' => array(
    					'value' => ( get_post_meta( $post_id, 'column_content', true )[$bethouse->ID] ?: get_post_meta( $bethouse->ID, 'column_content', true ) ),
    				)
    			) );
    
    			$i++;
    		endforeach;
    	endif;
    
    // Save meta boxes fields
    
    add_action( 'save_post', function( $post_id ) {
    	$slug = APP_POSTTYPE_TOP_BETHOUSE;
    	if ( $slug !== $_POST['post_type'] ) {
    		return;
    	}
    	if ( ! current_user_can( 'edit_post', $post_id ) ) {
    		return;
    	}
    
    	if ( isset( $_REQUEST['active'] ) ) {
    		update_post_meta( $post_id, 'active', $_REQUEST['active'] );
    	}
    
    	if ( isset( $_REQUEST['url'] ) ) {
    		update_post_meta( $post_id, 'url', $_REQUEST['url'] );
    	}
    
    	if ( isset( $_REQUEST['thumbnail'] ) ) {
    		update_post_meta( $post_id, 'thumbnail', $_REQUEST['thumbnail'] );
    	}
    
    	if ( isset( $_REQUEST['logo'] ) ) {
    		update_post_meta( $post_id, 'logo', $_REQUEST['logo'] );
    	}
    
    	if ( isset( $_REQUEST['post_content'] ) ) {
    		update_post_meta( $post_id, 'post_content', $_REQUEST['post_content'] );
    	}
    
    	if ( isset( $_REQUEST['column_content'] ) ) {
    		update_post_meta( $post_id, 'column_content', $_REQUEST['column_content'] );
    	}
    
    } );
    

    btw my only requirement is to save data as serialized array when name attribute set as array for a non array type field and also want to fix textarea_name option for WYSIWYG field, in your documents it clearly stated that for WYGIWYG field
    'textarea_name' => $editor_id, // set the textarea name to something different, square brackets [] can be used here
    but when you will use square brackets it won’t work that’s why I wanted that changed into the function set_field_defaults_wysiwyg to avoid overwrites during updates.

    And ofcourse I am trying to achieve complex thing with your plugin and I have achieved it too but to do that I have to manually save fields using hook.

    Thread Starter Vikas Khunteta

    (@vikas_khunteta)

    @tw2113 Hi Michael,

    I know you are not interested into this complex setup but can you please make changes into set_field_defaults_wysiwyg function as stated above to avoid overwrites during updates, the changes I want is not just specific to my code but it would fix the WYSIWYG field populate as repeater field.

    Please have a look at this, you will understand why I urge you to make this change.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    The thing is that we need to make sure we’re not going to be breaking anything for anyone else using the library as well, which is probably going to be more than our number on our repo page here since it can be bundled with other plugins or themes.

    We have https://github.com/CMB2/CMB2/ where the development work happens if you want to try your hand at a Pull Request for review and consideration. We also haven’t had a lot of active work on the library lately so you could always modify yourself for your own needs in the meantime or fork it on GitHub so that you could easily pull in new changes as they show up.

    Thread Starter Vikas Khunteta

    (@vikas_khunteta)

    Thanks, I will try.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘WYSIWYG Field Options Setting Issue’ is closed to new replies.