How Do I Create a New Option in WordPress?

If you want to create a new option in WordPress, you first need to create a new folder in your WordPress site’s root directory. Within this new folder, create a new file called option.php and paste the following code into it:

name = ‘new option'; $option->description = ‘A new option for your WordPress site'; $option->default = false; $option->show_on_front = true; $option->show_on_home = true; $option->show_in_menu = true; $option->show_in_nav_menu = true; $option->show_in_search_menu = true; $option->save_on_exit = true; // create new option table $wp_options_table = new WP_Options_Table(); $wp_options_table->add_option( $option ); // get option value $option_value = $wp_options_table->get_option( $option->id ); // store option value in database $option_value = esc_attr( $option_value ); // update option table $wp_options_table->update( $option_value ); // return the option value return $option_value;

Now, you’ll need to create a function to handle the save and update actions on the option. To do this, create a file called option_save.php and paste the following code into it:

get_option( $option->id ); // store option value in database $option_value = esc_attr( $option_value ); // update option table $wp_options_table->update( $option_value ); } // update option function function update_option() { // get option value $option_value = $wp_options_table->get_option( $option->id ); // if the option value is not present, then create it if (!isset($option_value)) { $option_value = new WP_Option(); $option_value->name = ‘new option'; $option_value->description = ‘A new option for your WordPress site'; $option_value->default = false; $option_value->show_on_front = true; $option_value->show_on_home = true; $option_value->show_in_menu = true; $option_value->show_in_nav_menu = true; $option_value->show_in_search_menu = true; $option_value->save_on_exit = true; $wp_options_table->add_option( $option_value ); } // return the option value return $option_value; }

Now, you’ll need to create a function to handle the delete action on the option. To do this, create a file called option_delete.php and paste the following code into it:

get_option( $option->id ); // if the option value is not present, then delete it if (!isset($option_value)) { $option_value = new WP_Option(); $option_value->name = ‘new option'; $option_value->description = ‘A new option for your WordPress site'; $option_value->default = false; $option_value->show_on_front = true; $option_value->show_on_home = true; $option_value->show_in_menu = true; $option_value->show_in_nav_menu = true; $option_value->show_in_search_menu = true; $option_value->delete_on_exit = true; $wp_options_table->add_option( $option_value ); } // return the option value return $option_value; }

Now, you’ll need to create a function to handle the get action on the option. To do this, create a file called option_get.php and paste the following code into it:

get_option( $option->id ); // if the option value is not present, then return the empty string if (!isset($option_value)) { $option_value = new WP_Option(); $option_value->.