很多 WordPress 主题都提供用户投稿的功能,比如国内比较知名的 JustNews 主题,就有非常好的用户投稿功能。如果使用的 WordPress 主题没有投稿功能,可以按照本文下面介绍的方法,手动创建模板添加投稿功能。
要在 WordPress 网站添加用户投稿功能,可以按照下面的步骤进行操作:
1、在当前启用的 WordPress 主题目录下新建一个 php 文件,将其命名为 submit.php。
2、将下面的示例代码添加到 submit.php 模板文件中。
示例代码
<?php
/* Template Name: 网址提交 */
get_header();
$bloginfo = get_bloginfo(\'url\');
if( isset($_POST[\'tougao_form\']) && $_POST[\'tougao_form\'] == \'send\') {
global $wpdb;
$current_url = $bloginfo.\'/site\'; // 注意修改此处的链接地址
$last_post = $wpdb->get_var(\"SELECT `post_date` FROM `$wpdb->posts` ORDER BY `post_date` DESC LIMIT 1\");
// 表单变量初始化
$title = isset( $_POST[\'tougao_title\'] ) ? trim(htmlspecialchars($_POST[\'tougao_title\'], ENT_QUOTES)) : \'\';
$url = isset( $_POST[\'tougao_url\'] ) ? $_POST[\'tougao_url\'] : \'\';
$description = isset( $_POST[\'tougao_description\'] ) ? trim(htmlspecialchars($_POST[\'tougao_description\'], ENT_QUOTES)) : \'\';
$icon = isset( $_POST[\'tougao_icon\'] ) ? $_POST[\'tougao_icon\'] : \'\';
?>
<?php if ( empty($title) || mb_strlen($title) < 1 ) { ?>
<script>alert(\"网站标题不能为空!\");</script>
<meta http-equiv=\"refresh\" content=\"0;url=<?php $bloginfo ?>\">
<?php die; } ?>
<?php if ( empty($url) || mb_strlen($url) < 1 ) { ?>
<script>alert(\"网站地址不能为空!\");</script>
<meta http-equiv=\"refresh\" content=\"0;url=<?php $bloginfo ?>\">
<?php die; } ?>
<?php if ( empty($description) || mb_strlen($description) < 5 ) { ?>
<script>alert(\"网站描述不能为空,且不得少于5个字!\");</script>
<meta http-equiv=\"refresh\" content=\"0;url=<?php $bloginfo ?>\">
<?php die; } ?>
<?php if ( empty($icon) || mb_strlen($icon) < 1 ) { ?>
<script>alert(\"网站icon图标不能为空!\");</script>
<meta http-equiv=\"refresh\" content=\"0;url=<?php $bloginfo ?>\">
<?php die; } ?>
<?php
$post_content = \'网站标题: \'.$title.\'<br />网站地址: \'.$url.\'<br />网站描述: \'.$description.\'<br />网站icon图标:\'.$icon;
$tougao = array(
\'post_title\' => $title,
\'post_content\' => $post_content,
\'post_status\' => \'publish\',
//\'post_type\' => \'site\' //tougao_type是要保存到的自定义文章类型
);
// 将文章插入数据库
$status = wp_insert_post( $tougao );
if ($status != 0) {
?>
<script>alert(\"发布成功!\");</script>
<meta http-equiv=\"refresh\" content=\"0;url=<?php $bloginfo ?>\">
<?php } else { ?>
<script>alert(\"发布失败,请重新填写!\");</script>
<meta http-equiv=\"refresh\" content=\"0;url=<?php $bloginfo ?>\">
<?php } } ?>
添加表单
3、将下面所示的表单代码添加到 submit.php 模板文件中。
<form method=\"post\" action=\"<?php echo $_SERVER[\"REQUEST_URI\"]; $current_user = wp_get_current_user(); ?>\">
<input type=\"text\" id=\"tougao_title\" value=\"\" name=\"tougao_title\" placeholder=\"网站标题\"/>
<input type=\"text\" id=\"tougao_url\" value=\"\" name=\"tougao_url\" placeholder=\"网站地址\" />
<input type=\"text\" id=\"tougao_url\" value=\"\" name=\"tougao_url\" placeholder=\"网站类别(请填写贵站类别)\"/>
<input type=\"text\" id=\"tougao_description\" name=\"tougao_description\" placeholder=\"网站描述\">
<input type=\"text\" id=\"tougao_icon\" value=\"\" name=\"tougao_icon\" placeholder=\"网站icon图标\"/>
<div class=\"t\">
<input type=\"hidden\" value=\"send\" name=\"tougao_form\" />
<button class=\"button\" type=\"submit\" value=\"提交\" >提交申请</button>
<button class=\"button\" type=\"reset\" value=\"重填\" >清空</button>
</div>
</form>
4、然后,添加页面内容代码。
<div>
<?php while(have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
5、添加获取页脚代码。
<?php get_footer(); ?>
6、最后新建一个页面,在页面属性中选择刚才创建的模板。
总结
通过上面的步骤,我们就成功给 WordPress 网站添加了一个简单的投稿功能。
在此声明:本文中所引用的向 WordPress 网站添加投稿功能的代码均来自网络,在此表示感谢。
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/wordpress/allow-user-to-submit-posts-to-your-wordpress-site/