相信有不少站长朋友都或多或少接触过 WordPress 吧,其中很多人都需要一个投稿页面,当然其中好处不言而喻了。网上有的投稿插件是英文的,有的类似于留言本,有的会显示乱码,而今天笔者就教大家自己做一个插件来实现投稿功能。

首先新建 submit_posts_ajax.php,将以下内容复制进去:

function submit_posts_ajax(){
         if($_POST['submit_posts_ajax']=='yinheli'){
         $title=strip_tags(trim($_POST['post_title']));
         $name=trim($_POST['your_name']);
         $mail=trim($_POST['your_email']);
         $site=trim($_POST['your_site']);
         $content=stripslashes(trim($_POST['post_content']));
         $tags=strip_tags(trim($_POST['post_tags']));
                   $db="SELECT post_title FROM $wpdb->posts WHERE post_title = '$title' LIMIT 1";
                   if ($wpdb->get_var($db)){
                            echo '<div > 发现重复文章. 你已经发表过了. 或者存在该文章</div>';
                            if(substr($site, 0, 7) != 'http://') $site= 'http://'.$site;
                   $author='<a href="'.$site.'" title="'.$name.'">'.$name.'</a>';

         $info='<div > 感谢: '.$author.' 的供稿.</div>'."

";

         if(isset($_POST['post_submit'])){
                   echo '<div > 错误: 没有填写标题!</div>';
                   echo '<div > 错误: 没有填写邮箱地址.</div>';
                   echo '<div > 错误: 还没有填写内容!!!</div>';
                            'post_content' =>$content,
                            'post_status'   => 'pending'
                   $post_id = wp_insert_post($submitdata,$wp_error = false);
                   $subject='您给 [ '.get_option('blogname').' ] 写了篇文章 标题是: '.$title;
                   $message='非常感谢您的供稿. 您的稿件已经提交. 等待管理员的审核. 以下是您提交的内容:<hr>'.stripslashes(trim($_POST['post_content']));
                   yinheli_sendmail_ps($mail,$subject,$message);
                   echo '<div > 非常感谢您的供稿. 您的稿件已经提交. 等待管理员的审核. 请耐心等待.</div>';
                   @header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
         if(isset($_POST['post_review'])){
                   if(''==$tags) $tags='您还没有填写 标签 (Tags)';
                   echo '<div >
                            <div >
                                     <p > 标签:'.$tags.'</p>
function yinheli_sendmail_ps($to,$subject,$message){
         $blogname = get_option('blogname');
         $charset = get_option('blog_charset');
         $ps_sendmail_headers = "From: $blogname
" ;
         $ps_sendmail_headers .= "MIME-Version: 1.0
";
         $ps_sendmail_headers .= "Content-Type: text/html;charset="$charset"
";
         return @wp_mail($to, $subject, $message, $ps_sendmail_headers);
function submit_posts_load_js(){
echo "
".'<script type="text/javascript" src="'%20.%20get_bloginfo('wpurl')%20.%20'/wp-content/plugins/submit_posts/submit_posts.js"></script>'."
";
add_action('init', 'submit_posts_ajax');
require_once('submit_posts.php');
function submit_posts_display(){
$submit_posts_html=submit_posts_html();
return $submit_posts_html;
add_shortcode('submit_posts','submit_posts_display');

接下来新建 submit_posts.php,同样将以下内容复制进去:

<script type="text/javascript">
function submit_posts_html(){
<div id="ps_msg"> 欢迎给我供稿</div>
<form id="post_submit_form" name="post_submit_form" method="post" action="">
<input type="hidden" name="submit_posts_ajax" id="submit_posts_ajax" value="yinheli"/>
<label><input type="text" name="post_title" id="post_title" tabindex="1" value="<?php echo strip_tags($_POST['post_title']);?>"/> 投稿标题 (必填)</label>
<label><input type="text" name="your_name" id="your_name" tabindex="2" value="<?php echo $_POST['your_name'];?>" /> 您的名字或昵称</label>
<label><input type="text" name="your_email" id="your_email" tabindex="3" value="<?php echo $_POST['your_email'];?>" /> 您的邮箱 (必填)</label>
<label><input type="text" name="your_site" id="your_site" tabindex="4" value="<?php echo $_POST['your_site'];?>" /> 您的网站</label>
<textarea name="post_content" cols="50" rows="15" id="post_content" tabindex="5"><?php echo stripslashes($_POST['post_content']);?></textarea>
<label><input type="text" name="post_tags" id="post_tags" tabindex="6" value="<?php echo strip_tags($_POST['post_tags']);?>" /> 标签 (tags)[每个标签用半角的逗号分开]</label>
<input type="submit" name="post_review" id="post_review" value="预览" tabindex="7" />
<input type="submit" name="post_submit" id="post_submit" value="提交" tabindex="8" />
<div style="clear:both"></div>

最后再新建 submit_posts.js,将以下内容复制进去:

Author URI: http://www.89948.net/
if(!window.YHLJSPS) window['YHLJSPS']={};
function isCompatible(other) {
                   || !Object.hasOwnProperty
                   || !document.createElement
                   || !document.getElementsByTagName
                   alert('TR- if you see this message isCompatible is failing incorrectly.');
return document.getElementById(id);
function getXmlHttpObject(){
                   xmlHttp = new XMLHttpRequest();
                            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
function setStyle(element, key, value) {
         element.style[key] = value;
function addEvent(node,type,listener){
         if(!isCompatible()) { return false }
         if(node.addEventListener){
         node.addEventListener(type,listener,false);
         }else if(node.attachEvent){
         node['e'+type+listener]=listener;
         node[type+listener]=function(){
                   node['e'+type+listener](window.event);
                   node.attachEvent('on'+type,node[type+listener]);
function insertAfter(node, referenceNode) {
    if(!(node = $(node))) return false;
    if(!(referenceNode = $(referenceNode))) return false;
    return referenceNode.parentNode.insertBefore(node, referenceNode.nextSibling);
function preventDefault(eventObject) {
         eventObject = eventObject || getEventObject(eventObject);
         if(eventObject.preventDefault) {
                   eventObject.preventDefault();
                   eventObject.returnValue = false;
function formToRequestString(form_obj,val){
         for (i=0;i<form_obj.length ;i++ ){
                   if (e.name!='' && e.type!='submit'){
                            if (e.type=='select-one'){
                                     element_value=e.options[e.selectedIndex].value;
                            }else if (e.type=='checkbox' || e.type=='radio'){
                            query_string+=and+encodeURIComponent(e.name)+'='+encodeURIComponent(element_value);
function setopacity(node,opacity){
setStyle(node, 'opacity', opacity);
setStyle(node, 'MozOpacity', opacity);
setStyle(node, 'KhtmlOpacity', opacity);
setStyle(node, 'filter', 'alpha(opacity=' + opacity * 100 + ')');
function stateChangeListener(){
var the_form=$('post_submit_form');
         if(xmlHttp.readyState==1){
         r_msg.innerHTML='数据载入中, 请稍候...';
         setopacity(the_form,0.8);
         $('post_submit').disabled=true;
         window.scrollTo(0,r_msg.offsetTop+15);
         }else if(xmlHttp.readyState==4 && xmlHttp.status==200){
         r_msg.innerHTML=xmlHttp.responseText;
         setTimeout(function(){$('post_submit').disabled=false;},1000);
         }else if(xmlHttp.status!=200){
                   alert('呃 出错了 (建议您保存好文章再刷新浏览器.) 错误信息:'+xmlHttp.statusText);
function submitactiontype(type){
var A=formToRequestString($('post_submit_form'))+'&'+encodeURIComponent($(type).name)+'='+encodeURIComponent($(type).value);
function ps_submit(action){
         xmlHttp = getXmlHttpObject();
                   alert ("Oop! Browser does not support HTTP Request.")
         var url=window.location.href;
         if(action=='post_submit'){
                   var senddata=submitactiontype(action);
         if(action=='post_review'){
                   var senddata=submitactiontype(action);
         xmlHttp.onreadystatechange=function(){
         xmlHttp.open("POST", url, true);
         xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
         addEvent($('post_submit'),'click',function(W3CEvent){
                   ps_
submit(action='post_submit');
                   preventDefault(W3CEvent);
         addEvent($('post_review'),'click',function(W3CEvent){
                   ps_submit(action='post_review');
                   preventDefault(W3CEvent);
if (document.addEventListener) {
         document.addEventListener("DOMContentLoaded", initps, false);
} else if (/MSIE/i.test(navigator.userAgent)) {
         document.write('<script id="__ie_onload_for_post_posts" defer src="javascript:void(0)"></script>');
         var script = $('__ie_onload_for_post_posts');
         script.onreadystatechange = function() {
                   if (this.readyState == 'complete') {
} else if (/WebKit/i.test(navigator.userAgent)) {
         var _timer = setInterval( function() {
                   if (/loaded|complete/.test(document.readyState)) {
         window.onload = function(e) {

把以上三个文件保存在 submit_posts 文件夹内,上传至插件目录,并进后台安装该插件。使用时只需要新建页面并输入 [submit_posts] 即可调用出。