I am working on a admin page in Wordpress where I save my custom settings. Now I want to add a submit button where I can insert some pages at once with.
I cannot submit like this in my Wordpress admin. I did a simple test by echoing out 'test' after the submit but I got no output. Can someone help me with this.
This is my code:
<?php function stn_gen_settings() { ?>
<form method="post" action="options.php">
<div class="stn_divide">
<fieldset>
<h3><?php _e( 'Set account pages' ); ?></h3>
<?php
// Insert account pages
if( isset( $_POST['set_account_pages'] ) ) {
echo 'test';
$account_page = array(
'post_title' => 'Test page',
'post_content' => '[test_shortcode]',
'post_type' => 'account',
);
$page_id = wp_insert_post( $account_page );
}
?>
<?php submit_button( __( 'Set account pages' ), 'secondary', 'set_account_pages', $wrap, $other_attributes ) ?>
</fieldset>
</div><!--End stn_divide-->
</form>
<?php }
add_menu_page( '639Listings', '639Listings', 'manage_options', 'stn_general', 'stn_gen_settings' );
?>
Issues with your code:
1) Add the menu page through the hook admin_menu:
add_action( 'admin_menu', 'stn_gen_page' );
function stn_gen_page
{
add_menu_page(
'639Listings',
'639Listings',
'manage_options',
'stn_general',
'stn_gen_settings'
);
}
2) Leave the form action empty, options.php is only for special cases:
<form method="post" action="">
3) You pasted the submit_button() function without adjusting the parameters, you don't have and don't need $wrap and $other_attributes, read the docs for details:
<?php submit_button( __( 'Set account pages' ), 'secondary', 'set_account_pages' ) ?>
After that, the submit is successful, echoing test and creating the post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With