0

0

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

WBOY

WBOY

发布时间:2023-08-31 16:17:02

|

1167人浏览过

|

来源于php中文网

原创

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

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 文件中:

Red Panda AI
Red Panda AI

AI文本生成图像

下载
#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 = '
'; // 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 '
' . $success . '
'; // (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 '
' . $error . '
' . $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 = '
    '; 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 .= '
  • '; $output .= '' . $signer['name'] . ''; $output .= '' . $signer['date'] . ''; $output .= '
  • '; } } $output .= '
'; 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 中受益。如果您可以想到对此脚本(或教程)的改进,请在下面的评论中分享您的想法。感谢您的阅读!

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php文件怎么打开
php文件怎么打开

打开php文件步骤:1、选择文本编辑器;2、在选择的文本编辑器中,创建一个新的文件,并将其保存为.php文件;3、在创建的PHP文件中,编写PHP代码;4、要在本地计算机上运行PHP文件,需要设置一个服务器环境;5、安装服务器环境后,需要将PHP文件放入服务器目录中;6、一旦将PHP文件放入服务器目录中,就可以通过浏览器来运行它。

1938

2023.09.01

php怎么取出数组的前几个元素
php怎么取出数组的前几个元素

取出php数组的前几个元素的方法有使用array_slice()函数、使用array_splice()函数、使用循环遍历、使用array_slice()函数和array_values()函数等。本专题为大家提供php数组相关的文章、下载、课程内容,供大家免费下载体验。

1282

2023.10.11

php反序列化失败怎么办
php反序列化失败怎么办

php反序列化失败的解决办法检查序列化数据。检查类定义、检查错误日志、更新PHP版本和应用安全措施等。本专题为大家提供php反序列化相关的文章、下载、课程内容,供大家免费下载体验。

1187

2023.10.11

php怎么连接mssql数据库
php怎么连接mssql数据库

连接方法:1、通过mssql_系列函数;2、通过sqlsrv_系列函数;3、通过odbc方式连接;4、通过PDO方式;5、通过COM方式连接。想了解php怎么连接mssql数据库的详细内容,可以访问下面的文章。

948

2023.10.23

php连接mssql数据库的方法
php连接mssql数据库的方法

php连接mssql数据库的方法有使用PHP的MSSQL扩展、使用PDO等。想了解更多php连接mssql数据库相关内容,可以阅读本专题下面的文章。

1400

2023.10.23

html怎么上传
html怎么上传

html通过使用HTML表单、JavaScript和PHP上传。更多关于html的问题详细请看本专题下面的文章。php中文网欢迎大家前来学习。

1229

2023.11.03

PHP出现乱码怎么解决
PHP出现乱码怎么解决

PHP出现乱码可以通过修改PHP文件头部的字符编码设置、检查PHP文件的编码格式、检查数据库连接设置和检查HTML页面的字符编码设置来解决。更多关于php乱码的问题详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1439

2023.11.09

php文件怎么在手机上打开
php文件怎么在手机上打开

php文件在手机上打开需要在手机上搭建一个能够运行php的服务器环境,并将php文件上传到服务器上。再在手机上的浏览器中输入服务器的IP地址或域名,加上php文件的路径,即可打开php文件并查看其内容。更多关于php相关问题,详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1303

2023.11.13

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

0

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号