WordPress纯代码添加文章投稿功能

很多WordPress博客上都有用户投稿的功能,这个功能用处还是比较大的,不仅适用于文章投稿,还适用于友链提交、网址提交等需要用到表单提交的地方。

就像本站网址导航页面的网址提交,也算是一种投稿功能,只不过是投稿给网址导航而不是文章。如下图:

要实现文章投稿功能其实也挺简单的,下面分享我自用的投稿功能代码:

1、首先在当前主题的目录下新建一个php文件,命名为submit.php。

2、将下面功能代码添加到 submit.php 文件中。

功能代码

<code><?php
/* Template N<span class="xiomao-516-mypl" data-mark="seo"></span>ame: 网址提交 */
get_hea<span class="xiomao-536-mypl" data-mark="seo"></span>der();
$bloginfo = get_bloginfo('ur<span class="xiomao-572-mypl" data-mark="seo"></span>l');
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->po<span class="xiomao-800-mypl" data-mark="seo"></span>sts` ORDER BY `p<span class="xiomao-816-mypl" data-mark="seo"></span>ost_date` DESC LIMIT 1"<span class="xiomao-840-mypl" data-mark="seo"></span>);
// 表单变量初始化
$title =  isset( $_POST['tougao_title'] ) ? trim(htmlspecialchars($_POST['tougao_title'], ENT_QUOTES)) : '';
$url = isset( $<span class="xiomao-984-mypl" data-mark="seo"></span>_POST['tougao_u<span class="xiomao-1000-mypl" data-mark="seo"></span>rl'] ) ? $_PO<span class="xiomao-1014-mypl" data-mark="seo"></span>ST['tougao_url'] : '';
$description =  isset( $_POST['tougao_description'] ) ? trim(htmlspecialchars($<span class="xiomao-1122-mypl" data-mark="seo"></span>_POST<span class="xiomao-1127-mypl" data-mark="seo"></span>['tougao_des<span class="xiomao-1140-mypl" data-mark="seo"></span>cription'], ENT_QUOTES)) : '';<span class="xiomao-1173-mypl" data-mark="seo"></span>
$icon = isset( $_POST['tougao_icon'] ) ? $_POST[<span class="xiomao-1224-mypl" data-mark="seo"></span>'tougao_icon'] : '';
?>
<?<span class="xiomao-1260-mypl" data-mark="seo"></span>php if ( empty($title) || m<span class="xiomao-1287-mypl" data-mark="seo"></span>b_str<span class="xiomao-1292-mypl" data-mark="seo"></span>len($title) < 1 ) { ?>
<script>alert("网站标题不能为空!");&<span class="xiomao-1357-mypl" data-mark="seo"></span>lt;/script>
<meta http-equiv="refresh" cont<span class="xiomao-1408-mypl" data-mark="seo"></span>ent="0;url=<span class="xiomao-1420-mypl" data-mark="seo"></span><?php $bloginfo ?<span class="xiomao-1440-mypl" data-mark="seo"></span>>">
&<span class="xiomao-1452-mypl" data-mark="seo"></span>lt;?php die; } ?>
<?php if ( empty($url) || mb_strlen($url) < 1 ) { ?<span class="xiomao-1530-mypl" data-mark="seo"></span>>
<script>alert("网站地址不能为空!");</script>
<meta http-equiv="refresh" content="0;url=<?php $bloginfo ?>">
<?php die; }<span class="xiomao-1680-mypl" data-mark="seo"></span> ?>
<?php if ( empty($descript<span class="xiomao-1716-mypl" data-mark="seo"></span>ion) || mb_strlen($description) < 5 ) { ?>
<script>alert("网站描述不能为空,且不得少于5个字!");</script>
<<span class="xiomao-1829-mypl" data-mark="seo"></span>meta<span class="xiomao-1833-mypl" data-mark="seo"></span> http-equiv="ref<span class="xiomao-1850-mypl" data-mark="seo"></span>resh" content="0;url=<?php $bloginfo ?>">
<?php die; } ?>
<?php if ( empty($icon) || mb_s<span class="xiomao-1960-mypl" data-mark="seo"></span>t<span class="xiomao-1961-mypl" data-mark="seo"></span>rlen($icon) &lt<span class="xiomao-1976-mypl" data-mark="seo"></span>; 1 ) { ?>
<script&<span class="xiomao-2001-mypl" data-mark="seo"></span>gt;alert("网站icon图标不能为空!");</script>
<meta http-equiv="refresh" content="0;url=<?php $bloginfo ?>">
<?php di<span class="xiomao-2135-mypl" data-mark="seo"></span>e; } ?&gt<span class="xiomao-2144-mypl" data-mark="seo"></span>;
<?php
$post<span class="xiomao-2160-mypl" data-mark="seo"></span>_content = '网站标题: '.$t<span class="xiomao-2184-mypl" data-mark="seo"></span>itle.'<br />网站地址: '.$url.'<br />网站描述: '.$des<span class="xiomao-2244-mypl" data-mark="seo"></span>cription.'<br />网站icon图标:'.$icon;
$tougao = array(
'post_<span class="xiomao-2310-mypl" data-mark="seo"></span>title' =&<span class="xiomao-2320-mypl" data-mark="seo"></span>gt; $title,
'post_content' =&g<span class="xiomao-2352-mypl" data-mark="seo"></span>t; $post_content,
'post_status'<span class="xiomao-2385-mypl" data-mark="seo"></span> => 'publish<span class="xiomao-2401-mypl" data-mark="seo"></span>',
//'post_type' => 'site' //<span class="xiomao-2438-mypl" data-mark="seo"></span>toug<span class="xiomao-2442-mypl" data-mark="seo"></span>ao_type是要保存到的自定义文章<span class="xiomao-2460-mypl" data-mark="seo"></span>类型
);
// 将文章插入数据库
$status = wp_insert_post( $tougao );
if ($status != 0) {
?>
<scr<span class="xiomao-2548-mypl" data-mark="seo"></span>ipt>alert("发布成功!");</script>
<meta http-equiv="refresh" content="0;url=<<span class="xiomao-2640-mypl" data-mark="seo"></span>?php $bloginfo ?<span class="xiomao-2656-mypl" data-mark="seo"></span>>">
<?php } else { ?>
<<span class="xiomao-2695-mypl" data-mark="seo"></span>script>a<span class="xiomao-2706-mypl" data-mark="seo"></span>lert("发布失败,请重新填写!");</script>
<meta ht<span class="xiomao-2755-mypl" data-mark="seo"></span>tp-equiv="refres<span class="xiomao-2772-mypl" data-mark="seo"></span>h" content="0;url=<?php $blogin<span class="xiomao-2808-mypl" data-mark="seo"></span>fo ?&<span class="xiomao-2813-mypl" data-mark="seo"></span>gt;">
<?php } } ?></code>

3、将下面表单代码添加到功能代码下面

添加表单

<code><form method="post" action="<?php echo $_SERVER["REQUEST<span class="xiomao-3024-mypl" data-mark="seo"></span>_URI"]; $current_user = wp_<span class="xiomao-3052-mypl" data-mark="seo"></span>get_current_user(); ?><span class="xiomao-3078-mypl" data-mark="seo"></span>">
<input type="text" id="tougao_title" value="" name="tougao_title" placeho<span class="xiomao-3168-mypl" data-mark="seo"></span>lder="网站标题"/>
<input type="text" id="tougao_url" value="" name="tougao_u<span class="xiomao-3255-mypl" data-mark="seo"></span>rl" placeholder="网站地址" />
<input type="text" id="tougao_url"<span class="xiomao-3328-mypl" data-mark="seo"></span> value="" name="t<span class="xiomao-3348-mypl" data-mark="seo"></span>ougao_url" placeholder="网站类别(请填写贵站类别)"/>
<i<span class="xiomao-3400-mypl" data-mark="seo"></span>npu<span class="xiomao-3403-mypl" data-mark="seo"></span>t type="text" id="tougao_description" name="tougao_description" placeholder<span class="xiomao-3484-mypl" data-mark="seo"></span>="网站描述">
&l<span class="xiomao-3500-mypl" data-mark="seo"></span>t;input type="text" id="tougao_icon"<span class="xiomao-3540-mypl" data-mark="seo"></span> value="" name="tougao_icon" placeholder="网站icon图标"/>
&<span class="xiomao-3604-mypl" data-mark="seo"></span>lt;div class="t"><span class="xiomao-3626-mypl" data-mark="seo"></span>
<input type="hidden" value="send" name="tougao_form" />
<b<span class="xiomao-3700-mypl" data-mark="seo"></span>utton class="button" type="submit" value="提交" >提交申请</button>
<button class="<span class="xiomao-3795-mypl" data-mark="seo"></span>button" type="rese<span class="xiomao-3815-mypl" data-mark="seo"></span>t<span class="xiomao-3816-mypl" data-mark="seo"></span>" value="重填" ><span class="xiomao-3836-mypl" data-mark="seo"></span>清空</button>
</div>
<<span class="xiomao-3871-mypl" data-mark="seo"></span>/form>
</code>

4、添加页面内容代码

<code><div>
<?php while(have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
</div></code>

5、添加底部代码

<code><?php get_foot<span class="xiomao-4214-mypl" data-mark="seo"></span>er(); ?></code>

6、最后新建页面,页面属性选择刚才创建的网址提交即可。

结束

一个简单的投稿功能就完成了,大家可以亲自上手试试,

该文章转载来自暖岛网

人已赞赏
wordpress教程网站教程

WordPress使用timthumb.php截取文章缩略图教程

2020-10-28 22:02:32

wordpress教程网站教程

WordPress纯代码实现前台登录功能

2020-10-28 22:02:38

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索