【问题标题】:How to get value from user in wordpress custom plugin?如何在 wordpress 自定义插件中从用户那里获取价值?
【发布时间】:2018-10-24 16:36:06
【问题描述】:

我已经为 wordpress 创建了一个插件,它通过简码向用户显示表单,我想提交表单,获取值并存储在我的插件激活时创建的定义表中。
现在我不知道这个表格应该提交到哪里。我在管理面板中为插件定义了一个子菜单(并将表单操作设置为此子菜单 slug)以获取该值并存储在数据库中,但只有那些如何登录才能提交该表单。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    如果您的表单在前端,您可以使用template_redirect 钩子处理任何表单提交。如果您的表单在后端,那么您可以使用admin_init hook

    说,你的表单代码看起来像前端

    <form method="post">
        <input type="text" name="input_1"/>
        <input type="number" name="input_2"/>
        <?php wp_nonce_field( 'name_of_your_nonce_action', 'name_of_your_nonce_field' ) ?>
        <input type="submit" name="submit_form" value="Submit" />
    </form>
    

    现在在主题functions.php 文件中,你可以像这样处理这个表单

    <?php
    
    add_action( 'template_redirect', 'wp1213_handle_custom_form', 11 );
    
    function wp1213_handle_custom_form() {
        if( ! isset( $_POST['submit_form'] ) ) {
            return;
        }
    
        if( ! wp_verify_nonce( $_POST['name_of_your_nonce_field'], 'name_of_your_nonce_action' ) ) {
            return;
        }
    
        // Then you can handle all post data ($_POST) and save those data in db
        .......
    }
    

    【讨论】:

    • @Sabbir Ahmed “现在在主题 functions.php 文件中”是什么意思:您的意思是您需要将该代码添加到主题文件夹中的 functions.php 文件中吗?
    • 是的,我的意思是那些代码需要写在functions.php文件中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 2021-11-05
    相关资源
    最近更新 更多