The Settings API is a convenient way to add administrative options. Tutorials on the settings API hook register_setting to admin_init. But what if you also want to programmatically change the same options outside of the Settings API?
For example, use the Settings API to maintain a base value for option X. In addition, each new post automatically increments option X via a direct update_option function call. Unfortunately, if option X is registered, update_option triggers a call to the validation function associated with the registered setting. The validation fails because the update_option argument isn't passed because I screw up the different validation required from the different sources of options change.
Where should register_setting for option X be hooked to allow option X also to be updated outside of the Settings API? Alternatively, where should unregister_setting for option X be hooked to allow the direct update_option call to work without the Settings API sanitization/validation function call?
Update: t31os below helpfully provided some illustrative code. Here is a small addition to his code:
function __construct() {
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action('add_meta_boxes', array( &$this, 'add_regtest_box' )); //added hook
}
//added functions
function add_regtest_box() {
global $post;
add_meta_box( 'regtest_box', 'Registered Setting Test', array( &$this, 'regtest_box_contents'), 'post' , 'side' , 'high' );
}
function regtest_box_contents() {
update_option( 'test-option', array('fieldone'=>-11,'fieldtwo'=>8) );
$new_vals = get_option( 'test-option' );
print_r( $new_vals );
}
If you "add new post" from the dashboard, you'll notice that the metabox on the top right shows fieldone with a value of 11, not -11. That's because the update_option function call in regtest_box_contents is putting the option values through the sanitizing/validating function that was registered (register_setting) to the option. In this case, that's t31os's update_option function, which applies absint() to the option value passed through. The question is how to avoid that function call, e.g. just have the option updated to -11.