2017-03-03 27 views
0
同一頁

我有一個表單數據發佈到同一個頁面來處理它在WordPress的一個問題。 我已經創建了一個名爲[lwCSForm]的短代碼,它可以工作並將其添加到的頁面上的表單吐出。問題是當我按提交頁面不會注意到$ _POST ['submit']變量和我的代碼不能處理數據。WordPress的自定義簡碼的形式不能張貼到

我已經測試使用的形式,這些操作:

  • action="php echo $_SERVER['PHP_SELF']"(我不能讓PHP的標籤在這裏工作)

  • action="php echo $post->post_title"(我不能得到php標籤在這裏工作)

  • 甚至直接寫頁面名稱:action="about"

這是通過簡碼的網頁上顯示錶單功能:

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 

    ?><br /> 
     <!-- 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--> 
    <br /> <?php 
} 
} 
add_shortcode('lwCSForm', 'DisplayCustomSettingsForm_shortcode'); 

圖片添加的形式與簡碼

The form added with shortcode

爲什麼不會WordPress的處理這些信息?並再次聲明。我無法在這裏打印php標籤,但它們在源代碼中。

回答

1

我發現,你可以寫函數類似下面,它會更容易使數據的表單驗證和處理。請注意,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--> 
     <br /> 
       <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> 
     <br /> 
     <!-- END LwCSForm Plugin--> 
<?php 
} 
add_shortcode('lwCSForm', 'DisplayCustomSettingsForm_shortcode'); 

兩個尼克鄧肯的答案,這個作品,這取決於使用什麼樣的情況。

0

更改您的代碼

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; 
    } 


    ?><br /> 
     <!-- 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--> 
    <br /> <?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陣列識別它。

+0

我怎麼沒想到這一點,將其加載到頁面初始化?我會盡快回家測試這個! Thanx – Nuc

+0

這解決了完美的問題,標記爲正確的。只是一個問題。如果我們在'init'鉤子中獲取數據,那麼我如何在短代碼函數中進行回調並打印出「數據已保存」? – Nuc