:edit:
To clarify what I'm trying to do: I need access to form data submitted to options.php so that I can preprocess it into a specific data format. Then, depending on one of the fields, I either append that data format into an array which is itself the value of a created database option, or I create a new database option identified by the value of said field, with a newly created array as the value of said new option, and append the newly formatted form data to that option.
:end-edit:
I'm roughly following the official documentation on setting up a custom settings page here..
However, instead of trying to update my plugin settings from this, I want to use the submitted form data to create a new option and use the data for that new option to create and populate a new custom post type.
When I say create new option, I don't necessarily mean a 100% new option. I could update the data for the option that's already being updated. The important thing is that I am trying to create some way of storing multiple instances of the submitted form data, and associating each instance with some other object somewhere.
This is currently what I have for the function that generates the code for the form in the settings page:
function form_submit_options_page_html(){ if(!current_user_can('manage_options')){ return; } if(isset( $_GET['settings-updated'])){ add_settings_error('form_submit', 'data_saved', __('Data Saved', 'form_submit'), 'Data Saved.'); if(isset( $_POST['event_title'])){ echo "<p>" . esc_attr($_POST['event_title']) . "</p>"; } else { echo "<p>Event title not found. Hm.</p>"; } } settings_errors('form_submit'); ?> <div class="wrap"> <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> <form action="options.php" method="post"> <?php // output security fields for the registered setting "wporg" settings_fields( 'form_submit' ); // output setting sections and their fields // (sections are registered for "wporg", each field is registered to a specific section) do_settings_sections( 'form_submit' ); // output save settings button submit_button( 'Save Settings' ); ?> </form> </div> <?php } When I submit the form on the actual settings page, I do get the proper "Data Saved." message from the add_settings_error message. However, if I then check for any posted data in the $_POST super global, the data doesn't appear to be set.
The original documentation code has the form action set to options.php. I tried changing it to admin_post.php, but then, I can't find in the documentation anywhere if the code in the tutorial properly generates the hidden input field. I tried creating the hidden input field myself using an add_settings_field method:
add_settings_field('fs_action', __('Hidden:', 'form_submit'), 'fs_event_form_action_field_cb', 'form_submit', 'form_submit_events', array( 'name' => 'action', 'class' => 'form_submit_row', 'type' => 'hidden', 'value' => 'process_new_event' )); But the problem is that somehow still generates a <label> with the value "Hidden:" in it and a hidden input field. Also, it feels way too hacky and like I'm going down the wrong path. There's got to be a proper wordpress way of doing this.
I've looked at various resources online regarding using the admin_post to handle form data, but everything I've seen so far has only covered forms generated for non-admin pages, and not using the Settings API like I am.
Also, the documentation I linked to above was the only place I could find something that seems to talk about creating custom menus in the admin, but it only talks about menus that update settings, and not anything else.
Basically, this is an event management system. The actual event data needs to be stored in the backend in a manner that separates it from the custom post type that displays the data. Also, my end user needs to be able to edit the actual posts with whatever html they want in the usual editor without actually changing this event data that is stored on the backend, hence the need for separation. Only an initial post of a custom type is generated from the form-submitted data, but after that said generated post is free to change in whatever way the end-user sees fit without changing the actual event data.
I've also tried to find documentation on handling form submissions on form data sent to "options.php", but Duck AI is useless, and I can't seem to make heads or tails of what I eventually end up looking at.
Could somebody please explain if I'm on the right track or not, and how to proceed from here?