To avoid adding post meta values one at a time (and reduce repeat code in my theme) I wrote a simple class to store all the options so I can iterate through. The class works fine and constructs an object that I'm able to add valid options to (in much the same format one adds controls to the Customizer screen), and output the options forms in the meta box, but the values won't save. Here's how I'm trying to save the options, with some notes after. Please let me know if you have any ideas or need more info:
add_action( 'save_post', 'save_theme_options' );
function save_theme_options( $post_id ) {
global $theme_options;
foreach ( $theme_options->options as $option ) {
if ( array_key_exists( $option->id, $_POST ) ) {
update_post_meta($post_id, $option->meta_key, $_POST[$option->id]);
}
}
}
$theme_options is the global variable where I instantiate my object.
The $id of each option within $options requires a program-friendly string, such as "example_option". (It's not a number, like in WP objects.)
So with a single option set up like:
$theme_options->add_theme_option( 'example_text', array(
'title' => 'Substrate Text Example',
'description' => 'Example Description',
'label' => 'Text:',
'type' => 'text',
'default' => 'Some text'
));
I expect $id to be 'example_text' and $meta_key to be '_example_text'.
Please let me know if you need more info, and thanks. (First post here.)
get_my_theme_options()
. Most likely your global is not initialized at the time of save. – Mark Kaplun Mar 15 '18 at 5:14metakey
defined in your options block (the second code block)... unless you are implying it is generated from the id? – majick Mar 15 '18 at 11:12