I am trying to display the selected options from my functioning CMB2 multicheck metabox. Currently this is returned on the front-end:
check1check2
I am trying to return:
Open Ceiling Drop Ceiling
What am I missing? I could do this in a different way, but it's become a bit of a vendetta for me. Any help is greatly appreciated!
My metabox is here and appears in the appropriate page edit area:
$zf_ind_boxes->add_field( array(
'name' => esc_html__( 'Fan Types', 'cmb2' ),
'desc' => esc_html__( 'For Wassup content. Check all that apply.', 'cmb2' ),
'id' => $prefix . 'ind_fan_types',
'type' => 'multicheck',
'label_cb' => 'get_the_labels',
'options' => array(
'check1' => 'Open Ceiling',
'check2' => 'Drop Ceiling',
'check3' => 'Spot Cooling',
)
) );
In my template I have:
$array = get_post_meta($metafield_id, 'zf_ind_fan_types', true);
if($array) {
foreach ($array as $key => $value){
echo $value;
}
}
If you want display checked values at front-end just replace your section with this one below.
'options' => array(
'Open Ceiling' => 'Open Ceiling', //key => value
'Drop Ceiling' => 'Drop Ceiling', //key => value
'Spot Cooling' => 'Spot Cooling', //key => value
)
Value is hardcoded - so it isn't saved in database, in database is saved only key
Related
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();
}
I am trying to show a form based on a variable in the url. This is my array:
$blocks = array(
'oc1' => array(
'slugid' => 'oc1',
'title' => 'One Column 1',
'desc' => 'Block with text',
'values' => array(
'textarea',
'title'
)
),
'oc2' => array(
'slugid' => 'oc2',
'title' => 'One Column 2',
'desc' => 'Block with button',
'values' => array(
'title'
)
)
);
Now I want to show form fields based on the values array. So if my url is test.php?b=oc1 it should show the textarea field. If test.php?b=oc2 it should not because textarea is not added to the values array.
I've tried a lot of answers I found on StackOverflow but I can't get it to work.
So if anyone knows how to do this I would be very very grateful.
Check if is defined the $_GET variable (if you have not done it before) and using the in_array function check if textarea value exist in your two-dimensional array.
if (isset($_GET['b']) && in_array('textarea', $blocks[$_GET['b']]['values']))
{
echo 'textarea';
}
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.
What I am trying to achieve is create a jQuery Tabs markup from PHP Array. Basic intention is to add tabs and content dynamically. Both existing StackExchange answers and Google Search left me empty handed here. Finding it pretty dificult with my level of PHP knowledge.
Here are one of the many code that I have tried:
My Array:
$args = array();
$args[] = array(
'name' => 'Shortcode Name',
'tabname' => 'Shortcode Tab Name', //Same As Tab 4
'action' => 'shortcodeaction',
'icon' => 'codeicon',
'image' => 'codeimage',
);
$args[] = array(
'name' => 'Shortcode2 Name',
'tabname' => 'Shortcode2 Tab Name',
'action' => 'shortcodeaction2',
'icon' => 'codeicon2',
'image' => 'codeimage2',
);
$args[] = array(
'name' => 'Shortcode3 Name',
'tabname' => 'Shortcode3 Tab Name',
'action' => 'shortcodeaction3',
'icon' => 'codeicon3',
'image' => 'codeimage3',
);
$args[] = array(
'name' => 'Shortcode4 Name',
'tabname' => 'Shortcode Tab Name', //Same As Tab 1
'action' => 'shortcodeaction4',
'icon' => 'codeicon4',
'image' => 'codeimage4',
);
$args[] = array(
'name' => 'Shortcode5 Name',
'tabname' => 'Shortcode5 Tab Name',
'action' => 'shortcodeaction5',
'icon' => 'codeicon5',
'image' => 'codeimage5',
);
The PHP Code:
echo '<ul>';
foreach ( $args as $arg ) {
echo '<li>'.$arg['tabname'].'</li>';
}
echo </ul>
foreach ( $args as $arg ) {
echo '<div id="' . preg_replace("/[^a-z0-9]+/", "", $arg['tabname'] ) . '"></div>';
}
The tabname key value for Tab 1 and Tab 4 are same, so they should go under same tab name and tab content. This is exactly where I am getting lost. I know I can do a foreach loop to create tabs, but those two are not going under same tab instead they are creating new tab with same name.
Used this to get the count for array with unique value for tabname:
$lists = wp_list_pluck( $args, 'tabname' );
$counts = count( array_unique( $lists ));
// 4
Now a foreach loop will create 3 tabs, but not sure how to pass the $args inside the loops.
I am not asking you to do this for me, but a little head start would be great.
Thanks
I have been looking how to do this and am a bit stumped.
My array is as follows:
$returndata->setup_array = array(
'General' => array(
'Main Details' => 'setup/maindets',
'Directories' => 'directories',
'Extension Allocation' => 'xtnallo',
'List Holidays' => 'setup/holidays',
'List Group VM' => 'groupvm',
'Conference Rooms' => 'confroom'
),
'Offices' => array(
'List Offices' => 'iptoffices'
),
'Users' => array(
'List Users' => 'iptusers'
),
'Phones' => array(
'List Phones' => 'iptphones'
),
);
However I have 1 item that on a certain condition(triggered by the users session) that needs to be added to the listin the general array. The section being 'View Details => setup/viewdetails'. I have tried array push (probably incorrectly) but this adds the item as another array at the end under the main array.
I want/need it to work like this:
$returndata->setup_array = array(
'General' => array(
$viewdets
'Main Details' => 'setup/maindets',
'Directories' => 'directories',
'Extension Allocation' => 'xtnallo',
'List Holidays' => 'setup/holidays',
'List Group VM' => 'groupvm',
'Conference Rooms' => 'confroom'
),
'Offices' => array(
'List Offices' => 'iptoffices'
),
'Users' => array(
'List Users' => 'iptusers'
),
'Phones' => array(
'List Phones' => 'iptphones'
),
);
$viewdets = "'View Details' => 'setup/viewdetails'";
and still be interpreted as a functioning array for use as a menu.
$returndata->setup_array['General']['View Details'] = 'setup/viewdetails'
Cheers Rick!
You can use ArrayObject to have the array as a reference:
$a = new ArrayObject();
$b = array(
"a" => $a
);
$a[] = "foo";
print_r($b);
What did you try calling array_push() on? Have you tried
array_push($returndata->setup_array['General'], $viewdets);
You would need to add the variable to the specific depth of the array you wanted it to be present. check out array_push here, there's also a short language syntax that avoids the function call:
$returndata->setup_array['General'][] = $viewdets;