1、在当前使用的主题根目录下建一个模板:
Php代码:
- <?php
- /**
- * Template Name: Front Post(前台发布文章)
- * 作者:碎石头
- */
- if( ‘POST’ == $_SERVER[‘REQUEST_METHOD’] && !emptyempty( $_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;
- if (isset ($_POST[‘title’])) {
- $title = $_POST[‘title’];
- } else {
- echo ‘请输入文章标题!’;
- }
- if (isset ($_POST[‘content’])) {
- $content = $_POST[‘content’];
- } else {
- echo ‘请输入文章内容!’;
- }
- $tags = $_POST[‘post_tags’];
- $post = array(
- ‘post_author’ => $user_id,
- ‘post_title’ => $title,
- ‘post_content’ => $content,
- ‘post_category’ => array($_POST[‘cat’]),
- ‘tags_input’ => $tags,
- ‘post_status’ => ‘publish’,
- ‘post_type’ => $_POST[‘post_type’]
- );
- wp_insert_post($post);
- wp_redirect( home_url() );
- }
- do_action(‘wp_insert_post’, ‘wp_insert_post’);
- ?>
- <?php get_header(); ?>
- <div style=“width:100%; text-align: center;”>
- <div style=“width:96%; text-align: left; padding: 0 20px 0 20px;”>
- <?php
- if( current_user_can( ‘publish_posts’ ) ) {
- ?>
- <div style=“width: 100%; font-size: 20px; font-weight: bold; text-align: center;”>发布新文章</div><br/>
- <!–以下为发表文章的表单–>
- <script type=“text/javascript”>
- function log_check(){
- if(new_post.title.value==“” || new_post.title.value==“输入文章标题”){
- alert(“请输入文章标题!”);
- new_post.title.focus();
- return false;
- }
- if(new_post.post_tags.value==“” || new_post.title.value==“输入文章TAGS,用单引号隔开”){
- alert(“请设置文章标签!”);
- new_post.post_tags.focus();
- return false;
- }
- if(new_post.description.value==“”){
- alert(“请输入文章内容!”);
- new_post.description.focus();
- return false;
- }
- if(new_post.cat.value==“” || new_post.cat.value==“-1”){
- alert(“请选择文章分类!”);
- new_post.cat.focus();
- return false;
- }
- }
- </script>
- <form id=“new_post” name=“new_post” method=“post” action=” “ onsubmit=“return log_check();”>
- <p><label for=“title”>文章标题:</label><input type=“text” id=“title” value=“输入文章标题” onfocus=“this.value==this.defaultValue?this.value=”:null;” onblur=“this.value==”?this.value=’输入文章标题’:null;” tabindex=“1” size=“80” name=“title” /></p>
- <p><label for=“post_tags”>文章标签:</label><input type=“text” value=“输入文章TAGS,用单引号隔开” onfocus=“this.value==this.defaultValue?this.value=”:null;” onblur=“this.value==”?this.value=’输入文章TAGS,用单引号隔开’:null;” tabindex=“5” size=“80” name=“post_tags” id=“post_tags” /></p>
- <p><label for=“cat”>文章分类:</label><?php /*wp_dropdown_categories( ‘show_option_none=选择文章分类&tab_index=4&taxonomy=category’ );*/wp_dropdown_categories( ‘tab_index=4&taxonomy=category’ ); ?></p>
- <p><label for=“content”>文章内容:</label>
- <?php wp_editor( ”, content, $settings = array(
- ‘quicktags’=>1,
- ‘tinymce’=>0,
- ‘media_buttons’=>0,
- ‘textarea_rows’=>4,
- ‘editor_class’=>“textareastyle”
- ) ); ?></p>
- <input type=“hidden” name=“post_type” id=“post_type” value=“post” />
- <input type=“hidden” name=“action” value=“post” />
- <p style=“width: 100%; text-align: center;”>
- <input type=“submit” value=“发 布” tabindex=“6” id=“submit” name=“submit” class=“inputy”/>
- <input type=“reset” value=“重 置” id=“reset” name=“” class=“inputn”/>
- </p>
- <?php wp_nonce_field( ‘new-post’ ); ?>
- </form>
- <?php
- }else{
- ?>
- 对不起,您没有发布文章的权限!
- <?php
- }
- ?>
- </div>
- </div>
- <?php get_footer(); ?>
二、新建一个页面,模板选择刚才新建的“Front Post”;
三、通过
http://<域名>/?page_id=<刚才新建的页面ID>
即可访问发布文章页面。