首頁  >  文章  >  後端開發  >  用漂亮的請願書修改你的帖子

用漂亮的請願書修改你的帖子

WBOY
WBOY原創
2023-08-31 16:17:021098瀏覽

用漂亮的請願書修改你的帖子

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn