首页 文章

Wordpress自定义短代码表单无法发布到同一页面

提问于
浏览
0

我将表单数据发布到同一页面以在Wordpress中处理它时遇到问题 . 我创建了一个名为[lwCSForm]的短代码,它可以工作并在页面上添加已添加的表单 . 问题是,当我按提交页面时,不会注意到$ _POST ['submit']变量,而我的代码无法处理数据 .

I have tested to use these actions in the form:

  • action="php echo $_SERVER['PHP_SELF']" (我无法让php标签在这里工作)

  • action="php echo $post->post_title" (我无法让php标签在这里工作)

  • 甚至直接写页面名称: action="about"

This is the function to display the form through shortcode on a page:

function DisplayCustomSettingsForm_shortcode() {
global $post;

if( !is_user_logged_in() ){
    echo '<div class="lwCSForm-notloggedin">
            <p> Please login to view the content! </p>
          </div>';
    return;
}

if( isset( $_POST['submit'] ) )
{
    echo "The submit button is pressed and has data";
    /* Code to update customers settings, like avatar, email, username */

} else {
$phpself = $post->post_title; // get the current page

  ?>
<!-- LwCSForm Plugin--> <section id="lwCSForm-wrapper"> <form name="lwCSForm" id="lwCSForm" method="post" action="<?php $phpself ?>" autocomplete="on"> Name: <input type="text" name="realname" placeholder="Your Name" > Sitename: <input type="text" name="username" placeholder="Username" > E-mail: <input type="text" name="email" placeholder="E-mail" > Avatar: <input type="text" name="avatar" placeholder="Avatar" > <input type="submit" > </form> </section> <!-- END LwCSForm Plugin-->
<?php } } add_shortcode( 'lwCSForm', 'DisplayCustomSettingsForm_shortcode');

Picture of the added form with the shortcode

The form added with shortcode

为什么wordpress不会处理这些信息?并再次免责声明 . 我无法在这里打印php标签,但它们在源代码中 .

2 回答

  • 0

    我发现你可以编写如下所示的函数,并且更容易进行表单验证和数据处理 . 请注意 action="" 正在使用Wordpress的"the_permalink()"函数 . 这使我们能够在相同的函数中编写所有已发布数据的验证和处理,而不是将其单独挂钩到Wordpress的'INIT'函数 .

    if( isset( $_POST[ 'lwCSFormSubmit' ] ) )
    {
    
    }
    

    以下新功能:

    function DisplayCustomSettingsForm_shortcode() {
        global $post;
    
        if( !is_user_logged_in() ){
            echo '<div class="lwCSForm-notloggedin">
                    <p> Please login to view the content! </p>
                  </div>';
            return;
        }
    
        if( isset( $_POST['lwCSFormSubmit'] ) )
        {
            echo "The submit button is pressed and has data";
            var_dump($_POST);
        } 
        ?>
             <!-- LwCSForm Plugin-->
             
    <section id="lwCSForm-wrapper"> <form name="lwCSForm" id="lwCSForm" method="post" action="<?php the_permalink(); ?>" autocomplete="on"> Name: <input type="text" name="realname" placeholder="Your Name" > Sitename: <input type="text" name="username" placeholder="Username" > Email: <input type="text" name="email" placeholder="E-mail" > Avatar: <input type="text" name="avatar" placeholder="Avatar" > <input type="submit" name="lwCSFormSubmit"> </form> </section>
    <!-- END LwCSForm Plugin--> <?php } add_shortcode( 'lwCSForm', 'DisplayCustomSettingsForm_shortcode');

    Nick Duncans都回答了这个问题,这取决于使用什么情况 .

  • 1

    将您的代码更改为

    function DisplayCustomSettingsForm_shortcode() {
        global $post;
    
        if( !is_user_logged_in() ){
            echo '<div class="lwCSForm-notloggedin">
                    <p> Please login to view the content! </p>
                  </div>';
            return;
        }
    
    
        ?>
    <!-- LwCSForm Plugin--> <section id="lwCSForm-wrapper"> <form name="lwCSForm" id="lwCSForm" method="post" action="" autocomplete="on"> Name: <input type="text" name="realname" placeholder="Your Name" > Sitename: <input type="text" name="username" placeholder="Username" > E-mail: <input type="text" name="email" placeholder="E-mail" > Avatar: <input type="text" name="avatar" placeholder="Avatar" > <input name='WCSPostSubmitButton' type="submit" > </form> </section> <!-- END LwCSForm Plugin-->
    <?php } add_shortcode( 'lwCSForm', 'DisplayCustomSettingsForm_shortcode'); add_action( 'init', 'WCSGetPostData' ); function WCSGetPostData() { if ( isset( $_POST['WCSPostSubmitButton'] ) ) { // dump out the POST data var_dump($_POST); } }

    我已经将POST处理移动到 init 钩子,并且我已经为您的表单命名,因此我们可以通过$ _POST数组识别它 .

相关问题