Skip to content

Instantly share code, notes, and snippets.

@jtsternberg
Last active June 8, 2023 16:53
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jtsternberg/8601075 to your computer and use it in GitHub Desktop.
Save jtsternberg/8601075 to your computer and use it in GitHub Desktop.
Create a theme options page with CMB2
@rogercoathup
Copy link

You are missing a bracket around line 110 to close the 'fields' array.

@BernardMyburgh
Copy link

How do you actually echo out the options in your template files? I know about echo get_post_meta($post->ID, '_cmb_so_and_so', true); but not about how to echo the actual options, a company logo, a colour selected, etc. I could not find this in the wiki? Thank you though, this theme options page works like a charm, like a charm I tell you.

@purwa-astawa
Copy link

@BernardMyburgh
from the wiki, use '' to echo the theme options.

@sureshamk
Copy link

repeatable is not working

@wpbean
Copy link

wpbean commented Jun 24, 2014

Text field type default value not echo until I set a value and save it. Any solution?

@RJdeJong
Copy link

RJdeJong commented Jul 8, 2014

The option page add's backslashes to single and double quotes every time you save, any way around this?

@EnekoBarrero
Copy link

Where should I put this code?

@heddesheimer
Copy link

When I used this example, I found that all my quotes are escaped with a backslash ' probably because magic_quotes kicks in here. The strange thing is, that I haven't seen this, when I use the custom fields in a custom post type, instead of an option page. (probably the same problem that RJdeJong posted here a few days ago).

My quick workaround for this was to define a custom sanitization for the textarea fields that I use in the option page and put a stripslashes() here.

@jtsternberg
Copy link
Author

If it's happening on save, custom sanitization callback may be needed. If only happening on display, you'll want a custom escaping callback.

@manwithpowers
Copy link

Line 143 should be 'cmb2_get_option' in order for this to work with CMB2.

@amitrahav
Copy link

taxonomy_multicheck working but not in admin display - its always show all the checkboxes checked

@mauricenaef
Copy link

Is there a hook to display a message when the metabox gets saved?

@owldesign
Copy link

Is there a way to use this to create multiple options/sections? Basically I want to have "Social Links", "Homepage Layout", etc as sections that have their own custom fields..

Like any other Theme Options panels that come with themes these days.

@thatryan
Copy link

Can repeatable groups work in here? I cant get it to...

@jtsternberg
Copy link
Author

@amitrahav taxonomy_multicheck is for setting terms on posts/users. It shouldn't be used for an admin page. @mauricenaef you can use the 'admin_notices' hook. @owldesign you can use the 'title' field type to highlight sections. @thatryan yes, should work the same.

@clifgriffin
Copy link

Important to note: the key that makes CMB2 save the data is line 104: https://gist.github.com/jtsternberg/8601075#file-theme-options-cmb-php-L104

show_on must be an array and key must be set to "options-page". This was not obvious to me as I deconstructed this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment