CMB2 option-page parameter - php

CMB2 has an option to use as an option page.
I'm looking in the example files, and on the wiki page but even copying and pasting the example on the files it not work.
I'm probably missing something, but I can't find what it is, I already spent two days trying to make this work.
Following the wiki and the example I modified to this code
add_action( 'cmb2_admin_init', 'yourprefix_register_theme_options_metabox' );
function yourprefix_register_theme_options_metabox() {
$option_key = 'wherever';
$cmb = new_cmb2_box( array(
'id'=> $option_key . '_theme_options-page',
'object_types' => array( 'options-page' ),
'hookup' => false,
'menu_title' => 'Site Options',
'parent_slug' => 'tools.php',
'capability' => 'manage_options'
) );
$cmb->add_field( array(
'name' => 'Site Background Color',
'desc' => 'field description',
'id' => 'bg_color',
'type' => 'colorpicker',
'default' => '#ffffff'
) );
}
Any leads on why it's not working?

Currently the documentation for CMB2's options page capabilities just takes you to their Snippet Library which isn't 100% straightforward, so hopefully I can help clarify how to use these functions properly.
First, the metaboxes you register in cmb2_admin_init can generate an entire admin page. Take this code example straight from the snippet library for instance:
add_action('cmb2_admin_init', 'register_my_admin_page');
function register_my_admin_page() {
/**
* Registers options page menu item and form.
*/
$cmb_options = new_cmb2_box( array(
'id' => 'myprefix_option_metabox',
'title' => esc_html__( 'Site Options', 'myprefix' ),
'object_types' => array( 'options-page' ),
/*
* The following parameters are specific to the options-page box
* Several of these parameters are passed along to add_menu_page()/add_submenu_page().
*/
'option_key' => 'myprefix_options', // The option key and admin menu page slug.
// 'icon_url' => 'dashicons-palmtree', // Menu icon. Only applicable if 'parent_slug' is left empty.
// 'menu_title' => esc_html__( 'Options', 'myprefix' ), // Falls back to 'title' (above).
// 'parent_slug' => 'themes.php', // Make options page a submenu item of the themes menu.
// 'capability' => 'manage_options', // Cap required to view options-page.
// 'position' => 1, // Menu position. Only applicable if 'parent_slug' is left empty.
// 'admin_menu_hook' => 'network_admin_menu', // 'network_admin_menu' to add network-level options page.
// 'display_cb' => false, // Override the options-page form output (CMB2_Hookup::options_page_output()).
// 'save_button' => esc_html__( 'Save Theme Options', 'myprefix' ), // The text for the options-page save button. Defaults to 'Save'.
) );
/*
* Options fields ids only need
* to be unique within this box.
* Prefix is not needed.
*/
$cmb_options->add_field( array(
'name' => __( 'Test Text', 'myprefix' ),
'desc' => __( 'field description (optional)', 'myprefix' ),
'id' => 'test_text',
'type' => 'text',
'default' => 'Default Text',
) );
$cmb_options->add_field( array(
'name' => __( 'Test Color Picker', 'myprefix' ),
'desc' => __( 'field description (optional)', 'myprefix' ),
'id' => 'test_colorpicker',
'type' => 'colorpicker',
'default' => '#bada55',
) );
}
This code snippet will generate a top-level admin page named "Site Options" with two fields: a text field and a color-picker field, complete with a title, form fields, submit button, etc. You can configure how the page is displayed to the user using the commented out settings on the new_cmb2_box function.
When the form is saved, it will save the meta box and its fields to the site option myprefix_options. So if you call the function get_option('myprefix_options'), it will return the following array:
array(
'myprefix_option_metabox' => array(
'test_text' => '' // value of the Test Text field,
'test_colorpicker' => '' // value of the Test Color Picker field
)
)
I hope that helps clarify things a bit.

Related

adding user list dropdown in customize theme

$wp_customize->add_control( new WP_Customize_Control(
$wp_customize, //Pass the $wp_customize object (required)
'user_theme_name', //Set a unique ID for the control
array(
'label' => __( 'Select Theme Name', 'user' ), //Admin-visible name of the control
'description' => __( 'Using this option you can change the user' ),
'settings' => 'bootstrap_theme_name', //Which setting to load and manipulate (serialized is okay)
'priority' => 10, //Determines the order this control appears in for the specified section
'section' => 'user_options', //ID of the section this control should render in (can be one of yours, or a WordPress default section)
'type' => 'select',
'choices' => array(
'default' => 'admin',
'cerulean' => 'Cerulean',
'cosmo' => 'Cosmo',
'cyborg' => 'cyborg',
)
)
) );
I already created a dropdown menu in a customized theme but I can't get the user list and put it in the dropdown menu. I try to add this hook $users = get_users( array( 'fields' => array( 'ID' ) ) ); and replace the choices in array object the $user but it did not show. i just want to get all user and put in on dropdown.
i need to display all users in dropdown
screenshot of the customize theme
i find the solution. I just add this hook to get the user in array format
$users = get_users();
$user_names = wp_list_pluck( $users,'display_name' );
and change the 'choices' value to choices => $user_names

Check value from Gravity Forms API

I am creating a custom Gravity Forms add-on and it appears to work so far. The settings are showing and saving as expected.
Here's what I have:
public function plugin_settings_fields() {
return array(
array(
'title' => esc_html__( 'Animal Types', 'animaltypes' ),
'fields' => array(
array(
'name' => 'gravity_forms_animal_types',
'type' => 'checkbox',
'label' => esc_html__( 'Animal Types', 'animaltypes' ),
'choices' => array(
array(
'label' => esc_html__( 'Cat', 'animaltypes' ),
'name' => 'option_cat',
'default_value' => 0,
),
array(
'label' => esc_html__( 'Dogs', 'animaltypes' ),
'name' => 'option_dog',
'default_value' => 0,
)
)
),
)
)
);
}
But what I can't figure out is how to check, for example, if option_cat has been set so that I can then run a custom function if it is.
So essentially (and I know the below code is not correct) something like this:
if(option_cat == true) {
my_cat_function();
}
In gravity forms when you create a new addon you provide a slug for that addon. Gravity forms save that settings with the help of that slug.
So if you want to get that settings you can use below code.
$settings = get_option('gravityformsaddon_slugname__settings');
$option = rgar( $settings, 'gravity_forms_animal_types' );
In options you can get selection of your settings, and if you want to one selection at a time you must use radio button instead of checkbox.
It was quite simple after all.
$options = get_option('gravityformsaddon_animaltypes_settings');
$cat = $options['option_cat'];
if($cat) {
my_cat_function();
}

Need to print variable in textfield

Im working on a WordPress-site where a reservationform is installed.
I want the name of the logged on user is the defaultvalue in the "name" textfield since only loggen on users can make reservations.
The php-code which builds the form looks like this (originial):
// Contact details fieldset
'contact' => array(
'legend' => __( 'Contact Details', 'restaurant-reservations' ),
'fields' => array(
'name' => array(
'title' => __( 'Firmenname', 'restaurant-reservations' ),
'request_input' => empty( $request->name ) ? '***VALUE***' : $request->name,
'callback' => 'rtb_print_form_text_field',
'required' => true,
),
This continues with other value like Mail and Phonenumber.
I need to get the value from the $user_login - variable printed in the textfield. I tried entering the variable itself, declare it, extending a string and tried to execute php-code. But nothing of this works out.
Is there any other way for printing out the variable in a textfield?
Please dont mind asking futher questions as i cant explain it that detailed.
Greets
Assuming you are using a user plugin you can just call the current users details and output the user name in the text field
$current_user = wp_get_current_user();
// Contact details fieldset
'contact' => array(
'legend' => __( 'Contact Details', 'restaurant-reservations' ),
'fields' => array(
'name' => array(
'title' => __( 'Firmenname', 'restaurant-reservations' ),
'request_input' => $current_user->user_login,
'callback' => 'rtb_print_form_text_field',
'required' => true,
),
That should do the trick

Genesis Start Theme remove services post type

I'm trying to remove the services post type in the Start Genesis child theme. The Start theme comes bundled with a services post type. I have a page with the URL -- http://domain.com/services -- but when I try to the view the page on this url, I am greeted with a 404 not found yet I know this page exists and has content.
Now for SEO reasons this is the best URL for this page so changing it is not an option.
To my question, is there a way to remove the services post type in the Start theme?
Thanks
Try this..
function custom_unregister_theme_post_types() {
global $wp_post_types;
if ( isset( $wp_post_types[ 'services' ] ) ) {
unset( $wp_post_types[ 'services' ] );
}
}
add_action( 'init', 'custom_unregister_theme_post_types', 20 );
Note : Please make a back up of your database before trying.
For anyone having the same issue, response from the theme author regarding the "services" post type
There's a custom post type "Services" and /services/ url will load the archive page of services post type which conflicts with your page.
If you don't use services post type, you can remove that in zp_cpt.php file ( the file is in /include/cpt/ folder ).
In the file, remove or comment out this code
$services_custom_default = array('supports' => array( 'title', 'editor','thumbnail', 'revisions' ),'menu_icon' => get_stylesheet_directory_uri().'/include/cpt/images/portfolio.png',);
$services = new Super_Custom_Post_Type( 'services', 'Service', 'Services', $services_custom_default );
$services->add_meta_box( array('id' => 'services_settings','context' => 'normal','fields' => array('icon_type' => array( 'type' => 'select', 'options' => array('font-awesome' => 'Font-Awesome','glyphicons' => 'Glyphicons', 'image' => 'Image' ), 'data-zp_desc' => __( 'Select icons to use. Font-Awesome, Glyphicons or an Image.','start') ),'icon_class' => array( 'type' => 'text','data-zp_desc' => __( 'Add icon classes. For font-awesome classes, please refer to this link page. For Glyphicons, refer to this page ','start') ),'icon_link' => array( 'type' => 'text', 'data-zp_desc' => __( 'Service item link','start') ),'icon_target' => array( 'type' => 'select', 'options' => array('_blank' => '_blank','_self' => '_self', '_parent' => '_parent' ), 'data-zp_desc' => __( 'Target','start') ),)
) );

Showing a description under input field on WooCommerce Checkout Page

Scenario - We're working on a particular WooCommerce site that is meant for Charities only. So on the Checkout page, we'd like to change "Company Name" to "Charity Name"
We've manage to to that with the following code. -
add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_fields' );
function custom_woocommerce_billing_fields( $fields ) {
$fields['billing_company'] = array(
'label' => __('Charity Name', 'woothemes'),
'placeholder' => __('Charity Name', 'woothemes'),
'required' => true,
'class' => array('billing-company'),
);
return $fields;
}
This changes the field name alright, however we require a description to show up below the Label that reads - "Please enter the full name of your Charity"
We tried adding the line
'description' => __( 'Please enter the full name of your Charity', 'woothemes' )
However, it doesn't seem to work or show up on the page or even in the code.
Could someone please guide me with the same.
Thank you in advance.
This isn't well documented, but these are the default fields for the woocommerce_form_field() function, which is where the array is passed, so you can determine the possible keys. It is found in includes/wc-template-functions.php:
$defaults = array(
'type' => 'text',
'label' => '',
'placeholder' => '',
'maxlength' => false,
'required' => false,
'class' => array(),
'label_class' => array(),
'input_class' => array(),
'return' => false,
'options' => array(),
'custom_attributes' => array(),
'validate' => array(),
'default' => '',
);
There isn't a way in this method to append your own HTML, but you could hack it in using the custom_attributes (save your text description) and input_class (tell jQuery to use this to append to the DOM) options.
Add CSS class and custom attribute to hold the values for jQuery
add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_fields' );
function custom_woocommerce_billing_fields( $fields ) {
$fields['billing_company'] = array(
'label' => __('Charity Name', 'woothemes'),
'placeholder' => __('Charity Name', 'woothemes'),
'required' => true,
'class' => array('billing-company'),
'custom_attributes' = array( 'item-description'=>'YOUR DESCRIPTION' ),
'input_class' = array( 'append-description' ),
);
return $fields;
}
Use jQuery to use your CSS class as a selector and grab the description
jQuery('.append-description').each(function(){
var item = jQuery(this);
var description = item.attr('item-description');
item.parent().append('<div>'+description+'</div>');
});
Please note the above code is untested and likely doesn't work, use as an example for your implementation.
Description is now available. You can add one and it will show as a span="description" underneath.

Categories