Skip to content

Instantly share code, notes, and snippets.

@snoise
Created May 23, 2014 04:20
Show Gist options
  • Save snoise/ab9ed74aa6fe2de6f0df to your computer and use it in GitHub Desktop.
Save snoise/ab9ed74aa6fe2de6f0df to your computer and use it in GitHub Desktop.
メニューの新規追加とは、別に新しく記事を追加する#WordPress
<?php
/*
Plugin Name: Paka3post
Plugin URI: http://www.paka3.com/wpplugin
Description: メニューの新規追加とは、別に新しく記事を追加する
Author: Shoji ENDO
Version: 0.1
Author URI:http://www.paka3.com/
*/
$paka3Post = new Paka3_post;
class Paka3_post {
function __construct( ) {
//メニューの追加
add_action( 'admin_menu' , array($this , 'adminAddMenu' ) );
}
//メニューの「paka3投稿」の設定(今回はサブメニュー)
function adminAddMenu ( ) {
add_submenu_page("edit.php", '別の投稿ページだよ', 'paka3投稿', 'edit_themes', 'paka3_post', array($this,'paka3_post_page'));
}
//paka3投稿のページ
function paka3_post_page () {
if(isset($_POST['newPost']) && check_admin_referer( get_bloginfo('url').'paka3post_new','paka3post' )){
//new_my_post
$res = $this->new_my_post( $_POST['newPost'], $catID );
if($res){
$data = get_post($res);
echo '<div class="updated fade"><p><strong>新規に投稿されました';
echo "</strong></p></div>";
}
}
//ページに表示する内容
$wp_n = wp_nonce_field(get_bloginfo('url').'paka3post_new','paka3post');
echo <<<EOS
<div class="wrap">
<h2>paka3 投稿ページ</h2>
<form method="post" action="">
{$wp_n}
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="titile">タイトル</label></th>
<td><input name="newPost[title]" type="text" value="{$title}" /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="titile">内容</label></th>
<td>
EOS;
wp_editor( $content, 'content' ,
array( 'textarea_name' => 'newPost[content]' ,
'drag_drop_upload' => true) ) ;
echo <<< EOS
</td>
</tr>
</table>
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="新しく記事を追加する" /></p>
</form>
</div>
EOS;
}
function new_my_post($post, $catID ) {
$my_post = array( );
$my_post[ 'post_title' ] = $post['title'];
$my_post[ 'post_content' ] = $post['content'];
$my_post[ 'post_status' ] = 'draft'; //下書き
$my_post[ 'post_author' ] = 1;
$my_post[ 'post_date' ] = date( 'Y-m-d H:i:s', current_time('timestamp') );
$my_post[ 'post_category' ] = array( $catID );
// データベースに投稿を追加
$res = wp_insert_post( $my_post );
if( $res != 0 ) {
return $res; //post_id
}else{
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment