adding user list dropdown in customize theme - php

$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

Related

Woocommerce: populating a billing address field from functions.php

I have added a custom field to the Woocommerce create new order page (admin) with this code:
function rt_woocommerce_admin_billing_fields( $fields ) {
$fields['billing_email'] = array(
'label' => __( 'Email' ),
'show' => true,
'class' => 'short',
);
return $fields;
}
add_filter('woocommerce_admin_billing_fields', 'rt_woocommerce_admin_billing_fields');
My question is how do I then populate this new field when the page loads? I want to populate it from a URL token using
$email = get_query_var('email');
But how do I insert that value into the new field so it is visible to the admin while creating the order?
To give a value to the field, change the $field to:
$email = get_query_var('email');
$fields['billing_email'] = array(
'label' => __( 'Email' ),
'value' => $email,
'show' => true,
'class' => 'short',
);

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();
}

CMB2 option-page parameter

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.

Dynamically add key value pair to options property of woocommerce checkout fields filter

I am adding some custom fields to woocommerce checkout using woocommerce_checkout_fields filter. One of those fields is a select dropdown. This is my code for the fields.
// Add a new checkout field
function ds_filter_checkout_fields($fields){
$suburb = ds_get_delivery_suburbs();
$postcodes = ds_get_delivery_postcodes();
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
),
'select_field' => array(
'type' => 'select',
'options' => array('key' => 'value'),
'required' => true,
'label' => __( 'Another field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'ds_filter_checkout_fields' );
If you check the select_field code there is a options property and it takes key and value pair ... I want to insert dynamic key and dynamic value to options property ... In the code I am getting the dynamic key from $postcodes and dynamic value from $suburb and when I try to insert it like this 'options' => array($postcodes => $suburb), I get this warning Warning: Illegal offset type ... I have tried couple of other methods but they didn't work so I turned to you guys ... I appreciate your help ... looking forward to your responses.
NOTE: I have googled this but haven't found any answers so that is why I turned to Stackoverflow for help.
If you want it to be dynamic you have to first set and fill the array and then use it, so it would be
// Add a new checkout field
function ds_filter_checkout_fields($fields){
$suburb = ds_get_delivery_suburbs();
$postcodes = ds_get_delivery_postcodes();
$ma_options[$postcodes] = $suburb;
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
),
'select_field' => array(
'type' => 'select',
'options' => $ma_options[$postcodes],
'required' => true,
'label' => __( 'Another field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'ds_filter_checkout_fields' );
If it doesn't work then share var_dump($ma_options);
--- UPDATE
That could happen because of index not existing or passing something which isn't an string as the key. You should cast your type to string if desired efect, or use stdClass object.

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') ),)
) );

Categories