• Resolved Mr.Meerkat

    (@mrmeerkat)


    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:

    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 1 year, 6 months ago by Mr.Meerkat.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    The only thing standing out to me is this part:

    '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

    (@mrmeerkat)

    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 :

    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)

    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 1 year, 6 months ago by Mr.Meerkat.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Set pattern attribute depending on option’ is closed to new replies.