奇怪的是,非营利组织往往忽视这种灵活性并利用它。在本教程中,我们将展示如何创建一个简单的请愿脚本来演示组织如何从 wordpress 中受益。
我们到底在构建什么?我是短代码的忠实粉丝(正如您从我之前的帖子中看到的那样),因此我们将制作一堆短代码和一些有用的函数供短代码使用。我们将把所有这些放在一个名为 petition.php 的文件中,并将其用作 wordpress 插件。
辅助函数由于我们要在短代码中使用它们,我认为最好先创建并解释它们。
基本的电子邮件验证功能如果您在服务器上使用 php5,我们将使用内置电子邮件验证器来实现我们的功能:
// e-mail address validating functionfunction validate_email( $email ) { if ( $email == '' ) { return false; } else { return filter_var( $email, filter_validate_email ); }}
如果您使用的是像 php4 这样古老的东西,您可以使用使用正则表达式的不同函数:
// e-mail address validating functionfunction 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 postfunction 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 postfunction 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 posted... 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 posted (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 中受益。如果您可以想到对此脚本(或教程)的改进,请在下面的评论中分享您的想法。感谢您的阅读!
以上就是用漂亮的请愿书修改你的帖子的详细内容。
