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 , 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: , just replace this part of the code with all the code in content-page.php. Replace
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('hide_empty=0&id=tougaocategorg&show_count=1&hierarchical=1'); ?> </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('timestamp')替代time() * * 2011年04月12日 : * 修改了wp_die函数调用,使用合适的页面title * * 2013年01月30日 : * 错误提示,增加点此返回链接 * * 2013年07月24日 : * 去除了post type的限制;已登录用户投稿不用填写昵称、email和博客地址 * * 2015年03月08日 : * 使用date_i18n('U')代替current_time('timestamp') */ if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == 'send') { global $wpdb; $current_url = 'http://你的投稿页面地址'; // 注意修改此处的链接地址 $last_post = $wpdb->get_var("SELECT `post_date` FROM `$wpdb->posts` ORDER BY `post_date` DESC LIMIT 1"); // 博客当前最新文章发布时间与要投稿的文章至少间隔120秒。 // 可自行修改时间间隔,修改下面代码中的120即可 // 相比Cookie来验证两次投稿的时间差,读数据库的方式更加安全 if ( (date_i18n('U') - strtotime($last_post)) < 120 ) { wp_die('您投稿也太勤快了吧,先歇会儿!<a href="'.$current_url.'">点此返回</a>'); } // 表单变量初始化 $name = isset( $_POST['tougao_authorname'] ) ? trim(htmlspecialchars($_POST['tougao_authorname'], ENT_QUOTES)) : ''; $email = isset( $_POST['tougao_authoremail'] ) ? trim(htmlspecialchars($_POST['tougao_authoremail'], ENT_QUOTES)) : ''; $blog = isset( $_POST['tougao_authorblog'] ) ? trim(htmlspecialchars($_POST['tougao_authorblog'], ENT_QUOTES)) : ''; $title = isset( $_POST['tougao_title'] ) ? trim(htmlspecialchars($_POST['tougao_title'], ENT_QUOTES)) : ''; $category = isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0; $content = isset( $_POST['tougao_content'] ) ? trim(htmlspecialchars($_POST['tougao_content'], ENT_QUOTES)) : ''; // 表单项数据验证 if ( empty($name) || mb_strlen($name) > 20 ) { wp_die('昵称必须填写,且长度不得超过20字。<a href="'.$current_url.'">点此返回</a>'); } 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('Email必须填写,且长度不得超过60字,必须符合Email格式。<a href="'.$current_url.'">点此返回</a>'); } if ( empty($title) || mb_strlen($title) > 100 ) { wp_die('标题必须填写,且长度不得超过100字。<a href="'.$current_url.'">点此返回</a>'); } if ( empty($content) || mb_strlen($content) > 3000 || mb_strlen($content) < 100) { wp_die('内容必须填写,且长度不得超过3000字,不得少于100字。<a href="'.$current_url.'">点此返回</a>'); } $post_content = '昵称: '.$name.'<br />Email: '.$email.'<br />blog: '.$blog.'<br />内容:<br />'.$content; $tougao = array( 'post_title' => $title, 'post_content' => $post_content, 'post_category' => 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('投稿成功!感谢投稿!<a href="'.$current_url.'">点此返回</a>', '投稿成功'); } else { wp_die('投稿失败!<a href="'.$current_url.'">点此返回</a>'); } }
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:
'post_content' => $post_content,
changed to:
'post_content' => $post_content,'post_status' => 'publish',
2、如果你想让用户在投稿的同时,将投稿者注册成你本站的投稿者,并将文章的作者归到这个投稿者的名下,你可以参考此条回复的内容修改相应的代码:查看回复。
3、如果你的博客文章都有自定义栏目,并且想在用户投稿的同时添加自定义栏目,可以参考这条回复:查看回复。
4、如果你觉得本文提供的文章编辑框太过单调,需要一个富文本编辑,你可以看看这篇文章(包含图片上传功能):WordPress投稿功能添加富文本编辑器
5、如果你使用了一些富文本编辑器,文章提交后内容中的代码都被转义了,可以参考这条回复:查看回复。
6、如果你需要投稿的文章发布后通知投稿者,可以看看这篇文章(前提投稿的文章默认是草稿状态,而不是直接发布):WordPress投稿功能添加邮件提醒功能
7、如果你想给投稿页面增加验证码功能,可以 点此下载 验证码文件,解压后将captcha目录放到当前主题目录下,然后在代码一中,将35行的:
<br clear="all">
改成:
<br clear="all">
将代码二中的:
if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == 'send') {
改成:
if (!isset($_SESSION)) { session_start(); session_regenerate_id(TRUE); } if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == 'send') { 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!

Yes, WordPress is very suitable for e-commerce. 1) With the WooCommerce plugin, WordPress can quickly become a fully functional online store. 2) Pay attention to performance optimization and security, and regular updates and use of caches and security plug-ins are the key. 3) WordPress provides a wealth of customization options to improve user experience and significantly optimize SEO.

Do you want to connect your website to Yandex Webmaster Tools? Webmaster tools such as Google Search Console, Bing and Yandex can help you optimize your website, monitor traffic, manage robots.txt, check for website errors, and more. In this article, we will share how to add your WordPress website to the Yandex Webmaster Tool to monitor your search engine traffic. What is Yandex? Yandex is a popular search engine based in Russia, similar to Google and Bing. You can excel in Yandex

Do you need to fix HTTP image upload errors in WordPress? This error can be particularly frustrating when you create content in WordPress. This usually happens when you upload images or other files to your CMS using the built-in WordPress media library. In this article, we will show you how to easily fix HTTP image upload errors in WordPress. What is the reason for HTTP errors during WordPress media uploading? When you try to upload files to Wo using WordPress media uploader

Recently, one of our readers reported that the Add Media button on their WordPress site suddenly stopped working. This classic editor problem does not show any errors or warnings, which makes the user unaware why their "Add Media" button does not work. In this article, we will show you how to easily fix the Add Media button in WordPress that doesn't work. What causes WordPress "Add Media" button to stop working? If you are still using the old classic WordPress editor, the Add Media button allows you to insert images, videos, and more into your blog post.

Do you want to know how to use cookies on your WordPress website? Cookies are useful tools for storing temporary information in users’ browsers. You can use this information to enhance the user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPresscookies like a professional. Note: This is an advanced tutorial. It requires you to be proficient in HTML, CSS, WordPress websites and PHP. What are cookies? Cookies are created and stored when users visit websites.

Do you see the "429 too many requests" error on your WordPress website? This error message means that the user is sending too many HTTP requests to the server of your website. This error can be very frustrating because it is difficult to find out what causes the error. In this article, we will show you how to easily fix the "WordPress429TooManyRequests" error. What causes too many requests for WordPress429? The most common cause of the "429TooManyRequests" error is that the user, bot, or script attempts to go to the website

WordPresscanhandlelargewebsiteswithcarefulplanningandoptimization.1)Usecachingtoreduceserverload.2)Optimizeyourdatabaseregularly.3)ImplementaCDNtodistributecontent.4)Vetpluginsandthemestoavoidconflicts.5)ConsidermanagedWordPresshostingforenhancedperf

WordPress is very customized, providing a wide range of flexibility and customizability. 1) Through the theme and plug-in ecosystem, 2) use RESTAPI for front-end development, 3) In-depth code level modifications, users can achieve a highly personalized experience. However, customization requires mastering technologies such as PHP, JavaScript, CSS, etc., and pay attention to performance optimization and plug-in selection to avoid potential problems.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
