简体   繁体   English

如何处理Wordpress插件设置页面中的表单?

[英]how do I handle the form in wordpress plugin's settings page?

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: 例如,我从twitter-stream插件的设置页面获取以下代码:

<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: 我读过许多关于PHP的书,但仅看到以下内容:

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 )? 我可以仅将代码放入插件的php文件(称为scheduler.php ,位于wordpress/wp-content/plugins/scheduler/scheduler.php )吗?

Also, show me some examples of how to handle the form in wordpress please. 另外,请告诉我一些有关如何在wordpress中处理表格的示例。

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. 顺便说一句,我只需要在设置页面上输入两个字段:用户名和密码,当管理员按下Submit按钮时,我希望将该字段的内容存储在数据库中。

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 (~). 通过搜索%7E ,我发现它是Unix中的保留代码,代表波浪号(〜)。 Because lots of servers run Unix Operating Systems, you can't use the symbol %7E . 因为许多服务器都运行Unix操作系统,所以不能使用符号%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 named REQUEST_URI . php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']) )从名为REQUEST_URI的键中获取存储在$_SERVER (这是一个特殊数组)中的字符串。 It then replaces every occurence of %7E with ~ . 然后用~替换%7E出现。 In maybe 99% of the cases, no symbols are replaced but there still might be a chance of a tilde in the url. 在大约99%的情况下,不会替换任何符号,但网址中仍可能会出现波浪号。 It's quite trivial but echo echoes the edited string. 这很琐碎,但echo已编辑的字符串。

In this case, the string /sheduled/wp-admin/options-general.php?page=twitterstreamauth is returned by $_SERVER['REQUEST_URI'] , the symbol %7E is not found so the string is just echoed into the html code. 在这种情况下,字符串/sheduled/wp-admin/options-general.php?page=twitterstreamauth$_SERVER['REQUEST_URI'] ,未找到符号%7E因此该字符串仅回显到html代码中。

The action="..." is the action the browser needs to execute when the form is submitted, in this case open the file options-general.php?page=twitterstreamauth action="..."是提交表单时浏览器需要执行的操作,在这种情况下,打开文件options-general.php?page=twitterstreamauth

Also, if you don't know how to store the contents of a form into the database, you haven't read enough PHP books. 另外,如果您不知道如何将表单的内容存储到数据库中,则说明您没有阅读足够的PHP书籍。 You'd need to learn PHP + MySQL to store something in a database. 您需要学习PHP + MySQL才能将某些内容存储在数据库中。

I hope this helps you understand the code a bit better. 希望这可以帮助您更好地理解代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM