Trigger custom action when setting button pressed

You need a second form with admin_url(‘admin-post.php’) as form action. Then you can hook into admin_post_custom_action to execute your action. Sample code: add_action( ‘admin_post_wpse_79898’, ‘wpse_79898_test’ ); function wpse_79898_test() { if ( isset ( $_GET[‘test’] ) ) echo esc_html( $_GET[‘test’] ); die( __FUNCTION__ ); } In your settings page: <form action=”<?php echo admin_url( ‘admin-post.php’ ); ?>”> … Read more

Run function AFTER theme options are saved?

Use the filter update_option_{$option}. It runs after a successful saving. $option is the name of your option, and you get the old and the new value as parameters. From wp-includes/option.php: do_action( “update_option_{$option}”, $oldvalue, $_newvalue ); Use it like this for an option wpse_themesettings: add_action( ‘update_option_wpse_themesettings’, ‘wpse_check_settings’, 10, 2 ); function wpse_check_settings( $old_value, $new_value ) { … Read more

Custom Post Type Settings page, choose page to display archive

There is a dirty (actually dirty as hell) way to attach a ordinary WordPress page as archive page from a custom post type settings page. You need to unregister post type first, and create again. If you don’t use custom arguments or if you brave enough try this code. Add this code fragment end of … Read more

What are the advantages to the Settings API?

My point of view is that main purpose and benefit of Settings API is structure. It helps to keep complex settings setups: orderly (logic of registration and sections); secure (nonces, validation callbacks); extensible (hooking into another page or allowing to be hooked into). As with any such structural overhead it benefits more complex use cases … Read more

How to pass variable to add_settings_section() callback?

if you look at the do_settings_sections function more specifically the line 1164 where the callback function is being executed : call_user_func($section[‘callback’], $section); you can see that the $section array is being passed to the callback function, so you can identify the callback by the $section[‘id’] hope this make since. Update here is an example, if … Read more

Is get_option function cached?

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 of … Read more

How to pass arguments from add_settings_field() to the callback function?

Look at the declaration for the function: function add_settings_field( $id, $title, $callback, $page, $section = ‘default’, $args = array() ) { } The last parameter takes your arguments and passes them to the callback function. Example from my plugin Public Contact Data foreach ($this->fields as $type => $desc) { $handle = $this->option_name . “_$type”; $args … Read more

“Error: Options Page Not Found” on Settings Page Submission for an OOP Plugin

“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 … Read more