Title: Set pattern attribute depending on option
Last modified: May 16, 2023

---

# Set pattern attribute depending on option

 *  Resolved [Mr.Meerkat](https://wordpress.org/support/users/mrmeerkat/)
 * (@mrmeerkat)
 * [3 years ago](https://wordpress.org/support/topic/set-pattern-attribute-depending-on-option/)
 * Hi there,
 * I have a plugin-option ‘numbering’, that can be set to ‘numeric’ or ‘alphanumeric’
   and i like to set the value of the pattern attribute depending on the value this
   plugin option has been set to. Is that even possible? Here is one of my futile
   attempts:
 *     ```wp-block-code
       public function register_metabox() {
         ...
         $mb->add_field( array(
           'name'       => __( 'Number', 'digipano' ),
           'id'         => $prefix . 'tree_number',
           'type'       => 'text',
           'attributes' => array(
             'pattern' => 'pattern_cb',
           ),
         ) );
   
         function pattern_cb() {
           $settings  = get_option( 'my-settings' );
           $numbering = $settings['numbering'];
           switch ( $numbering ) {
             case 'numeric':
               return '[1-9]\d*';
             case 'alphanumeric':
               return '[A-Z]{1}[1-9]\d*';
             default: 
               return '.*';
           }
         }
       ...
       }
       ```
   
 * The code above is inside a class and there is a namespace, just for the case,
   that is important here.
 * Thanks in advance!
    -  This topic was modified 3 years ago by [Mr.Meerkat](https://wordpress.org/support/users/mrmeerkat/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [3 years ago](https://wordpress.org/support/topic/set-pattern-attribute-depending-on-option/#post-16741876)
 * The only thing standing out to me is this part:
 *     ```wp-block-code
       'attributes' => array(
             'pattern' => 'pattern_cb',
       ),
       ```
   
 * I don’t think this particular spot is set up to handle callback functions like
   that and like in other areas that I know custom callbacks can be provided.
 *  Thread Starter [Mr.Meerkat](https://wordpress.org/support/users/mrmeerkat/)
 * (@mrmeerkat)
 * [3 years ago](https://wordpress.org/support/topic/set-pattern-attribute-depending-on-option/#post-16742628)
 * Thanks!
 * Probably a bit naive to think that this callback magic that works for some fields
   also works for attributes as well.
 * I finally found a solution by passing the options as a javaScript-Object and 
   then using jQuery to set the attribute to the appropriate value. J ust for the
   case it helps someone else:
 * PHP:
 * Enqueue a custom admin script and transmitting the options as a JS-Object via
   wp_localize_script :
 *     ```wp-block-code
       class MyClass {
   
       public function init() {
         add_action(
           'admin_enqueue_scripts',
           array( $this, 'enqueue_admin' ) );
       }
   
   
       public function enqueue_admin() {
         wp_enqueue_script( 
           'my-admin-script',
           plugins_url( '/js/admin.js', MY_PLUGIN_FILE ), 
           array( 'jquery' ), 
           '1.0.0',
           true );
   
         $my_object = array(
            ... 
            'options' => get_option( 'my-settings' ),
         );
   
         wp_localize_script( 
            'my-admin-script', 
            'myObject', 
            $my_object 
         );
   
         ...
       }
       ...
       }
   
       ( new MyClass() )->init();
       ```
   
 * JS: (plugindir/js/admin.js)
 *     ```wp-block-code
       jQuery( function( $ ) {
          if ( $( 'body.post-my-post-type' ).length ) {
       	let numbering = myObject.options.numbering;
       	let input = $( 'input#my_cmb2_id' );
   
       	if ( 'numeric' === numbering ) {
       	  input.attr( 'pattern', '[1-9][0-9]*' );
       	}
   
       	if ( 'alphanumeric' === numbering ) {
       	  input.attr( 'pattern', '[A-Z]{1}[1-9][0-9]*');
       	}
          }
       });
       ```
   
    -  This reply was modified 3 years ago by [Mr.Meerkat](https://wordpress.org/support/users/mrmeerkat/).

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Set pattern attribute depending on option’ is closed to new replies.

 * ![](https://ps.w.org/cmb2/assets/icon.svg?rev=2866672)
 * [CMB2](https://wordpress.org/plugins/cmb2/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/cmb2/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/cmb2/)
 * [Active Topics](https://wordpress.org/support/plugin/cmb2/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/cmb2/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/cmb2/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Mr.Meerkat](https://wordpress.org/support/users/mrmeerkat/)
 * Last activity: [3 years ago](https://wordpress.org/support/topic/set-pattern-attribute-depending-on-option/#post-16742628)
 * Status: resolved