I have a form on a page that populates the database table:

<table border="1">
    <td align="center">Form Input Employees Data</td>
        <form method="post" action="input.php">

          <td>Product Name</td>
          <td><input type="text" name="name" size="20">
          <td><input type="text" name="brand" size="40">
          <td align="right"><input type="submit" name="submit" value="Sent"></td>

And here is input.php:

//the example of inserting data with variable from HTML form
mysql_connect("localhost","xxx","xxx");//database connection

$current_user= wp_get_current_user();
$id= $current_user->user_login; 

//inserting data order
$order = "INSERT INTO wp_userdata
            (id, product_name, product_brand)

//declare in the order variable
$result = mysql_query($order);  //order executes
    echo("<br>Input data is succeed");
} else{
    echo("<br>Input data is fail");
echo $id;

I am trying to pass username to a database as "id". I am getting next error:

Fatal error: Call to undefined function wp_get_current_user() in /home/xxx/input.php on line 10

I tried to change things around but still not working..

  • 1
    Does input.php load any of the WordPress functionality? wp_get_current_user() is a WordPress function, so you need to load up WordPress before you use it. Also, don't use PHP short tags -- use <?php instead of <? in your code. And last, check out the wpdb class, which simplifies interacting with the WordPress DB.
    – Pat J
    Commented Aug 8, 2014 at 17:46
  • Ok I moved the wp_get_current_user() bit to the top of first file and now it works, however it doesn't pass the username value
    – Acidon
    Commented Aug 8, 2014 at 18:06

1 Answer 1


Wordpress doesn't work this way. If you want to work with a framework like Wordpress, you should obey its rules!

Of course you can add a database record like this, but it is a really bad approach. Try some plugins for creating and saving forms in Wordpress like Ninja Forms or Quform. You can even do it yourself, but its not recommended for Wordpress newbies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.