CMB2-Snippet-Library icon indicating copy to clipboard operation
CMB2-Snippet-Library copied to clipboard

Default values not applicable in theme option page

Open Mnshawaty opened this issue 3 years ago • 10 comments
trafficstars

I made an theme option page And i have a sample to change the colour of background as like your example But the default values are not applied it need to open option page and click on save button tobe applied help me please

This is your example

Mnshawaty avatar Jun 30 '22 08:06 Mnshawaty

I think that you should change $default parameter to an array of default values. Then, when the code is first run myprefix_get_option will return a valid value.

Maybe: function myprefix_get_option( $key = '', $default = array('test_text'=>'Default Text', 'test_colorpicker'=>'#bada55') ) {

damiencarbery avatar Jun 30 '22 10:06 damiencarbery

Can you provide your attempted code so that we can see what may be going wrong, if it's not presently an exact copy of the example you're linking to?

Also are you referring to defaults at the point of visiting the options page for the first time and seeing some values filled in automatically? or are you expecting those when on the frontend and outputting the fetched options?

That said, regarding the term "default" and what's used in new_cmb2_box() sections, that's meant to be just for initially populating the fields, not what's saved to the database automatically. Upon first visit to the options page, nothing is written to the database in the chosen option key

Also the snippets are meant to be starting points rather than things you make use of out of box, so if you want to change that default parameter in some way, you can.

tw2113 avatar Jun 30 '22 15:06 tw2113

hi thank you all but i still have the problem sorry buy i am beginer on php

my code is this https://textdoc.co/MxZivujd9Ta8EqkR :(

Mnshawaty avatar Jun 30 '22 21:06 Mnshawaty

@Mnshawaty can you clarify at which point things are failing? Are the default values, upon say loading the options page the first time, and then clicking save, not actually saving to the option in the database and options table? Is it failing to apply styles of some sort upon fetching the option and output?

tw2113 avatar Jun 30 '22 22:06 tw2113

hi guys when i want to return a default value from option page the value returned number 1

<?php echo background_get_option( 'background_options', 'test_colorpicker', $default ) ; ?> how i can return the real default value

Mnshawaty avatar Feb 21 '23 18:02 Mnshawaty

Can you share what you're seeing in your option row from wp_options ? It's possible things are somehow saving as numeral 1.

tw2113 avatar Feb 21 '23 18:02 tw2113

this is my option page code :


<?php

add_action( 'cmb2_admin_init', 'background_register_theme_options_metabox' );

function background_register_theme_options_metabox() {
	$cmb_options = new_cmb2_box( array(
		'id'           => 'background_option_metabox',
		'title'        => esc_html__( 'Site Options', 'background' ),
		'object_types' => array( 'options-page' ),
		// 'save_button'     => esc_html__( 'Save Theme Options', 'background' ), // The text for the options-page save button. Defaults to 'Save'.
	) );


	$cmb_options->add_field( array(
		'name'    => __( 'Test Color Picker', 'background' ),
		'desc'    => __( 'field description (optional)', 'background' ),
		'id'      => 'test_colorpicker',
		'type'    => 'colorpicker',
		'default' => '#bada55',
	) );

}

function background_get_option( $key = '', $default = true ) {
	if ( function_exists( 'cmb2_get_option' ) ) {
		// Use cmb2_get_option as it passes through some key filters.
		return cmb2_get_option( 'background_options', $key, $default );
	}

	// Fallback to get_option if CMB2 is not loaded yet.
	$opts = get_option( 'background_options', $default );

	$val = $default;

	if ( 'all' == $key ) {
		$val = $opts;
	} elseif ( is_array( $opts ) && array_key_exists( $key, $opts ) && true !== $opts[ $key ] ) {
		$val = $opts[ $key ];
	}

	return $val;
}


in front end the resault will be "1"

but i want it to be "#bada55"

now i cant access the database to show you row wp_options but i can tell you that i test this on localhost and on a live website and the resault same

Mnshawaty avatar Feb 21 '23 19:02 Mnshawaty

If I'm reading everything right, function backgroundq_get_option( $key = '', $default = true ) {} would be using a boolean value if, when you're using backgroundq_get_option( 'test_colorpicker' ) without the 2nd option.

Also note that your example from earlier, shown below, is passing 3 parameters for a function that only accepts 2:

<?php echo background_get_option( 'background_options', 'test_colorpicker', $default ) ;  ?> 

You'd want to amend it to

<?php echo background_get_option( 'test_colorpicker', $default );  ?>

tw2113 avatar Feb 21 '23 19:02 tw2113

i tested your code tips and its alose print 1 but i discoverd something that there is no value stored even the default

as i see that default is not saved in database by default its just be like a placeholder in the page i did a test to check that :

` <?php if(background_get_option('test_colorpicker')){ 

echo 'its has a value' ;


}else{
echo 'it doesn'thas a value' ;

?>

`

and the resault is it doesn'thas a value

so the default value is stored in input as a placeholder text only you need to press on save button to store it in database i need to store it automaticly

Mnshawaty avatar Feb 21 '23 20:02 Mnshawaty

Outside of hardcoding a default when invoking, like echo background_get_option( 'test_colorpicker', '#ff0000' ), perhaps something like using add_option()` would work. Otherwise, there's just going to be some needed extra code to create the option values saved automatically for you.

Likely could be done within the background_register_theme_options_metabox() callback that you're using.

tw2113 avatar Feb 21 '23 20:02 tw2113