(原创)让你的wordpress具有首页发文功能
自从用上了微博,就觉得Wordpress的文章发布功能好麻烦,为什么不能在首页发布文章呢?找来找去,找到了P2主题,可是个性化需要修改CSS,弄了一半因为工作太忙无法进行了。
能不能有一种简单的方法,可以直接在wordpress首页发布文章,免去繁琐的后台登陆呢。记得原来有一款quick-press插件可以在widget区域发布文章。于是参考了下,把有关代码扣了出来,并实现前台发文:
添加代码的方式很简单,如下(红色背景的部分就是需要添加的代码):
1、找到你所用主题的index.php文件,在代码最前面加上:
if( ‘POST’ == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == ‘post’ ) {
if ( ! is_user_logged_in() )
auth_redirect();
if( !current_user_can( ‘publish_posts’ ) ) {
wp_redirect( get_bloginfo( ‘url’ ) . ‘/’ );
exit;
}
check_admin_referer( ‘new-post’ );
$user_id = $current_user->user_id;
$post_content = $_POST['posttext'];
$tags = $_POST['tags'];
$char_limit = 40;
$post_title = $_POST['title'];
$post_name = $_POST['name'];
$post_id = wp_insert_post( array(
‘post_author’ => $user_id,
‘post_title’ => $post_title,
‘post_content’ => $post_content,
‘tags_input’ => $tags,
‘post_status’ => ‘publish’
) );
wp_redirect( get_bloginfo( ‘url’ ) . ‘/’ );
exit;
}
2、 还是在你的index.php文件里面,在显示文章列表代码之前加上以下代码(比如我的文章列表是:
<?php
get_template_part( ‘loop’, ‘index’ );
?>
我就在这段代码之前加的):
<?php
/* 首页发文代码
*/
if( current_user_can( ‘publish_posts’ ) ) {
require_once dirname( __FILE__ ) . ‘/post-form.php’;
}
?>
3、在你所用主题文件夹根目录下新建post-form.php文件,其代码如下:
<?php
$user = get_userdata( $current_user->ID );
?>
<div id=”postbox” style=” padding: 15px; margin: 5px 0px; -moz-border-radius: 10px; -khtml-border-radius: 10px; -webkit-border-radius: 10px; border-radius: 10px;” >
<form id=”new_post” name=”new_post” method=”post” action=”<?php bloginfo( ‘url’ ); ?>/”>
<input type=”hidden” name=”action” value=”post” />
<?php wp_nonce_field( ‘new-post’ ); ?>
<label for=”title”> 博文标题:</label>
<br />
<input type=”text” name=”title” id=”title” style=”width:60%; height: 25px; border: 1px solid #99afbc; margin: 5px 0 10px 0;” />
<br />
<label for=”posttext”>扯蛋的内容:</label>
<br />
<textarea name=”posttext” id=”posttext” rows=”3″ style=”width:95%; height: 80px; border: 1px solid #99afbc; margin: 5px 0 10px 0;” ></textarea>
<br />
<label for=”tags”>标签:</label>
<br />
<input type=”text” name=”tags” id=”tags” style=”width:60%; height: 25px; border: 1px solid #99afbc; margin: 5px 0 10px 0;” />
<br />
<input id=”submit” type=”submit” value=”Post it” style=”background-color: #DDD; border: 1px solid #333; color: #333; font-weight: bold; padding: 5px 8px;” />
</form>
</div> <!– // postbox –>
做完以上修改后,保存,上传并替换服务器上原有文件(最好先备份index.php文件)。刷新一下看看?是不是可以在首页发布文章了?
哈哈,对大家有用的话顶我一下。并感谢quick-press插件作者。


让你的wordpress具有首页发文功能 | qdo.be 【有得分】 -- Share all can share 02:55 on 2011-10-17 链接地址 |
[...] Via shiya.net [...]
让你的wordpress具有首页发文功能 | Have To Share 【有得分】 -- Share all can share 02:50 on 2011-10-17 链接地址 |
[...] shiya.net 由 Share 编译 1 文章作者:Sharer 来源网站:Have To Share [...]
ixwebhosting 20:46 on 2011-05-25 链接地址 |
你可以直接离线发布得了!
搞毛线 15:10 on 2011-05-23 链接地址 |
求助···我试了发布的时候跳转有问题,方便加我QQ指导下吗379692413
一路向北 00:40 on 2011-05-24 链接地址 |
好的,你加我Q吧1950118003,不过要晚上才行的,白天没空啊。
浩子 02:45 on 2011-05-22 链接地址 |
我也不习惯前台弄
好麻烦哦
软件街小秘 08:43 on 2011-05-10 链接地址 |
还是习惯后台编辑的说
一路向北 09:14 on 2011-05-10 链接地址 |
呵呵,那可能是你要发布的文章比较复杂,要快捷的话 还是前台好。
小聪 04:33 on 2011-05-07 链接地址 |
嗯,这个方法不错,有空试试去。
番茄 15:48 on 2011-05-06 链接地址 |
不错,可以试试看。
倡萌 14:19 on 2011-05-06 链接地址 |
哈哈,很好的文章,有空的时候,我也试试
老衲 02:45 on 2011-04-27 链接地址 |
很强大
落花生 22:31 on 2011-04-26 链接地址 |
这个功能真不错!顶!
SB 14:48 on 2011-04-26 链接地址 |
这个功能不错。
集趣 14:15 on 2011-04-26 链接地址 |
不是有那个投稿的代码吗?
一路向北 14:18 on 2011-04-26 链接地址 |
那个倒还没有试过。不过感觉这个最好,可能每个人需求不一样吧。哈哈