How to Change the 'Add Row' Button Text for CMB2 Repeatable Fields
Content
If you've used CMB2's powerful repeatable fields, you might have wanted to change the default 'Add New' button text to something more specific to your project. This is a common request, and the method for doing so has evolved over time.
The Problem
By default, CMB2 labels the button for adding a new row in a repeatable field as 'Add New'. For a field collecting email addresses, you might prefer 'Add Another Email'. For a long time, changing this text was not consistently supported across all repeatable field types, leading to confusion.
The Solution
The recommended way to change this text is to use the 'text' parameter within your field configuration array. This parameter was introduced to provide a standardized method for overriding various button and description strings.
Here is an example for a repeatable text field:
$cmb->add_field( array(
'name' => 'Email Addresses',
'id' => '_email_id',
'type' => 'text',
'repeatable' => true,
'text' => array(
'add_row_text' => 'Add Another Email' // This changes the button text
)
) );
This method is now the standard and should work for all repeatable field types, including select, text, and others.
Important Note on Legacy Code (The 'options' Array)
You may encounter older code snippets or tutorials that suggest placing the 'add_row_text' override inside an 'options' array. This was a previous method that worked for some field types but was inconsistent. For future compatibility and clarity, it is best to use the new 'text' parameter.
The CMB2 team maintains a comprehensive wiki with field parameters, which is the best place to find the most up-to-date information on functionality like this.
Related Support Threads Support
-
Set Field values from database with add_group_fieldhttps://wordpress.org/support/topic/set-field-values-from-database-table-but-not-metadata/
-
Two fields in same row in back endhttps://wordpress.org/support/topic/two-fields-in-same-row-in-back-end/
-
How to display field name front end?https://wordpress.org/support/topic/how-to-display-field-name-front-end/
-
Change button label of a repeatable fieldhttps://wordpress.org/support/topic/change-button-label-of-a-repeatable-field/
-
READ ONLY FIELDS ON FRONT ENDhttps://wordpress.org/support/topic/read-only-fields-on-front-end-2/
-
Problem with custom fieldhttps://wordpress.org/support/topic/problem-with-custom-field-4/
-
repeatable adds another row on savehttps://wordpress.org/support/topic/repeatable-adds-another-row-on-save/
-
Repeatable Fields Exampleshttps://wordpress.org/support/topic/repeatable-fields-examples/
-
Cannon make “render_row_cb” workhttps://wordpress.org/support/topic/cannon-make-render_row_cb-work/
-
Custom field type vs Repeatable attrhttps://wordpress.org/support/topic/custom-field-type-vs-repeatable-attr/
-
How to use this as a multi-column display. Any sample code?https://wordpress.org/support/topic/repeatable-group-sames/
-
Custom repetable fields appear six times each and won’t savehttps://wordpress.org/support/topic/custom-repetable-fields-appear-six-times-each-and-wont-save/
-
Repeatable Field Text – New line button labelhttps://wordpress.org/support/topic/repeatable-field-text-new-line-button-label/
-
User fill in field valuehttps://wordpress.org/support/topic/user-fill-in-field-value/
-
CMB2 as an Options page – repeatable fields not workinghttps://wordpress.org/support/topic/cmb2-as-an-options-page-repeatable-fields-not-working/
-
BUG Editing repeateble field delete text valuehttps://wordpress.org/support/topic/editing-repeateble-field-delete-text-value/
-
Using update_post_meta to add url to repeatable fieldhttps://wordpress.org/support/topic/using-update_post_meta-to-add-url-to-repeatable-field/
-
update_post_meta for repeatable fieldhttps://wordpress.org/support/topic/update_post_meta-for-repeatable-field/
-
“after” for all fields of type “wysiwyg”https://wordpress.org/support/topic/after-for-all-fields-of-type-wysiwyg/
-
Custom repeatable field type missing data when updatehttps://wordpress.org/support/topic/custom-repeatable-field-type-missing-data-when-update/
-
Field type text with repeatable true – Lost data on post updatehttps://wordpress.org/support/topic/field-type-text-with-repeatable-true-lost-data-on-post-update/
-
Ability to save as single custom field with array of data?https://wordpress.org/support/topic/ability-to-save-as-single-custom-field-with-array-of-data/
-
Save repeatable fields separatelyhttps://wordpress.org/support/topic/save-repeatable-fields-separately/
-
How to calculate with Inputshttps://wordpress.org/support/topic/how-to-calculate-with-inputs/
-
Static text fieldhttps://wordpress.org/support/topic/static-text-field/
-
Repeating fieldshttps://wordpress.org/support/topic/repeating-fields-5/
-
Nested repeater field in CMB2https://wordpress.org/support/topic/nested-repeater-field-in-cmb2/
-
Columns within row?https://wordpress.org/support/topic/columns-within-row/
-
How to create repeatable blocks (not custom fields)https://wordpress.org/support/topic/how-to-create-repeatable-blocks-not-custom-fields/
-
READ ONLY FIELDShttps://wordpress.org/support/topic/read-only-fields-2/