Cmb2: Additional after_label hook

Created on 22 Aug 2015  路  12Comments  路  Source: CMB2/CMB2

Hey - I would like to add tooltips to our labels like so: https://github.com/WordImpress/google-maps-builder/issues/85

Currently the only way I can do this is with jQuery looping through each label and appending a span tag within the labels. Would you be open to adding a hook for us to add content/html before and after labels?

All 12 comments

@DevinWalker That's a good thought. The 744968d is a step, but ideally we would have more WordPress style hooks in there. I've been mulling it around in my head, and haven't quite determined what the best option is.

The easiest (for us users of CMB2) would be to add after_label and before_label strings to the array, like so:

    $cmb->add_field( array(
        'name'       => __( 'Test Text', 'cmb2' ),
        'desc'       => __( 'field description (optional)', 'cmb2' ),
        'id'         => $prefix . 'text',
        'type'       => 'text',
        'after_label'  => '<span class="tip"><i class="fa fa-info-circle"></i>This is info about this setting or field</span>',
    ) );

@mathetos that's the exact functionality I could use.

@jtsternberg would you be interested in a PR adding this functionality?

Wouldn't this accomplish the same thing a little more elegantly?

$cmb_demo->add_field( array(
    'name' => __( 'Test Text Area', 'cmb2' ),
    'id'   => $prefix . 'textarea',
    'type' => 'textarea',
    'label_cb' => 'render_custom_callback',
    'options' => array(
        'tooltip-class' => 'fa-info-circle',
        'tooltip'       => 'This is info about this setting or field',
    ),
) );

With a callback like:

function render_custom_callback( $field_args, $field ) {
    // Get default label
    if ( $label = $field->label() && $field->option( 'tooltip' ) ) {
        // If label and tooltip exists, add it
        $label .= sprintf( '<span class="tip"><i class="fa %s"></i>%s</span>', $field->option( 'tooltip-class' ), $field->option( 'tooltip' ) );
    }

    return $label;
}

@jtsternberg tip of the hat to you sir. That looks very clean.

Keep in mind, the 'label_cb' only exists in trunk at the moment, but will be in the next release.

Woot! Nice to see this added. Thanks!

The example provided doesn't seem to work anymore. Tried using the example on a options page.

@pgroot91 are you referring to the code in this comment? https://github.com/CMB2/CMB2/issues/435#issuecomment-135174595

@pgroot91 you are right, I had some bugs in my sample code. I have updated to work properly in the example in the wiki: https://github.com/CMB2/CMB2/wiki/Tips-&-Tricks#modify-field-label-output

@tw2113 @jtsternberg thanks guys for the clarification and the provided update/fix. 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

noquierouser picture noquierouser  路  8Comments

bomsn picture bomsn  路  5Comments

jflagarde picture jflagarde  路  6Comments

DevinWalker picture DevinWalker  路  4Comments

jquimera picture jquimera  路  8Comments