人氣 143°c

WP投稿功能新增密碼保護和隱藏內容

文章索引

WordPress Blog 博客(部落格)網誌公開讀者投稿頁,接受廣大的網友投稿,方便投稿者不用註冊帳號,即可透過投稿功能遞交文章,管理員收到來稿者的作品進行審閱並獲准發佈。然而坊間分享的投稿外掛(插件),很多沒有在欄位加上密碼或隱藏內容給投稿者填寫的,若是!文章內容能夠受到密碼保護,一方面可加強私隱度,也可以隱藏部份重要信息不被公開。

WP程序預設並不提供投稿功能,但是WordPress擁有強大的擴展能力,我們可以自己來添加這個功能。

插件(外掛)地址:WordPress添加投稿功能

由 露兜 作者分享的投稿外掛,也是沒有提供密碼保護和隱藏部份內容的欄位,不重要!...我們現在進行手動加上去。

圖片預覧:

放大圖片:請輕輕按我!

一、投稿加上密碼保護

總共需要新增 3 項代碼

01.

找:

$title =  isset( $_POST['tougao_title'] ) ? trim(htmlspecialchars($_POST['tougao_title'], ENT_QUOTES)) : '';

下方新增:

$password = isset( $_POST['tougao_postpassword'] ) ? trim(htmlspecialchars($_POST['tougao_postpassword'], ENT_QUOTES)) : '';

02.

找:

'post_title' => $title,

下方新增:

'post_password' => $password,

03.

添加HTML欄位:

<div style="text-align: left; padding-top: 10px;">
		<label for="tougao_postpassword">密碼(選填)</label>
		<input type="password" maxlength="8" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_password; ?>" id="tougao_postpassword" name="tougao_postpassword" />
	</div>

完!

圖片預覧:

放大圖片:請輕輕按我!

二、投稿加上隱藏內容

想給投稿者填入隱藏部份敏感內容

請先打開 WordPress 主題目錄的 functions.php 文件加入以下代碼

function e_secret($atts, $content=null){
    extract(shortcode_atts(array('key'=>null), $atts));
    if(isset($_POST['e_secret_key']) && $_POST['e_secret_key']==$key){
        return ''.$content.'';
    }
    else{
        return '
<form action="'.get_permalink().'" method="post" name="e-secret">
<p><label for="tougao_hidecontent">請輸入密碼查看加密內容:<input type="password" name="e_secret_key" size="20" /></label>
<input type="submit" value="確定" /></p>
</form>
';
    }
}
add_shortcode('secret','e_secret');

然後!在投稿文件,新增 3 項代碼

01.

找:

$title =  isset( $_POST['tougao_title'] ) ? trim(htmlspecialchars($_POST['tougao_title'], ENT_QUOTES)) : '';

下方新增

	$hide1 =  isset( $_POST['tougao_hidepassword'] ) ? trim(htmlspecialchars($_POST['tougao_hidepassword'], ENT_QUOTES)) : '';
	$hide2 =  isset( $_POST['tougao_hidecontent'] ) ? trim(htmlspecialchars($_POST['tougao_hidecontent'], ENT_QUOTES)) : '';

02.

找:

$post_content = '昵称: '.$name.'<br />Email: '.$email.'<br />blog: '.$blog.'<br />内容:<br />'.$content;

改為:

$post_content = ''.$content.'[secret key="'.$hide1.'"]'.$hide2.'[/secret]<br />昵称: '.$name.'<br />Email: '.$email.'<br />blog: '.$blog.'';

03.

添加HTML欄位:

	<div style="text-align: left; padding-top: 10px;">
		<label for="tougao_hidepassword">密碼:*</label>
		<input type="password" maxlength="8" size="40" value="" id="tougao_hidepassword" name="tougao_hidepassword" />
	</div>
	<div style="text-align: left; padding-top: 10px;">
		<label for="tougao_hidecontent">隱藏內容:</label>
		<textarea rows="15" id="tougao_hidecontent" name="tougao_hidecontent"></textarea>
	</div>

完!

01. 

[secret key="123456"]需要隐藏的内容[/secret]

其中 key="123456" 中的 123456 就是密碼,可自行修改。

02. 如果你想加上標籤欄目,給投稿者填寫標簽。

只要在 $tougao = array( 

加上 'tags_input' => $tags,

並按照密碼保護的代碼適當地修改即可。

備份內容:

如何為WordPress主題、WordPress小程序添加投稿功能?只需要使用 wp_insert_post 函數添加文章,並設置為待審核狀態。

編輯文章也可以使用這個函數,只需要在傳入的文章中包含文章ID就可以。

最重要的參數就是文章標題和文章內容,其他的文章屬性該函數都提供了默認值。

wp_insert_post(array(
'post_title' => '文章標題',
'post_content' => '文章內容',
'post_status' => 'pending' // 設置為待審核
))

其他參數詳解:

'ID'(int)

文章 ID。如果等於 0 以外的值,則將更新具有該 ID 的文章。默認值 0。

'post_author'(int)

發布文章的用戶的 ID。默認值是當前用戶 ID。

'post_date'(string)

發布日期。默認值為當前時間。

'post_date_gmt'(string)

在 GMT 時區中發布的日期。默認值為 $post_date。

'post_content'(string)

文章內容。默認為空。

'post_content_filtered'(string)

篩選後的文章內容。默認為空。

'post_title'(string)

文章標題。默認為空。

'post_excerpt'(string)

文章摘錄。默認為空。

'post_status'(string)

文章狀態。默認"草稿"。

'post_type'(string)

文章類型。默認"Post"。

'comment_status'(string)

文章是否可以接受評論。接受"打開"或"關閉"。Default 是"default_comment_status"選項的值。

'ping_status'(string)

Post 是否可以接受 Ping。接受"打開"或"關閉"。Default 是"default_ping_status"選項的值。

'post_password'(string)

訪問文章的密碼。默認為空。

'post_name'(string)

職位名稱。默認值是創建新文章時經過清理的文章標題。

'to_ping'(string)

空格或回車符將 URL 的分隔列表返回給 Ping。默認為空。

'pinged'(string)

空格或回車分隔已 Ping 的 URL 列表。默認為空。

'post_modified'(string)

上次修改文章的日期。默認值為當前時間。

'post_modified_gmt'(string)

在 GMT 時區中最後一次修改文章的日期。默認值為當前時間。

'post_parent'(int)

為其所屬的文章設置此選項(如果有)。默認值 0。

'menu_order'(int)

文章的顯示順序。默認值 0。

'post_mime_type'(string)

文章的 MiMe 類型。默認為空。

'guid'(string)

引用文章的全局唯一 ID。默認為空。

'import_id'(int)

插入新文章時要使用的文章 ID。如果指定,則不能與任何現有的文章 ID 匹配。默認值為 0。

'post_category'(int[])

類別 ID 的數組。默認為"default_category"選項的值。

'tags_input'(array)

標記名﹑Slug或 ID 的數組。默認為空。

相關文章:
01. WP設置密碼保護的文章禁止搜尋引擎收錄
02. WP刪除修改標題前的私密or受保護的內容
03. WP用CSS美化密碼保護頁的提示樣式
標籤: