wp发布 post.php,让你的WordPress博客实现前台快捷发布文章 | 蚂蚁博客

自从用上了微博,就觉得Wordpress的文章发布功能好麻烦,为什么不能在首页发布文章呢?找来找去,找到了P2主题,可是个性化需要修改CSS,弄了一半因为工作太忙无法进行了。

66104998_1.jpg

WordPress博客实现前台快捷发布文章的界面

能不能有一种简单的方法,可以直接在wordpress首页发布文章,免去繁琐的后台登陆呢。记得原来有一款quick-press插件可以在widget区域发布文章。于是参考了下,把有关代码扣了出来,并实现前台发文。添加代码的方式很简单[适用于所有wordpress主题],如下(需要添加的代码):

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文件里面,在显示文章列表代码之前加上以下代码(比如我的文章列表是:

get_template_part( ‘loop’, ‘index’ );

?>

我就在这段代码之前加的):

/* 首页发文代码

*/

if( current_user_can( ‘publish_posts’ ) ) {

require_once dirname( __FILE__ ) . ‘/post-form.php’;

}

?>

3、在你所用主题文件夹根目录下新建post-form.php文件,其代码如下:

$user   = get_userdata( $current_user->ID );

?>

/”>

博文标题:

扯蛋的内容:

标签:

做完以上修改后,保存,上传并替换服务器上原有文件(最好先备份index.php文件)。刷新一下看看?是不是可以在首页发布文章了?

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值