Wordpress Admin表单自定义$ _POST请求

时间:2014-03-13 13:46:12

标签: php wordpress request admin

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

我不能在我的Wordpress管理员中提交这样的内容。我做了一个简单的测试,回应了测试'提交后我没有输出。有人可以帮我这个。

这是我的代码:

<?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 个答案:

答案 0 :(得分:1)

您的代码问题:

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_attributesread the docs详情:

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

之后,提交成功,回显test并创建帖子。