获取Wordpress用户ID并使用表单提交

时间:2012-10-26 23:23:05

标签: php forms wordpress

我采用简单的方法,使用Mac Form Web Builder程序为我的网站创建表单。它实际上做了一个很好的工作,上传到mySQL,但我真的希望它能够找到登录用户的用户ID与表单上传。这样我就可以将提交与数据库中的Wordpress用户相关联。

我的网站用户必须登录才能访问该表单,因此始终可以获取userID。我确实在Wordpress上找到了这个应该有用的内容,但我不确定如何正确添加代码以及放置它的位置:http://codex.wordpress.org/Function_Reference/get_userdata

我在这里附加表单的php。希望我能得到一些指导,因为这对我的网站来说是一个巨大的帮助。

Idea Submission.php:

<?php

$scriptpath = substr( $_SERVER['SCRIPT_FILENAME'], 0, -4 );
$paths = explode ( DIRECTORY_SEPARATOR , $scriptpath );
$myName = end($paths);
require $scriptpath . '/fbapp/php/config.inc.php';


if( strtoupper( $_SERVER['REQUEST_METHOD'] ) == 'POST' ) {

    $ctl = new FormController();
    $ctl->Dispatch();

} else {

    if( ! isset( $_GET['action'] ) )        $myPage->ReportStats( 'NotifyFormView' );
}

ob_start();

$myPage->Show();
ob_end_flush();

?>

Form.cfg.php(我已经更改了敏感材料,并且为这篇文章删除了一些不必要的代码):

<?php exit(0); ?> { 
"settings":
{
    "data_settings" : 
    {
        "save_database" : 
        {
            "database" : "database",
            "is_present" : true,
            "password" : "password",
            "port" : 3306,
            "server" : "55.55.555.55",
            "tablename" : "Submissions",
            "username" : "username"
        },
        "save_file" : 
        {
            "filename" : "ideasubmissionform-results.csv",
            "is_present" : true
        },
        "save_sqlite" : 
        {
            "database" : "Idea Submission.dat",
            "is_present" : false,
            "tablename" : "Idea Submission"
        }
    },
    "email_settings" : 
    {
        "auto_response_message" : 
        {
            "custom" : 
            {
                "body" : "<!DOCTYPE html>\n<html dir=\"ltr\" lang=\"en\">\n<head>\n<title>RegisterYourApp has received your files!!</title>\n</head>\n<body>\nHello [FirstName] [LastName],\n<br>\n   We really appreciate you signing up for our services and our excited to tell you that your app idea submission for [Title] has been received! Within the next 24 hours you'll be receiving confirmation of your file(s) being stamped and your unique hash code for the submitted files.  If you've submitted multiple files they will be stamped in one zip file so will contain a single hash code and confirmation e-mail.</p>\n<br>\n<br>\n<br>\nHere's a copy of what you submitted for your records:\n<p>\n[_form_results_]\n</body>\n</html>\n",
                "is_present" : true,
                "subject" : "We have received your submission!!"
            },
            "from" : "Info@MyDomain.com",
            "is_present" : true,
            "to" : "[email]"
        },
        "notification_message" : 
        {
            "bcc" : "",
            "cc" : "",
            "custom" : 
            {
                "body" : "<!DOCTYPE html>\n<html dir=\"ltr\" lang=\"en\">\n<head>\n<title>You got mail!</title>\n</head>\n<body>\nHey there,\n<p>\n<b>Someone filled out your form, and here's what they said:</b>\n<p>\n[_form_results_]\n</body>\n</html>\n",
                "is_present" : false,
                "subject" : "Idea Submission"
            },
            "from" : "[email]",
            "is_present" : true,
            "replyto" : "[email]",
            "to" : "Info@MyDomain.com"
        }
    },
    "general_settings" : 
    {
        "colorboxautoenabled" : false,
        "colorboxautotime" : 3,
        "colorboxenabled" : false,
        "colorboxname" : "Default",
        "formname" : "Idea Submission",
        "is_appstore" : "0",
        "timezone" : "America/Los_Angeles"
    },
    "mailchimp" : 
    {
        "apiKey" : "0ef1f58",
        "lists" : 
        [

            {
                "action" : 
                {
                    "subscribe" : 
                    {
                        "condition" : "fields",
                        "fb_name" : "mailing_list",
                        "fb_value" : "Yes"
                    },
                    "unsubscribe" : 
                    {
                        "condition" : "never"
                    }
                },
                "is_present" : true,
                "listid" : "0bfc32032c",
                "merge_tags" : 
                [

                    {
                        "fb_name" : "email_mailing",
                        "field_type" : "email",
                        "isGrouping" : false,
                        "name" : "",
                        "req" : true,
                        "tag" : "EMAIL"
                    },

                    {
                        "fb_name" : "firstname",
                        "field_type" : "text",
                        "isGrouping" : false,
                        "name" : "",
                        "req" : false,
                        "tag" : "FNAME"
                    },

                    {
                        "fb_name" : "lastname",
                        "field_type" : "text",
                        "isGrouping" : false,
                        "name" : "",
                        "req" : false,
                        "tag" : "LNAME"
                    }
                ],
                "name" : "Updates & News",
                "subscribe" : 
                {
                    "double_optin" : true,
                    "email_address_field" : "email_mailing",
                    "email_type_field" : "html",
                    "replace_interests" : true,
                    "send_welcome" : false,
                    "update_existing" : true
                },
                "unsubscribe" : 
                {
                    "delete_member" : false,
                    "email_address_field" : "email_mailing",
                    "send_goodbye" : true,
                    "send_notify" : true
                }
            }
        ]
    },
    "payment_settings" : 
    {
        "confirmpayment" : "",
        "currencysymbol" : "",
        "decimals" : 2,
        "fixedprice" : "",
        "invoicelabel" : "",
        "is_present" : false,
        "paymenttype" : "invoice",
        "shopcurrency" : "TRY",
        "usecustomsymbol" : false
    },
    "redirect_settings" : 
    {
        "confirmpage" : "<!DOCTYPE html>\n<html dir=\"ltr\" lang=\"en\">\n<head>\n<title>Success!</title>\n<meta charset=\"utf-8\">\n<style type=\"text/css\">\nbody {background: #f9f9f9;padding-left: 110px;padding-top: 70px; padding-right: 20px;max-width:700px;font-family: Helvetica, Arial;}\np{font-size: 16px;font-weight: bold;color: #666;}\nh1{font-size: 60px; !important;color: #ccc !important;margin:0px;}\nh2{font-size: 28px !important;color: #666 ! important;margin: 0px; border-bottom: 1px dotted #00A2FF; padding-bottom:3px;}\ntd {font-size: 12px !important; line-height: 30px;  color: #666 !important; margin: 0px;border-bottom: 1px solid #e9e9e9;}\ntd:first-child {font-size: 13px !important; font-weight:bold; color: #333 !important; vertical-align:text-top; min-width:100px; padding-right:5px;}\n</style>\n</head>\n<body>\n<h1>Thanks! </h1>\n<h2>The form is on its way.</h2>\n<p>Here&rsquo;s what was sent:</p>\n<div>[_form_results_]</div>\n</body>\n</html>\n",
        "gotopage" : "http://www.MyDomain.com/success/",
        "inline" : "<center><h2>Thank you!</h2>\n<p>We received your form</p></center>",
        "type" : "gotopage"
    },
    "uid" : "a67968bef1ba64039314ab953a1ce923",
    "validation_report" : "in_line"
},
"rules":{"firstname":{"label":"Name","fieldtype":"text","required":true,"maxlength":"25"},"lastname":{"fieldtype":"text","required":true,"maxlength":"25"},"email":{"email":true,"label":"Email","fieldtype":"email","required":true,"equalTo":"email_verification","label_equal":"Confirm Email","maxlength":"254"},"form_of_id":{"label":"Social Security #, Driver&#39;s License, ID or Other","fieldtype":"text","required":true},"streetaddress":{"label":"Address","fieldtype":"text","required":true},"city":{"fieldtype":"text","required":true,"maxlength":"100"},"zip_code":{"fieldtype":"text","required":true,"maxlength":"25"},"state_province":{"fieldtype":"text","required":true,"maxlength":"50"},"country":{"fieldtype":"text","required":true},"phonenumber":{"phone":"phoneUS","label":"Phone Number (Optional)","fieldtype":"tel"},
"payment_rules":{},
"application_version":"Web Form Builder (OSX), build 2.1.4944"
}

uid.php:

<?php

$txt = false;

if( file_exists( 'form.cfg.dat' ) )
    $txt = file_get_contents( 'form.cfg.dat' );
else if ( file_exists( 'form.cfg.php' ) )
    $txt = file_get_contents( 'form.cfg.php' );

if( $txt === false ) {
    echo 'null';
    exit(0);
}

$config = json_decode( substr( $txt, strpos( $txt, "{" ) ), true );

if( isset( $config['settings'] ) &&
    isset( $config['settings']['uid'] ) ) {

        echo $config['settings']['uid'];

} else {

    echo 'null';
}

?>

Idea Submission.html(我更改了敏感材料并删除了此帖子的一些不必要的代码):

<!DOCTYPE HTML>
<html>

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Start of the headers for CoffeeCup Web Form Builder -->
    <script type="text/javascript" src="common/js/form_init.js" data-name=""
    id="form_init_script">
    </script>
    <link rel="stylesheet" type="text/css" href="theme/blue_pad/css/default.css?version=884"
    id="theme" />
    <!-- End of the headers for CoffeeCup Web Form Builder -->
    <title>
      Idea Submission
    </title>
  </head>

  <body>
    <!-- Start of the body content for CoffeeCup Web Form Builder -->
    <form id="docContainer" enctype="multipart/form-data" method="POST" action="../Idea%20Submission.php"
    novalidate="novalidate" class="fb-toplabel fb-100-item-column selected-object"
    style="width: 650px; " data-form="manual_iframe">
      <div id="fb-form-header1" class="fb-form-header" style="padding-bottom: 30px; padding-top: 20px; padding-left: 35px; height: 1px; ">
        <a id="fb-link-logo1" class="fb-link-logo" href="" target="_blank"><img alt="Alternative text" title="Alternative text" id="fb-logo1" class="fb-logo" src="common/images/image_default.png" style="display: none; "/></a>
      </div>
      <div id="section1" class="section">
        <div id="column1" class="column ui-sortable">
          <div style="display:none; min-height:2332px;" id="fb_confirm_inline">
          </div>
          <div style="display:none;" id="fb_error_report">
          </div>
          <div id="item13" class="fb-item fb-100-item-column" style="opacity: 1; ">
            <div class="fb-header fb-item-alignment-center">
              <h2 style="display: inline; color: rgb(8, 2, 0); ">
                Submit
              </h2>
            </div>
          </div>
          <div id="item14" class="fb-item fb-100-item-column" style="opacity: 1; padding-top: 0px; padding-bottom: 10px; padding-right: 0px; padding-left: 25px; ">
            <div class="fb-static-text fb-item-alignment-center">
              <p style="font-style: italic; ">
                Hello! Welcome to our submission page!
              </p>
            </div>
          </div>
          <div id="item50" class="fb-item fb-100-item-column" style="padding-bottom: 10px; padding-right: 0px; padding-left: 25px; opacity: 1; padding-top: 20px; ">
            <div class="fb-static-text fb-item-alignment-center">
              <p style="font-weight: bold; ">
                Please only upload submission per form.
                <BR>
              </p>
            </div>
          </div>
          <div id="item51" class="fb-item fb-100-item-column" style="padding-top: 0px; padding-bottom: 10px; padding-right: 0px; padding-left: 25px; opacity: 1; ">
            <div class="fb-static-text fb-item-alignment-center">
              <p>
                You may come back as many times as you want.
                <BR>
              </p>
            </div>
          </div>
          <div id="item1" class="fb-item fb-33-item-column" style="opacity: 1; padding-bottom: 5px; ">
            <div class="fb-grouplabel">
              <label id="item1_label_0" style="display: inline; ">Name</label>
            </div>
            <div class="fb-input-box">
              <input type="text" id="item1_text_1" maxlength="25" placeholder="First"
              autocomplete="on" data-hint="" name="FirstName" required autofocus/>
            </div>
          </div>
          <div id="item2" class="fb-item fb-66-item-column" style="padding-bottom: 0px; padding-top: 33px; ">
            <div class="fb-grouplabel">
              <label id="item2_label_0" style="display: none; "></label>
            </div>
            <div class="fb-input-box">
              <input type="text" id="item2_text_1" maxlength="25" placeholder="Last"
              autocomplete="on" data-hint="" name="LastName" required/>
            </div>
          </div>
          <div id="item9" class="fb-item fb-100-item-column" style="padding-top: 0px; padding-bottom: 0px; opacity: 1; ">
            <div class="fb-grouplabel">
              <label id="item9_label_0">Email</label>
            </div>
            <div class="fb-input-box">
              <input type="email" id="item9_email_1" maxlength="254" placeholder="you@domain.com"
              autocomplete="on" data-hint="" name="email" required/>
              <div class="fb-hint" style="color: rgb(136, 136, 136); font-weight: normal; font-style: normal; ">
                We will contact you by this email address.
              </div>
            </div>
            <div class="fb-grouplabel">
              <label style="color: rgb(0, 113, 188); ">Confirm Email</label>
            </div>
            <div class="fb-input-box">
              <input type="email" id="item9_email_1_verification" maxlength="254" placeholder=""
              autocomplete="off" data-hint="" name="email_verification" />
            </div>
          </div>
          <div id="item22" class="fb-item fb-100-item-column" style="opacity: 1; ">
            <div class="fb-grouplabel">
              <label id="item22_label_0" style="display: inline; ">Social Security #, Driver&#39;s License, ID or Other</label>
            </div>
            <div class="fb-input-box">
              <input type="text" id="item22_text_1" maxlength="254" placeholder="" autocomplete="off"
              data-hint="" name="Form_Of_ID" required/>
              <div class="fb-hint" style="color: rgb(136, 136, 136); font-weight: normal; font-style: normal; ">
                <BR>
              </div>
            </div>
          </div>
          <div id="item3" class="fb-item fb-100-item-column" style="padding-top: 0px; padding-bottom: 0px; ">
            <div class="fb-grouplabel">
              <label id="item3_label_0" style="display: inline; ">Address</label>
            </div>
            <div class="fb-input-box">
              <input type="text" id="item3_text_1" maxlength="254" placeholder="Address Line 1"
              autocomplete="on" data-hint="" name="StreetAddress" required/>
            </div>
          </div>
          <div id="item5" class="fb-item fb-33-item-column" style="padding-top: 0px; padding-right: 1px; ">
            <div class="fb-grouplabel">
              <label id="item5_label_0" style="display: none; "></label>
            </div>
            <div class="fb-input-box">
              <input type="text" id="item5_text_1" maxlength="100" placeholder="City"
              autocomplete="on" data-hint="" name="City" required/>
            </div>
          </div>
          <div id="item6" class="fb-item fb-33-item-column" style="opacity: 1; padding-top: 0px; padding-bottom: 0px; padding-right: 1px; padding-left: 2px; ">
            <div class="fb-grouplabel">
              <label id="item6_label_0" style="display: none; "></label>
            </div>
            <div class="fb-input-box">
              <input type="text" id="item6_text_1" maxlength="25" placeholder="Postal / Zip Code"
              autocomplete="on" data-hint="" name="Zip_Code" required/>
            </div>
          </div>
          <div id="item27" class="fb-item fb-33-item-column" style="opacity: 1; padding-top: 0px; padding-right: 0px; padding-left: 2px; ">
            <div class="fb-grouplabel">
              <label id="item27_label_0" style="display: none; "></label>
            </div>
            <div class="fb-input-box">
              <input type="text" id="item27_text_1" maxlength="50" placeholder="State/Province"
              autocomplete="on" data-hint="" name="State_Province" required/>
            </div>
          </div>
          <div id="item28" class="fb-item fb-50-item-column" style="opacity: 1; padding-top: 0px; padding-right: 0px; padding-left: 115px; ">
            <div class="fb-grouplabel">
              <label id="item28_label_0" style="display: none; "></label>
            </div>
            <div class="fb-input-box">
              <input type="text" id="item28_text_1" maxlength="254" placeholder="Country"
              autocomplete="on" data-hint="" name="Country" required/>
            </div>
          </div>
          <div id="item19" class="fb-item fb-66-item-column" style="padding-top: 0px; opacity: 1; padding-bottom: 10px; ">
            <div class="fb-grouplabel">
              <label id="item19_label_0" style="display: inline; ">Phone Number (Optional)</label>
            </div>
            <div class="fb-phone">
              <input type="tel" id="item19_tel_1" data-hint="" name="PhoneNumber" placeholder="890-123-4567"
              />
            </div>
          </div>

          <div id="item32" class="fb-item fb-100-item-column" style="padding-top: 0px; ">
            <div class="fb-grouplabel">
              <label id="item32_label_0" style="display: inline; ">Would You Like To Join Our Mailing List?</label>
            </div>
            <div class="fb-dropdown">
              <select id="item32_select_1" data-hint="" name="Mailing_List" required>
                <option selected id="item32_0_option" value="">
                  Choose one
                </option>
                <option id="item32_1_option" value="Yes">
                  Yes
                </option>
                <option id="item32_2_option" value="No">
                  No
                </option>
              </select>
            </div>
            <div class="fb-hint" style="color: rgb(136, 136, 136); font-weight: normal; font-style: normal; ">
              Stay caught up on all the awesome things happening with the site and company.
            </div>
          </div>
          <div id="item34" class="fb-item fb-100-item-column">
            <div class="fb-grouplabel">
              <label id="item34_label_0" style="display: inline; ">Email For Mailing List</label>
            </div>
            <div class="fb-input-box">
              <input type="email" id="item34_email_1" maxlength="25" placeholder="you@domain.com"
              autocomplete="on" data-hint="" name="email_mailing" required/>
              <div class="fb-hint" style="color: rgb(136, 136, 136); font-weight: normal; font-style: normal; ">
                The address you would like are updates sent to, if signing up for the
                mailing list. It is a required field, but if mark &quot;no&quot; above
                you will not be added to list.
              </div>
            </div>
          </div>
          <div id="item35" class="fb-item fb-100-item-column" style="opacity: 1; padding-bottom: 0px; ">
            <div class="fb-grouplabel">
              <label id="item35_label_0" style="display: inline; ">How&#39;d You Find Out About Us? (Optional)</label>
            </div>
            <div class="fb-input-box">
              <input type="text" id="item35_text_1" maxlength="254" placeholder="ie. Friend, Google Search, Blog..."
              autocomplete="off" data-hint="" name="HowFound" />
            </div>
          </div>
          <div id="item52" class="fb-item fb-100-item-column" style="padding-bottom: 10px; padding-right: 0px; padding-left: 25px; opacity: 1; padding-top: 20px; ">
            <div class="fb-static-text fb-item-alignment-center">
              <p style="font-style: italic; ">
                Thanks for being a member!
                <BR>
                <BR>
                We guarantee to keep all your info and idea materials confidential.
                <BR>
                We will never look at the content of your uploaded files until you inform
                us to do so.
                <BR>
                <BR>
              </p>
            </div>
          </div>
        </div>
      </div>
      <div id="fb-captcha_control" class="fb-captcha fb-item-alignment-center"
      style="cursor: default; display: block; ">
        <script type="text/javascript">
        var RecaptchaOptions = { theme : 'clean' };
        </script>
        <script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k=6Lc368kSAAAAALIQElOa6ig3G3PwwicRRKHM6gUK">
        </script>
        <noscript>
          <iframe src="https://www.google.com/recaptcha/api/noscript?k=6Lc368kSAAAAALIQElOa6ig3G3PwwicRRKHM6gUK"
          height="300" width="500" style="border:none;">
          </iframe>
          <br>
          <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
          <input type="hidden" name="recaptcha_response_field" value="manual_challenge">
        </noscript>
      </div>
      <div id="fb-submit-button-div" class="fb-item-alignment-left fb-footer"
      style="min-height: 1px; ">
        <input type="submit" class="fb-button-special" id="fb-submit-button" data-regular=""
        style="" value="Submit" />
      </div>
      <input type="hidden" name="fb_form_custom_html" />
      <input type="hidden" name="fb_form_embedded" />
      <input type="hidden" id="fb_js_enable" name="fb_js_enable" />
    </form>
    <!-- End of the body content for CoffeeCup Web Form Builder -->
  </body>

</html>

非常感谢!

2 个答案:

答案 0 :(得分:1)

您可以使用get_current_user_id()函数在Wordpress中检索当前用户ID,如果用户未登录,则可以使用0 - http://codex.wordpress.org/Function_Reference/get_current_user_id

也就是说,您需要在Wordpress上下文中调用它,看起来您的代码可能没有这样做。任何Wordpress template pages都将加载必要的类和函数以使用Wordpress codex。

答案 1 :(得分:1)

Maby不是最安全的方式,但由于你的表单和wordpress是分开的,你可以在WordPress标题的$ _SESSION中设置用户ID并在表单页面上捕获该会话。

对于WordPress,例如:

global $current_user;
$current_user = wp_get_current_user();    
$_SESSION['WP']['userId'] = get_userdata( $current_user->ID);

并在表单页面上抓住它:

<input type="hidden" name="fb_form_user_id" value="<?php echo $_SESSION['WP']['userId']; ?>" />

希望这有帮助!