Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Admin form custom $_POST request

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' );
?>
like image 979
Robbert Avatar asked May 07 '26 11:05

Robbert


1 Answers

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.

like image 66
brasofilo Avatar answered May 10 '26 01:05

brasofilo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!