WordPress 是一个出色的多功能平台。您可以创建具有多种不同目的的网站:公司网站、摄影展示、新闻门户、带有交互式菜单的餐厅网站......哦,当然还有博客。您可以使用 WordPress 写博客。忘记了。
奇怪的是,非营利组织往往忽视这种灵活性并利用它。在本教程中,我们将展示如何创建一个简单的请愿脚本来演示组织如何从 WordPress 中受益。
我们到底在构建什么?
我是短代码的忠实粉丝(正如您从我之前的帖子中看到的那样),因此我们将制作一堆短代码和一些有用的函数供短代码使用。我们将把所有这些放在一个名为 petition.php 的文件中,并将其用作 WordPress 插件。
辅助函数
由于我们要在短代码中使用它们,我认为最好先创建并解释它们。
基本的电子邮件验证功能
如果您在服务器上使用 PHP5,我们将使用内置电子邮件验证器来实现我们的功能:
// e-mail address validating function function validate_email( $email ) { if ( $email == '' ) { return false; } else { return filter_var( $email, FILTER_VALIDATE_EMAIL ); } }
如果您使用的是像 PHP4 这样古老的东西,您可以使用使用正则表达式的不同函数:
// e-mail address validating function function validate_email( $email ) { if ( $email == '' ) { return false; } else { $eregi = preg_replace( '/([a-z0-9_.-]+)' . '@' . '([a-z0-9.-]+){2,255}' . '.' . '([a-z]+){2,10}/i', '', $email ); } return empty( $eregi ) ? true : false; }
请注意:您不能同时使用两者!
提交参赛作品的功能
我们可以创建并使用不同的数据库表来包含请愿书的提交内容,但我认为这不是一个好的做法。嘿,自定义字段有什么问题吗?
// submit the signer with a 'petition_submission' meta key to the post function submission( $name, $email, $date ) { global $post; $array = array( 'name' => $name, 'email' => $email, 'date' => $date ); $petition_meta = serialize( $array ); add_post_meta( $post->ID, 'petition_submission', $petition_meta ); return true; }
正如您可以从代码中读到的那样;
- 我们将
$name
、$email
和$date
变量放入函数中(来自我们稍后将介绍的简码) - 通过创建数组并将其序列化将三个变量放在一起
- 并将数据保存为名为
'petition_submission'
的自定义字段
很简单,对吧?现在我们可以进入有点困难的部分了。
获取提交内容的函数
我们现在可以保存提交的内容,但是我们如何获取它们并使用它们进行操作?方法如下:
// fetch the submissions from the post function get_the_submissions( $number = 5 ) { $petition_meta = get_post_custom_values( 'petition_submission' ); if ( $petition_meta ) { $output = array_slice( $petition_meta, $number * -1 ); return array_reverse( $output ); } }
还记得我说过这会有点困难吗?我撒谎了:
- 我们使用“
petition_submission
”键将帖子元数据的值分配给数组变量 - 然后我们从数组末尾获得了
$number
(默认为 5)个提交内容(注意 -1) - 我们返回了该切片数组的反转列表,以将其从最新到最旧排序
额外:要使用的 CSS 选择器
我们将在代码中使用一些 CSS 选择器,因此请将它们放入主题的 style.css 文件中:
#petition_form {} #petition_form label { font-weight: bold; font-size: larger; line-height: 150%; } #petition_form input { display: block; margin: 5px 0; padding: 3px; } #petition_name { width: 200px; } #petition_email { width: 200px; } #petition_submit { padding: 5px; } .petition_success { color: #693; } .petition_error { color: #A00; } .petition_list { list-style: none; margin: 0; padding: 0; } .petition_list li { background-image: none !important; } .petition_list span { display: inline-block; width: 45%; padding: 1%; margin: 1%; background-color: #FAFAFA; } .submission_name {} .submission_date { font-style: italic; color: #888; }
随意编辑属性的默认值:)
简码
我们完成了辅助函数和 CSS 代码。现在,让我们开始有趣的部分 - 短代码!
我们可以只用一个大的短代码来附加表单、列出条目并显示提交的数量,但是......为什么要扼杀所有乐趣呢?此外,这三个元素的单独短代码将使我们能够在内容中的任何地方使用它们。
我有没有告诉过你我如何喜欢短代码?
请愿书的简码
这个函数很长,所以我将用 PHP 注释来解释代码内部:
function petition_form_sc( $atts ) { // extract the shortcode parameters extract( shortcode_atts( array( // the text value of the submit button 'submit' => 'Submit', // the text for the error message 'error' => 'Your e-mail address is not valid. Please re-enter the form with a valid name and e-mail address.', // the text when the submission is successful 'success' => 'Your submission has been saved. Thank you!' ), $atts ) ); // the HTML elements of our petition form $form = '<form action="'.get_permalink().'" method="post" id="petition_form"> <label for="petition_name">Name:</label> <input type="text" name="petition_name" id="petition_name" /> <label for="petition_email">E-mail address:</label> <input type="text" name="petition_email" id="petition_email" /> <input type="submit" name="petition_submit" id="petition_submit" class="submit" value="'.$submit.'" /> </form>'; // if the form is "POST"ed... if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) { // ...get the name... $name = $_POST['petition_name']; // ...and the e-mail address... $email = $_POST['petition_email']; // ...and the date of "just now", with the date format of your WP options. $date = date( get_option( 'date_format' ) ); // validate the e-mail address first! if ( validate_email( $email ) == true ) { // the e-mail address is valid! remember the function below? submission( $name, $email, $date ); // we sent the variables with the submission() function, now we print the success message WITHOUT THE FORM: return '<div class="petition_success">' . $success . '</div>'; // (if you want the form to be printed again after the submission, just add .$form before the semicolon.) } else { // the e-mail address is NOT valid! we should print the error message WITH THE FORM: return '<div class="petition_error">' . $error . '</div>' . $form; } } // and if the form isn't "POST"ed (meaning the visitor just visited the page), just show the form! else { return $form; } } add_shortcode( 'petition_form', 'petition_form_sc' );
我试图尽可能清楚地表达,但如果您认为我遗漏了一些内容,请随时通过评论这篇文章来询问我!
提交列表的简码
“最新条目”部分是人们加入您的事业的证明,因此我们必须列出至少一定数量的提交内容。
这也不是一个简短的函数,所以我将再次用注释解释代码:
function petition_list_sc( $atts ) { // extract the shortcode parameters extract( shortcode_atts( array( // we could set a default number but remember, we already did that in the get_the_submissions() function :) 'number' => '' ), $atts ) ); // get the $number latest submissions... $submissions = get_the_submissions( $number ); // ...and list 'em! $output = '<ul class="petition_list">'; if ( $submissions ) { foreach( $submissions as $submission ) { // unserialize the data $signer = unserialize( $submission ); // unserialize the data AGAIN, don't know why... $signer = unserialize( $signer ); // you might want to change this part, but the default format look great with the CSS in this tutorial. $output .= '<li>'; $output .= '<span class="submission_name">' . $signer['name'] . '</span>'; $output .= '<span class="submission_date">' . $signer['date'] . '</span>'; $output .= '</li>'; } } $output .= '</ul>'; return $output; } add_shortcode( 'petition_list', 'petition_list_sc' );
再次强调,如果您想问任何问题,请在这篇文章中发表评论。
请愿计数的简码
这是一个非常小的函数,只是为了获取提交了多少条目:
function petition_count_sc() { $petition_meta = get_post_custom_values( 'petition_submission' ); return count( $petition_meta ); } add_shortcode( 'petition_count', 'petition_count_sc' );
如您所见,它只是将自定义字段扔到一个数组中并对其进行计数并返回数字。
结论
我应该强调,这是一个非常简单的示例,表明组织可以通过利用此类脚本从 WordPress 中受益。如果您可以想到对此脚本(或教程)的改进,请在下面的评论中分享您的想法。感谢您的阅读!
以上是用漂亮的请愿书修改你的帖子的详细内容。更多信息请关注PHP中文网其他相关文章!

phpientifiesauser'ssessionusessessionSessionCookiesAndSessionIds.1)whiwSession_start()被称为,phpgeneratesainiquesesesessionIdStoredInacookInAcookInamedInAcienamedphpsessidontheuser'sbrowser'sbrowser.2)thisIdAllowSphptptpptpptpptpptortoreTessessionDataAfromtheserverMtheserver。

PHP会话的安全可以通过以下措施实现:1.使用session_regenerate_id()在用户登录或重要操作时重新生成会话ID。2.通过HTTPS协议加密传输会话ID。3.使用session_save_path()指定安全目录存储会话数据,并正确设置权限。

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

利用会话构建高效购物车系统的步骤包括:1)理解会话的定义与作用,会话是服务器端的存储机制,用于跨请求维护用户状态;2)实现基本的会话管理,如添加商品到购物车;3)扩展到高级用法,支持商品数量管理和删除;4)优化性能和安全性,通过持久化会话数据和使用安全的会话标识符。

本文讨论了PHP中的crypt()和password_hash()之间的差异,以进行密码哈希,重点介绍其实施,安全性和对现代Web应用程序的适用性。

文章讨论了通过输入验证,输出编码以及使用OWASP ESAPI和HTML净化器之类的工具来防止PHP中的跨站点脚本(XSS)。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Dreamweaver CS6
视觉化网页开发工具

记事本++7.3.1
好用且免费的代码编辑器

Atom编辑器mac版下载
最流行的的开源编辑器

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器