I want to create my own plugin, but I have no idea how to handle a form.
For example I have this code from the twitter-stream plugin’s settings page:
<form action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" method="post">
<label for="consumerkey" style="font-weight:bold;display:block;width:150px;">Consumer Key:</label> <input type="text" value="" id="consumerkey" name="consumerkey" />
<label for="consumersecret" style="font-weight:bold;display:block;width:150px;margin-top:5px;">Consumer Secret:</label> <input type="text" value="" id="consumersecret" name="consumersecret" />
<input type="submit" value="Save" style="display:block;margin-top:10px;" />
</form>
I can’t understand this:
action="php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI'])"
I’ve read many books on PHP and only saw this:
action="somescript.php" for the URL.
I just don’t know where to put the code that handles my form.
Can I just put my code in the plugin’s php file (called scheduler.php, located in wordpress/wp-content/plugins/scheduler/scheduler.php)?
Also, show me some examples of how to handle the form in wordpress please.
By the way, I just need two fields on my settings page: username and password, and I’d want the field’s content to be stored in the database when the admin presses submit button.
And I still can’t understand how
action="php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI'])"
is converted to
action="/sheduled/wp-admin/options-general.php?page=twitterstreamauth"
Sorry my English is not very good. Thank you very much for your attention.
By googling
%7E, I found that it’s a reserved code in Unix, representing a tilde (~). Because lots of servers run Unix Operating Systems, you can’t use the symbol%7E. You can however use the symbol~which may be used in a web address or somewhere on the page.php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI'])takes the string stored in$_SERVER(which is a special array) from the key namedREQUEST_URI. It then replaces every occurence of%7Ewith~. In maybe 99% of the cases, no symbols are replaced but there still might be a chance of a tilde in the url. It’s quite trivial butechoechoes the edited string.In this case, the string
/sheduled/wp-admin/options-general.php?page=twitterstreamauthis returned by$_SERVER['REQUEST_URI'], the symbol%7Eis not found so the string is just echoed into the html code.The
action="..."is the action the browser needs to execute when the form is submitted, in this case open the fileoptions-general.php?page=twitterstreamauthAlso, if you don’t know how to store the contents of a form into the database, you haven’t read enough PHP books. You’d need to learn PHP + MySQL to store something in a database.
I hope this helps you understand the code a bit better.