Hot answers tagged

39

Well the second bit of code is technically the correct way to do it. However, at the end of the add_settings_field() you can pass arguements. Please view the WordPress Add_Settings_Field function reference. This will help you in getting the best understanding of how the add_settings_field() function really works. Now, with that said, you could use a '...


29

When in doubt, look at the source code. Digging in to get_option(), you'll see (abbreviated): $value = wp_cache_get( $option, 'options' ); if ( false === $value ) { $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); // Has to be get_row instead of get_var because ...


24

The unfortunate situation is that there are indeed two different options: Newer timezone_string, which saves PHP–style time zone. Older gmt_offset, which saves numeric float offset in hours. But in newer environments timezone_string actually overrides gmt_offset, the value returned by the latter will be based on the former. However the opposite isn't true —...


21

"Error: Options Page Not Found" Bug This is a known issue in the WP Settings API. There was a ticket opened years ago, and it was marked as solved -- but the bug persists in the latest versions of WordPress. This is what the (now removed) Codex page said about this: The "Error: options page not found." problem (including a solution and explanation): ...


20

The answer that yes, the theme_mod functions will be slower, but not significantly, and the benefits outweigh the differences. Theme mods are stored as options. So, in essence, the theme_mod functions are wrappers around the options functions. First, understand that theme_mod settings are stored as an array in a single option, keyed to the specific theme ...


20

WP-CLI is definitely the answer to this after the update to 1.4.0 which introduced the pluck and patch commands for accessing serialized data in WordPress. The pluck command takes this format for grabbing serialized values wp option pluck <key> <key-name> For example in the active_plugins option you can grab first item wp option pluck ...


19

To get WordPress admin email simply do: $admin_email = get_option('admin_email');


17

Solved it myself, this is how to do it: If you want to programatically update serialized options: download wp-cli from wp-cli.org Find out what is the "key" for the options you are using. In this example the key is "wp_smtp_options" If you don't know the key, search in wp_options table and try to figure it out. Example: select * from wp_options where ...


17

Today I run a test over my db to explore the speed difference between accessing a key from options, custom table & transients. I ran the test for 1000 times and following is the time taken to run 1000 get operations: Keep in mind that the options table is used for both options and transients on most systems, and that table has been optimised, with ...


14

I used: DELETE FROM `wp_options` WHERE `option_name` LIKE ('%\_transient\_%'); to cleanup with great results :) (from here https://stackoverflow.com/questions/10422574/can-i-remove-transients-in-the-wp-options-table-of-my-wordpress-install)


14

Here is how I do it, beware, post is extensive. /* Add Menus -----------------------------------------------------------------*/ add_action('admin_menu', 'ch_essentials_admin'); function ch_essentials_admin() { /* Base Menu */ add_menu_page( 'Essentials Theme', 'Essentials Theme', 'manage_options', 'ch-essentials-options', '...


14

I just had the same problem but fixed it quickly! :-) In my case, my homepage was on draft. Apparently, the settings then can't find the homepage anymore. It thinks it's gone and disables the option to set a default homepage. So turn on your homepage to visible. (also name it "Home") Hope it works for you! Cheers, Koen


14

There are two defines in wp-config.php of WordPress website: define('DB_CHARSET', 'utf8'); define('DB_COLLATE', ''); There are several things which are most commonly misunderstood. Names of constants in those defines, might suggest that they are related to the database itself. They are not. They are related to tables within the database. ...


12

I don't recommend to use if (!(get_option("XXXX")) approach to check for existence, as it fails with false/null/0/empty legitimate value. I suggest: if (!option_exists("XXXX")) { add_option("XXXX", "valueee"); } code: public function option_exists($name, $site_wide=false){ global $wpdb; return $wpdb->query(...


12

It's impossible to help without having a look at your code. As I am one of the people who worked on WordPress 4.5.3, I can assure you that it's a usual maintenance and security release. There were no database-related changes between these versions except for #36748. Citing the commit message here: Database: dbDelta() will no longer try to downgrade the ...


11

You can use wp_add_inline_style() to add to a stylesheet that you've already defined, such as in your plugin. This way an options screen or other user settings can affect the final style output. That could become very tedious, however, depending on how many changes you are giving the user the power over. However, it is the "best practice" as far as I know.


10

No, this won't work: <?php echo get_option('notice_data[Message]'); ?> Because get_option pulls whole option value by option_name, it doesn't pull by pieces of the serialized array. What you are asking for is a key (option_name) called literally notice_data[Message]. Assuming you've saved the option as notice_data you aren't going to get a match, and ...


10

There is no absolutely safe way to store such information permanently. You have two options to increase security a little bit: Use the options table and encrypt the data Use a strong encryption method, and bind it to either: your password when you want to use the API call only when you are logged in, or a secret key stored in your wp-config.php – then an ...


10

Thanks to a tip from milo in the comments above, I looked at this similar question. There is an answer provided by Laurent which basically gets the option using wp-cli, pipes it to an inline php program, makes the adjustments and then pipes it back to wp-cli. I took that idea and generalized it somewhat by creating a file sibling to my cloning script: ...


8

Pull the add_action() outside of the function, and put the conditional inside the callback. Also, if you're printing a script directly, use wp_print_scripts instead of wp_head. You also have a syntax error. function testingone(){ if( get_option( 'MyOptionName' ) ) { ?> <script>var Script = GoesHere; </script> <?...


8

The culprit is: <?php settings_errors(); ?> It isn't needed and will produce a second "Settings saved." notification if there are no errors. Edited for formatting.


8

You are using get_the_content() wrong, it can't take a ID, which is what get_option('page_for_posts') does return, and generally gets the content of the current post inside the loop, in which it has to be used. To get the actual content of that page you can do: $page_for_posts_id = get_option( 'page_for_posts' ); $page_for_posts_obj = get_post( $...


8

It depends on how you are going to use the stored data. If you want to run complex queries against the values, use a custom table with indexes optimized for those queries. If you will always just fetch all the values for a given object, create a non-public custom post type, and store the data as post meta. Do not store the data in a serialized string, ...


7

I am satisfied with Nikolay Yordanov's answer. Just generalizing the solution. Yes, we can update WordPress options programmatically. WordPress saves options in wp_options table. wp_options holds two rows option_name and option_value to store key and value respectively. We need right option_name and a way to save value in wp_options table. As we know, we ...


7

You could delete an existing cache for your option before you call get_option(): $GLOBALS['wp_object_cache']->delete( 'your_option_name', 'options' ); $value = get_option( 'your_option_name' );


7

The two functions output exactly the same thing. From the Codex entry for get_bloginfo(): 'name' - Returns the "Site Title" set in Settings > General. This data is retrieved from the "blogname" record in the wp_options table. From source: case 'name': default: $output = get_option('blogname'); Neither get_bloginfo() nor bloginfo() do any sort of ...


7

The best place to flush rewrite rules is on plugin activation/deactivation. function myplugin_activate() { // register taxonomies/post types here flush_rewrite_rules(); } register_activation_hook( __FILE__, 'myplugin_activate' ); function myplugin_deactivate() { flush_rewrite_rules(); } register_deactivation_hook( __FILE__, '...


7

I just found this post while looking for the same issue. The solution is much simpler than it looks because the documentation is misleading : in register_setting() the first argument named $option_group is your page slug, not the section in which you want to display the setting. In the code above you should use // Update Settings ...


7

As the codex says, it is legacy with probably no use in any modern version. I have been around wordpress since 2.0.4 and I don't remember it being used or referenced so my guess is that it is a really old legacy thing.


7

Here is how I do it in a bash script: wp option get wp_smtp_options --format=json | php -r ' $var = json_decode( fgets(STDIN) ); $var->from = "email@email.com"; $var->fromname = "me"; print json_encode($var); ' | wp option set wp_smtp_options --format=json


Only top voted, non community-wiki answers of a minimum length are eligible