Home  >  Article  >  CMS Tutorial  >  Detailed explanation of how to add submission function in WordPress

Detailed explanation of how to add submission function in WordPress

青灯夜游
青灯夜游forward
2023-03-02 19:55:411128browse

Detailed explanation of how to add submission function in WordPress

Many websites want to open up the submission function from readers. Accepting readers' submissions can not only enrich the content of the blog, but also increase communication with readers. It can be said that it kills two birds with one stone. Why not do it? WordPress itself does not provide a submission function, but WordPress has powerful extension capabilities, and we can add this function ourselves.

There are two ways to implement user submissions. One is to open the registration function of the backend. When ordinary users register, they are set as contributors by default. After logging in, they can add articles (the default is draft); the other method A submission form is provided at the front desk, and users can fill in the corresponding form. The former method is relatively simple to implement and basically does not require bloggers to configure too many things. However, some bloggers may feel awkward and do not want others to see their blog backend; while the latter method is more convenient for contributors. Many bloggers don’t have to worry about the backend privacy of their blogs, but this method is more troublesome to implement and requires a lot of configuration. This article will only introduce the latter method. I hope it will be helpful to you. Of course, just copy and paste the code.

1. Add submission form

1. First create a new php file in the directory of the current theme, named tougao-page.php, and then change page.php Copy all the code in tougao-page.php;

2. Delete all comments at the beginning of tougao-page.php, that is, /* and */, and everything in between;

3. Search for: the_content, you can find similar code dcc7971ea2fb1ccad717c5cd04c83203, replace it with code one

If you are on tougao-page the_content cannot be found in .php, then you can search: get_template_part, you can find similar code: 7a6a0f296529c24097cf0d0ebc0a95a5 , just replace this part of the code with all the code in content-page.php. Replace dcc7971ea2fb1ccad717c5cd04c83203

with the following code. Code 1:

<?php the_content(); ?>

<!-- 关于表单样式,请自行调整-->
<form class="ludou-tougao" method="post" action="<?php echo $_SERVER["REQUEST_URI"]; $current_user = wp_get_current_user(); ?>">
	<div style="text-align: left; padding-top: 10px;">
		<label for="tougao_authorname">昵称:*</label>
		<input type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_login; ?>" id="tougao_authorname" name="tougao_authorname" />
	</div>

	<div style="text-align: left; padding-top: 10px;">
		<label for="tougao_authoremail">E-Mail:*</label>
		<input type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_email; ?>" id="tougao_authoremail" name="tougao_authoremail" />
	</div>
					
	<div style="text-align: left; padding-top: 10px;">
		<label for="tougao_authorblog">您的博客:</label>
		<input type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_url; ?>" id="tougao_authorblog" name="tougao_authorblog" />
	</div>

	<div style="text-align: left; padding-top: 10px;">
		<label for="tougao_title">文章标题:*</label>
		<input type="text" size="40" value="" id="tougao_title" name="tougao_title" />
	</div>

	<div style="text-align: left; padding-top: 10px;">
		<label for="tougaocategorg">分类:*</label>
		<?php wp_dropdown_categories(&#39;hide_empty=0&id=tougaocategorg&show_count=1&hierarchical=1&#39;); ?>
	</div>
					
	<div style="text-align: left; padding-top: 10px;">
		<label style="vertical-align:top" for="tougao_content">文章内容:*</label>
		<textarea rows="15" cols="55" id="tougao_content" name="tougao_content"></textarea>
	</div>
					
	<br clear="all">
	<div style="text-align: center; padding-top: 10px;">
		<input type="hidden" value="send" name="tougao_form" />
		<input type="submit" value="提交" />
		<input type="reset" value="重填" />
	</div>
</form>

2. Add form processing code

At the beginning of tougao-page.php, change the first

<?php
/**
 * Template Name: tougao
 * 作者:露兜
 * 博客:https://www.ludou.org/
 * 
 * 更新记录
 *  2010年09月09日 :
 *  首个版本发布
 *  
 *  2011年03月17日 :
 *  修正时间戳函数,使用wp函数current_time(&#39;timestamp&#39;)替代time()
 *  
 *  2011年04月12日 :
 *  修改了wp_die函数调用,使用合适的页面title
 *  
 *  2013年01月30日 :
 *  错误提示,增加点此返回链接
 *  
 *  2013年07月24日 :
 *  去除了post type的限制;已登录用户投稿不用填写昵称、email和博客地址
 *  
 *  2015年03月08日 :
 *  使用date_i18n(&#39;U&#39;)代替current_time(&#39;timestamp&#39;)
 */
    
if( isset($_POST[&#39;tougao_form&#39;]) && $_POST[&#39;tougao_form&#39;] == &#39;send&#39;) {
    global $wpdb;
    $current_url = &#39;http://你的投稿页面地址&#39;;   // 注意修改此处的链接地址
    $last_post = $wpdb->get_var("SELECT `post_date` FROM `$wpdb->posts` ORDER BY `post_date` DESC LIMIT 1");
    // 博客当前最新文章发布时间与要投稿的文章至少间隔120秒。
    // 可自行修改时间间隔,修改下面代码中的120即可
    // 相比Cookie来验证两次投稿的时间差,读数据库的方式更加安全
    if ( (date_i18n(&#39;U&#39;) - strtotime($last_post)) < 120 ) {
        wp_die(&#39;您投稿也太勤快了吧,先歇会儿!<a href="&#39;.$current_url.&#39;">点此返回</a>&#39;);
    }
        
    // 表单变量初始化
    $name = isset( $_POST[&#39;tougao_authorname&#39;] ) ? trim(htmlspecialchars($_POST[&#39;tougao_authorname&#39;], ENT_QUOTES)) : &#39;&#39;;
    $email =  isset( $_POST[&#39;tougao_authoremail&#39;] ) ? trim(htmlspecialchars($_POST[&#39;tougao_authoremail&#39;], ENT_QUOTES)) : &#39;&#39;;
    $blog =  isset( $_POST[&#39;tougao_authorblog&#39;] ) ? trim(htmlspecialchars($_POST[&#39;tougao_authorblog&#39;], ENT_QUOTES)) : &#39;&#39;;
    $title =  isset( $_POST[&#39;tougao_title&#39;] ) ? trim(htmlspecialchars($_POST[&#39;tougao_title&#39;], ENT_QUOTES)) : &#39;&#39;;
    $category =  isset( $_POST[&#39;cat&#39;] ) ? (int)$_POST[&#39;cat&#39;] : 0;
    $content =  isset( $_POST[&#39;tougao_content&#39;] ) ? trim(htmlspecialchars($_POST[&#39;tougao_content&#39;], ENT_QUOTES)) : &#39;&#39;;
    
    // 表单项数据验证
    if ( empty($name) || mb_strlen($name) > 20 ) {
        wp_die(&#39;昵称必须填写,且长度不得超过20字。<a href="&#39;.$current_url.&#39;">点此返回</a>&#39;);
    }
    
    if ( empty($email) || strlen($email) > 60 || !preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)) {
        wp_die(&#39;Email必须填写,且长度不得超过60字,必须符合Email格式。<a href="&#39;.$current_url.&#39;">点此返回</a>&#39;);
    }
    
    if ( empty($title) || mb_strlen($title) > 100 ) {
        wp_die(&#39;标题必须填写,且长度不得超过100字。<a href="&#39;.$current_url.&#39;">点此返回</a>&#39;);
    }
    
    if ( empty($content) || mb_strlen($content) > 3000 || mb_strlen($content) < 100) {
        wp_die(&#39;内容必须填写,且长度不得超过3000字,不得少于100字。<a href="&#39;.$current_url.&#39;">点此返回</a>&#39;);
    }
    
    $post_content = &#39;昵称: &#39;.$name.&#39;<br />Email: &#39;.$email.&#39;<br />blog: &#39;.$blog.&#39;<br />内容:<br />&#39;.$content;
    
    $tougao = array(
        &#39;post_title&#39; => $title, 
        &#39;post_content&#39; => $post_content,
        &#39;post_category&#39; => array($category)
    );
    // 将文章插入数据库
    $status = wp_insert_post( $tougao );
  
    if ($status != 0) { 
        // 投稿成功给博主发送邮件
        // somebody#example.com替换博主邮箱
        // My subject替换为邮件标题,content替换为邮件内容
        wp_mail("somebody#example.com","My subject","content");
        wp_die(&#39;投稿成功!感谢投稿!<a href="&#39;.$current_url.&#39;">点此返回</a>&#39;, &#39;投稿成功&#39;);
    }
    else {
        wp_die(&#39;投稿失败!<a href="&#39;.$current_url.&#39;">点此返回</a>&#39;);
    }
}

Finally, save tougao-page.php in UTF-8 encoding, otherwise the Chinese may be garbled. Then enter the WordPress management background - Page - Create a page with the title Contribution (you can name it yourself), fill in the content with submission instructions, etc. You can select a template on the right and select tougao. This page is the front-end registration page. Place the link to this page anywhere on the website for users to click to register.

Okay, the basic submission function has been added. As for the form style is not good-looking, the form lacks the items you want, etc., you can just add the css and form items yourself. Finally, you are also welcome to contribute to this site. Of course, the submission method of this site is to open the backend registration function, not the above form.

Code Supplementary Instructions

1. If you want the submitted article to be published immediately without review and editing, then please change the following in the above code:

&#39;post_content&#39; => $post_content,

changed to:

&#39;post_content&#39; => $post_content,'post_status' => 'publish',

2、如果你想让用户在投稿的同时,将投稿者注册成你本站的投稿者,并将文章的作者归到这个投稿者的名下,你可以参考此条回复的内容修改相应的代码:查看回复

3、如果你的博客文章都有自定义栏目,并且想在用户投稿的同时添加自定义栏目,可以参考这条回复:查看回复

4、如果你觉得本文提供的文章编辑框太过单调,需要一个富文本编辑,你可以看看这篇文章(包含图片上传功能):WordPress投稿功能添加富文本编辑器

5、如果你使用了一些富文本编辑器,文章提交后内容中的代码都被转义了,可以参考这条回复:查看回复

6、如果你需要投稿的文章发布后通知投稿者,可以看看这篇文章(前提投稿的文章默认是草稿状态,而不是直接发布):WordPress投稿功能添加邮件提醒功能

7、如果你想给投稿页面增加验证码功能,可以 点此下载 验证码文件,解压后将captcha目录放到当前主题目录下,然后在代码一中,将35行的:

<br clear="all">

改成:

<br clear="all">

将代码二中的:

if( isset($_POST[&#39;tougao_form&#39;]) && $_POST[&#39;tougao_form&#39;] == &#39;send&#39;) {

改成:

if (!isset($_SESSION)) {
 session_start();
session_regenerate_id(TRUE);
}
 
if( isset($_POST[&#39;tougao_form&#39;]) && $_POST[&#39;tougao_form&#39;] == &#39;send&#39;) {
  if(empty($_POST['captcha_code'])
    || empty($_SESSION['ludou_lcr_secretword'])
    || (trim(strtolower($_POST['captcha_code'])) != $_SESSION['ludou_lcr_secretword'])
  ) {
    wp_die('验证码不正确!点此返回');
  }

大功造成!

推荐学习:《WordPress教程

The above is the detailed content of Detailed explanation of how to add submission function in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:ludou.org. If there is any infringement, please contact admin@php.cn delete