Wordpress管理表单自定义$_POST请求


Wordpress Admin form custom $_POST request

我在Wordpress中保存自定义设置的管理页面上工作。现在我想添加一个提交按钮,我可以一次插入一些页面。

我不能在我的Wordpress管理员中这样提交。我做了一个简单的测试,在提交后回显'test',但我没有得到任何输出。有人能帮我一下吗?

这是我的代码:

<?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' );
?>

代码问题:

1)通过钩子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)保留表单动作为空,options.php仅用于特殊情况:

<form method="post" action="">

3)你没有调整参数就粘贴了submit_button()函数,你没有也不需要$wrap$other_attributes,详细信息请阅读文档:

<?php submit_button( __( 'Set account pages' ), 'secondary', 'set_account_pages' ) ?>

之后,提交成功,返回test并创建post。