WordPress – How to render option page after form submit in wp-admin?

I am trying to create a custom plugin in which form is submitting on first option page added.

I want to fetch posts from database on the basis of submitted data and show on other page in wp-admin to further mark these posts to add some more data with these posts.

Problem: Upon submitting a form to action added, this shows only the values of $_POST on a broken page instead of proper admin page.

Question: how to show fetched data on proper admin page?

function listposts_register_lists_page() {
//brings all posts at first time
add_options_page(‘Set Message in Selected Posts’, ‘Alert Messages’, ‘manage_options’, ‘listposts_plugin’, ‘listposts_lists_page’); //listposts_option_page
//Triggers upon Add New Button
add_options_page( ‘Set Message in Selected Posts’, ”, ‘manage_options’, ‘add-new-message’, ‘add_new_message_page’);

}
add_action(‘admin_menu’, ‘listposts_register_lists_page’);

Form

function add_new_message_page()
{
?>
<div class=”wrap”>
<h1>Set Alert Message</h1>
<form method=’POST’ action='<?php echo admin_url( ‘admin-post.php’ ); ?>’>

<input type=’hidden’ name=’action’ value=’fetch_posts’/>
<?php wp_nonce_field( ‘submitform’, ‘submitform_nonce’ ); ?>
<table class=”form-table”>
<tr valign=”top”>
<th scope=”row”>Message</th>
<td><textarea id=”listposts_option_name” name=”listposts_option_name” value=”<?php echo get_option(‘listposts_option_name’); ?>” ></textarea></td>
</tr>

<?php

// Excluded CPTs. You can expand the list.
$exclude_cpts = array(
‘attachment’
);
// Builtin types needed.
$builtin = array(
‘post’,
‘page’,
// ‘attachment’
);
// All CPTs.
$cpts = get_post_types( array(
‘public’ => true,
‘_builtin’ => false
) );
// remove Excluded CPTs from All CPTs.
foreach($exclude_cpts as $exclude_cpt)
unset($cpts[$exclude_cpt]);
// Merge Builtin types and ‘important’ CPTs to resulting array to use as argument.
$post_types = array_merge($builtin, $cpts);

//$post_types = get_post_types( $args, $output, $operator );

foreach ( $post_types as $post_type ) {
echo ‘ <tr valign=”top”>
<td><input type=”checkbox” class=”selectposttype” name=”posttypes” value=”‘.$post_type.'” ‘.checked( $post_type, 1 ).’ /></td>
<td scope=”row”><label for=”‘.$post_type.'” >’.$post_type.'</label></td>
</tr>’;

}

?>

</table>
<input type=”submit” value=”Send”/>
</form>

</div>

<?php
}

Function to handle form submit

function show_posts(){
//This should be displayed on proper Option page instead of white page
var_dump($_POST);
}

Here how to render this out put on proper wp-admin page in WP_LIST_TABLE ?

Read more here:: WordPress – How to render option page after form submit in wp-admin?

Leave a Reply

Your email address will not be published. Required fields are marked *