Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have default values for options for a WordPress Plugin?

Or how can I populate wordpress database with default plugin values, add option doesnt works for me, like http://codex.wordpress.org/Function_Reference/add_option

I am sure my syntax is correct, what I want is, when the user goes to the settings menu of my plugin, he/she doesn't see the blank input field, rather see it filled with default data.

like image 617
WebDDelhi Avatar asked Jun 27 '11 19:06

WebDDelhi


People also ask

How do I get option value in WordPress?

When adding options like this: add_option( 'my_option_name', 'value' ) and then retrieving them with get_option( 'my_option_name' ) , the returned values will be: false returns string(0) "" true returns string(1) "1" 0 returns string(1) "0"

Where are my WordPress plugins options stored?

The text, metadata, and settings are stored in the WordPress database. Static files like images, JS, CSS used by the plugin are stored in the plugins directory. The users' files uploaded when working with the plugin are stored in the plugin-specific folder inside the wp-content/uploads directory.

How do I change the default plugin in WordPress?

So, to create this «By default enabled and loading plugins» you have to create a folder inside /wp-content/plugins/ named «mu-plugins» and put inside that folder every plugin you want WordPress to load ALWAYS by default.

How do I change default settings in WordPress?

To change the default category so site visitors can better understand the content of your blog posts, go to Posts > Categories to create a new category. Next, go to Settings > Writing and choose the new category you created from the Default Post Category dropdown menu.


2 Answers

Just add the default values of your option(s) as the second parameter of the get_option() function call(s). Unless the option does not exists, this value will be returned then.

like image 62
hakre Avatar answered Sep 28 '22 03:09

hakre


http://codex.wordpress.org/Function_Reference/add_option is a safest way to register option and also set a default value. It will only work if that option does not already exists in the options.

like image 38
thevikas Avatar answered Sep 28 '22 01:09

thevikas