【问题标题】:Wordpress Admin form custom $_POST requestWordpress 管理员表单自定义 $_POST 请求
【发布时间】:2014-03-13 13:46:11
【问题描述】:

我正在使用 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' );
?>

【问题讨论】:

    标签: php wordpress request admin


    【解决方案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并创建帖子。

    【讨论】:

    • 感谢您的回答。我需要options.php 那里,因为我有一些其他选项可以保存在该表单中。我现在该怎么办?为这个功能创建一个新的管理页面,或者在这个页面上创建一个新的表单而不做这个操作?
    • 您可以在插件页面中显示(和修改)这些特定选项。将尝试找到一个示例,但这是&lt;input&gt;$_POSTget_optionupdate_option 的基本内容。
    • 不,找不到示例,但您会找到 inside this search query
    • 是的,我知道。我已经保存了选项,但我的问题是如何将需要action="options.php" 的表单和不需要它的表单结合起来。我现在已经通过在一个插件页面上使用 2 个表单来修复它,并且可以正常工作。
    • 好的,但是您的原始帖子中没有说明...恐怕我们遇到了chameleon question ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多