用漂亮的请愿书修改你的帖子

WBOY
发布: 2023-08-31 16:18:01
原创
1098 人浏览过

用漂亮的请愿书修改你的帖子

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中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!