• Resolved mlyczko

    (@mlyczko)


    Hi

    Is there any option to disable CMB2 for some view, ex. single post?
    I’ve tried:

    add_action( 'init', 'changeActions' );
    function changeActions () {
    	if(is_single()){
    		remove_action('init', array( 'CMB2_Bootstrap_260', 'include_cmb' ), 9964);
    	}
    }

    but with no luck.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Justin Sternberg

    (@jtsternberg)

    Yes, that’s what the show_on_cb parameter is for. You can see an example in the example file: https://github.com/CMB2/CMB2/blob/develop/example-functions.php#L117
    and https://github.com/CMB2/CMB2/blob/develop/example-functions.php#L24-L37

    Here’s several more snippets/examples: https://github.com/CMB2/CMB2-Snippet-Library/tree/master/conditional-display

    Thread Starter mlyczko

    (@mlyczko)

    Hi

    Thank you for your answer.
    I didn’t describe my problem precisely.
    I have a custom plugin that uses CMB2 as a part of this plugin because I’ve made some core changes (showing images for radio list).
    When I have my custom plugin and CMB2 plugin enabled my core changes are not visible on the frontend – CMB2 plugin has a higher priority than my custom plugin with overridden CMB2 included.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    It’s a very bad idea to hack the core of CMB2 for the same reason it’s a bad idea to hack WordPress core. In fact, you are running up against one of the reasons it’s a bad idea. If you submit the hacked code you are using, I can probably show you how to accomplish that with a custom field type instead, a much safer and recommended approach.

    Thread Starter mlyczko

    (@mlyczko)

    Hi

    Thank you for a quick response.
    Please see code between tags // DJ – BEGIN and // DJ – END

    File CMB2_Type_Multi_Base.php:

    <?php
    /**
     * CMB Multi base field type
     *
     * @since  2.2.2
     *
     * @category  WordPress_Plugin
     * @package   CMB2
     * @author    CMB2 team
     * @license   GPL-2.0+
     * @link      https://cmb2.io
     */
    abstract class CMB2_Type_Multi_Base extends CMB2_Type_Base {
    
    	/**
    	 * Generates html for an option element
    	 *
    	 * @since  1.1.0
    	 * @param  array $args Arguments array containing value, label, and checked boolean
    	 * @return string       Generated option element html
    	 */
    	public function select_option( $args = array() ) {
    		return sprintf( "\t" . '<option value="%s" %s>%s</option>', $args['value'], selected( isset( $args['checked'] ) && $args['checked'], true, false ), $args['label'] ) . "\n";
    	}
    
    	/**
    	 * Generates html for list item with input
    	 *
    	 * @since  1.1.0
    	 * @param  array $args Override arguments
    	 * @param  int   $i    Iterator value
    	 * @return string       Gnerated list item html
    	 */
    	public function list_input( $args = array(), $i ) {
    		$a = $this->parse_args( 'list_input', array(
    			'type'  => 'radio',
    			'class' => 'cmb2-option',
    			'name'  => $this->_name(),
    			'id'    => $this->_id( $i ),
    			'value' => $this->field->escaped_value(),
    			'label' => '',
    		), $args );
    		// DJ - BEGIN
    		if($a['name'] == '_tc_category'){
    			$taxonomy = 'ad_category';
    			$terms = get_terms($taxonomy, array("hide_empty" => false)); // Get all terms of a taxonomy
    			foreach ( $terms as $term ) {
    				$cat = get_term_by('slug', $a['value'], $taxonomy);
    				$image_url = '';
    				$image = '';
    				$is_parent = '';
    				if(!empty($cat)){
    					$cat_id = $cat->term_id;
    					$level1 = $cat->parent;
    					$image_url = get_term_meta( $cat_id, '_tc_cat_image', true );
    					if(!empty($image_url) && $level1 == 0){
    						$image = '<img src="'.$image_url.'" alt="'.$a['label'].'" />';
    					} else {
    						$image = '';
    					}
    					
    					if(get_term_children( $cat_id, $taxonomy )){
    						$is_parent = 'class="parent"';
    						return sprintf( "\t" . '<li %s><span><input%s/><label for="%s">%s<span>%s</span></label></span>' . "\n", $is_parent, $this->concat_attrs( $a, array( 'label' ) ), $a['id'], $image, $a['label']);
    					} else {
    						return sprintf( "\t" . '<li %s><span><input%s/><label for="%s">%s<span>%s</span></label></span></li>' . "\n", $is_parent, $this->concat_attrs( $a, array( 'label' ) ), $a['id'], $image, $a['label']);
    					}
    				}
    			}
    		} else {
    			return sprintf( "\t" . '<li><input%s/> <label for="%s">%s</label></li>' . "\n", $this->concat_attrs( $a, array( 'label' ) ), $a['id'], $a['label'] );
    		}
    		
    		// DJ - END
    	}
    
    	/**
    	 * Generates html for list item with checkbox input
    	 *
    	 * @since  1.1.0
    	 * @param  array $args Override arguments
    	 * @param  int   $i    Iterator value
    	 * @return string       Gnerated list item html
    	 */
    	public function list_input_checkbox( $args, $i ) {
    		$saved_value = $this->field->escaped_value();
    		if ( is_array( $saved_value ) && in_array( $args['value'], $saved_value ) ) {
    			$args['checked'] = 'checked';
    		}
    		$args['type'] = 'checkbox';
    		return $this->list_input( $args, $i );
    	}
    
    	/**
    	 * Generates html for concatenated items
    	 *
    	 * @since  1.1.0
    	 * @param  array $args Optional arguments
    	 * @return string        Concatenated html items
    	 */
    	public function concat_items( $args = array() ) {
    		$field = $this->field;
    
    		$method = isset( $args['method'] ) ? $args['method'] : 'select_option';
    		unset( $args['method'] );
    
    		$value = null !== $field->escaped_value()
    			? $field->escaped_value()
    			: $field->get_default();
    
    		$value = CMB2_Utils::normalize_if_numeric( $value );
    
    		$concatenated_items = '';
    		$i = 1;
    
    		$options = array();
    		if ( $option_none = $field->args( 'show_option_none' ) ) {
    			$options[''] = $option_none;
    		}
    		$options = $options + (array) $field->options();
    		foreach ( $options as $opt_value => $opt_label ) {
    
    			// Clone args & modify for just this item
    			$a = $args;
    
    			$a['value'] = $opt_value;
    			$a['label'] = $opt_label;
    
    			// Check if this option is the value of the input
    			if ( $value === CMB2_Utils::normalize_if_numeric( $opt_value ) ) {
    				$a['checked'] = 'checked';
    			}
    
    			$concatenated_items .= $this->$method( $a, $i++ );
    		}
    
    		return $concatenated_items;
    	}
    
    }

    File CMB2_Type_Taxonomy_Base.php:

    <?php
    /**
     * CMB Taxonomy base field type
     *
     * @since  2.2.2
     *
     * @category  WordPress_Plugin
     * @package   CMB2
     * @author    CMB2 team
     * @license   GPL-2.0+
     * @link      https://cmb2.io
     */
    abstract class CMB2_Type_Taxonomy_Base extends CMB2_Type_Multi_Base {
    
    	/**
    	 * Checks if we can get a post object, and if so, uses <code>get_the_terms</code> which utilizes caching.
    	 *
    	 * @since  1.0.2
    	 * @return mixed Array of terms on success
    	 */
    	public function get_object_terms() {
    		switch ( $this->field->object_type ) {
    			case 'options-page':
    			case 'term':
    				return $this->options_terms();
    			case 'post':
    				// WP caches internally so it's better to use
    				return get_the_terms( $this->field->object_id, $this->field->args( 'taxonomy' ) );
    
    			default:
    				return $this->non_post_object_terms();
    		}
    	}
    
    	/**
    	 * Gets the term objects for the terms stored via options boxes.
    	 *
    	 * @since  2.2.4
    	 * @return mixed Array of terms on success
    	 */
    	public function options_terms() {
    		if ( empty( $this->field->value ) ) {
    			return array();
    		}
    
    		$terms = (array) $this->field->value;
    
    		foreach ( $terms as $index => $term ) {
    			$terms[ $index ] = get_term_by( 'slug', $term, $this->field->args( 'taxonomy' ) );
    		}
    
    		return $terms;
    	}
    
    	/**
    	 * For non-post objects, wraps the call to wp_get_object_terms with transient caching.
    	 *
    	 * @since  2.2.4
    	 * @return mixed Array of terms on success
    	 */
    	public function non_post_object_terms() {
    		$object_id = $this->field->object_id;
    		$taxonomy = $this->field->args( 'taxonomy' );
    
    		$cache_key = "cmb-cache-{$taxonomy}-{$object_id}";
    
    		// Check cache
    		$cached = get_transient( $cache_key );
    
    		if ( ! $cached ) {
    			$cached = wp_get_object_terms( $object_id, $taxonomy );
    			// Do our own (minimal) caching. Long enough for a page-load.
    			set_transient( $cache_key, $cached, 60 );
    		}
    
    		return $cached;
    	}
    
    	/**
    	 * Wrapper for <code>get_terms</code> to account for changes in WP 4.6 where taxonomy is expected
    	 * as part of the arguments.
    	 *
    	 * @since  2.2.2
    	 * @return mixed Array of terms on success
    	 */
    	public function get_terms() {
    		return CMB2_Utils::wp_at_least( '4.5.0' )
    			? get_terms( wp_parse_args( $this->field->prop( 'query_args', array() ), array(
    				'taxonomy' => $this->field->args( 'taxonomy' ),
    				'hide_empty' => false,
    			) ) )
    			: get_terms( $this->field->args( 'taxonomy' ), 'hide_empty=0' );
    	}
    
    	protected function no_terms_result( $error, $tag = 'li' ) {
    		if ( is_wp_error( $error ) ) {
    			$message = $error->get_error_message();
    			$data = 'data-error="' . esc_attr( $error->get_error_code() ) . '"';
    		} else {
    			$message = $this->_text( 'no_terms_text', esc_html__( 'No terms', 'cmb2' ) );
    			$data = '';
    		}
    
    		$this->field->args['select_all_button'] = false;
    
    		return sprintf( '<%3$s><label %1$s>%2$s</label></%3$s>', $data, esc_html( $message ), $tag );
    	}
    
    	public function get_object_term_or_default() {
    		$saved_terms = $this->get_object_terms();
    
    		return is_wp_error( $saved_terms ) || empty( $saved_terms )
    			? $this->field->get_default()
    			: array_shift( $saved_terms )->slug;
    	}
    
    	/**
    	 * Takes a list of all tax terms and outputs.
    	 *
    	 * @since  2.2.5
    	 *
    	 * @param  array  $all_terms   Array of all terms.
    	 * @param  array|string $saved Array of terms set to the object, or single term slug.
    	 *
    	 * @return string              List of terms.
    	 */
    	protected function loop_terms( $all_terms, $saved_terms ) {
    		return '';
    	}
    
    	/**
    	 * Build children hierarchy.
    	 *
    	 * @param  object       $parent_term The parent term object.
    	 * @param  array|string $saved       Array of terms set to the object, or single term slug.
    	 *
    	 * @return string                    List of terms.
    	 */
    	protected function build_children( $parent_term, $saved ) {
    		if ( empty( $parent_term->term_id ) ) {
    			return '';
    		}
    
    		$this->parent = $parent_term->term_id;
    
    		$terms   = $this->get_terms();
    		$options = '';
    
    		if ( ! empty( $terms ) && is_array( $terms ) ) {
    			// DJ - BEGIN
    			$options = '<ul class="cmb2-indented-hierarchy">';
    			$options .= $this->loop_terms( $terms, $saved );
    			$options .= '</ul>';
    			// DJ - END
    		}
    
    		return $options;
    	}
    
    }
    Plugin Author Justin Sternberg

    (@jtsternberg)

    Ok, and can you please provide the field/box configuration you are using which uses this?

    Thread Starter mlyczko

    (@mlyczko)

    Hi

    Please take a look for these fields:

    		$cmb->add_field( array(
    			'name'       => __( 'Category *', 'example' ), 
    			'id'   => $prefix . 'category',
    			'type'           => 'taxonomy_radio_hierarchical',
    			'taxonomy'       => 'ad_category', // Enter Taxonomy Slug
    			'render_row_cb' => 'override_category_field_callback'
    		) );
    		$cmb->add_field( array(
    			'name'           => __( 'Location', 'example' ),
    			'id'   => $prefix . 'locations',
    			'taxonomy'       => 'ad_location',
    			'type'           => 'taxonomy_radio_hierarchical',
    			'remove_default' => 'true',
    			'show_option_none' => false,
    		) );
    function override_category_field_callback( $field_args, $field ) {
    
    	// If field is requesting to not be shown on the front-end
    	if ( ! is_admin() && ! $field->args( 'on_front' ) ) {
    		return;
    	}
    
    	// If field is requesting to be conditionally shown
    	if ( ! $field->should_show() ) {
    		return;
    	}
    
    	$field->peform_param_callback( 'before_row' );
    
    	// Remove the cmb-row class
    	printf( '<div class="cmb-row %s">', $field->row_classes() );
    
    	if ( ! $field->args( 'show_names' ) ) {
    	
    		// If the field is NOT going to show a label output this
    		echo '<div class="cmb-td custom-label-class">';
    		$field->peform_param_callback( 'label_cb' );
    	
    	} else {
    
    		// Otherwise output something different
    		if ( $field->get_param_callback_result( 'label_cb', false ) ) {
    			echo '<div class="cmb-th">', esc_attr($field->peform_param_callback( 'label_cb' )), '</div>';
    		}
    		echo '<div class="cmb-td"><span class="tcf-categories-breadcrumb"></span><span class="tcf-add-category-done btn button">'.__( 'Use selected category', 'example' ).'</span><span class="tcf-change-category-button btn button">'.__( 'Change category', 'example' ).'</span><span class="tcf-add-category-button btn button">'.__( 'Choose  category', 'example' ).'</span>';
    	}
    
    	$field->peform_param_callback( 'before' );
    	
    	// The next two lines are key. This is what actually renders the input field
    	$field_type = new CMB2_Types( $field );
    	$field_type->render();
    
    	$field->peform_param_callback( 'after' );
    
    	echo '</div>';
    	echo '</div>';
    
    	$field->peform_param_callback( 'after_row' );
    
        // For chaining
    	return $field;
    }
    • This reply was modified 5 years, 1 month ago by mlyczko.
    Plugin Author Justin Sternberg

    (@jtsternberg)

    Ok, i’ve pushed up an example custom field type for you: https://github.com/CMB2/CMB2-Snippet-Library/tree/master/custom-field-types/taxonomy-radio-with-image-field-type

    To use it, you would do something like this:

    
    require_once dirname( __FILE__ ) . '/custom-field-types/taxonomy-radio-with-image-field-type/taxonomy-radio-with-image-field-type.php';
    
    $cmb_demo->add_field( array(
    	'name'     => __( 'Test Taxonomy Radio', 'cmb2' ),
    	'id'       => '_yourprefix_demo_text_taxonomy_radio',
    	'type'     => 'taxonomy_radio_with_image',
    	'taxonomy' => 'category', // Taxonomy Slug
    	'term_image_field_id' => 'yourprefix_category_avatar',
    ) );
    
    Thread Starter mlyczko

    (@mlyczko)

    Hi

    Thank you very much.
    There is only one issue: https://github.com/CMB2/CMB2-Snippet-Library/compare/master…mlyczko:patch-1

    Your support is excellent – better than for commercial plugins ??

    Plugin Author Justin Sternberg

    (@jtsternberg)

    Updated, thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Disable CMB2 on some view’ is closed to new replies.