Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default values not applicable in theme option page #111

Open
Mnshawaty opened this issue Jun 30, 2022 · 10 comments
Open

Default values not applicable in theme option page #111

Mnshawaty opened this issue Jun 30, 2022 · 10 comments

Comments

@Mnshawaty
Copy link

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

@damiencarbery
Copy link

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') ) {

@tw2113
Copy link
Contributor

tw2113 commented Jun 30, 2022

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.

@Mnshawaty
Copy link
Author

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 :(

@tw2113
Copy link
Contributor

tw2113 commented Jun 30, 2022

@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?

@Mnshawaty
Copy link
Author

Mnshawaty commented Feb 21, 2023

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

@tw2113
Copy link
Contributor

tw2113 commented Feb 21, 2023

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

@Mnshawaty
Copy link
Author

Mnshawaty commented Feb 21, 2023

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

@tw2113
Copy link
Contributor

tw2113 commented Feb 21, 2023

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 );  ?>

@Mnshawaty
Copy link
Author

Mnshawaty commented Feb 21, 2023

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

@tw2113
Copy link
Contributor

tw2113 commented Feb 21, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants