Last active
August 29, 2015 14:01
-
-
Save snoise/12e6c14fcd2942a781ca to your computer and use it in GitHub Desktop.
定期的(1日おきに)に、記事を自動で投稿する。#WordPressプラグイン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Paka3_task_post | |
Plugin URI: http://www.paka3.com/wpplugin | |
Description: 定期的(1日おきに)に、記事を自動で投稿する | |
Author: Shoji ENDO | |
Version: 0.1 | |
Author URI:http://www.paka3.com/ | |
*/ | |
//プラグインを有効化したとき | |
if(function_exists('register_activation_hook')) { | |
register_activation_hook (__FILE__, 'paka3_plugin_start'); | |
} | |
function paka3_plugin_start(){ | |
//時差を求める | |
$p = ( current_time( 'timestamp' ) - time( ) ) / 3600; | |
//(今日から)毎日タスクを実行する時間を設定する | |
//管理>設定されたタイムゾーンでの時間を設定する(13:00) | |
$my_time = date( 'Y-m-d 13:00:00', current_time( 'timestamp' ) ); | |
//時差を引いて、UNIX時間(UTC:秒)に合わせる | |
$task_time = strtotime( -1 * $p." hour", strtotime( $my_time ) ); | |
wp_schedule_event( $task_time, 'daily', paka3_task_post_hook ); | |
} | |
//プラグインをストップしたとき | |
if(function_exists('register_deactivation_hook')) { | |
register_deactivation_hook (__FILE__, 'paka3_plugin_stop'); | |
} | |
function paka3_plugin_stop(){ | |
wp_clear_scheduled_hook( 'paka3_task_post_hook' ); | |
} | |
//実行する処理 | |
add_action( 'paka3_task_post_hook', 'paka3_task_post_function'); | |
function paka3_task_post_function() { | |
$i = rand(0,2); | |
$c = array( '今日は元気だよ' , '今日はきついよ' , '今日はねる' ); | |
//処理の実行 | |
$post = array( | |
'title' => "本日の気分" , | |
'content' => $c[$i] , | |
); | |
new_my_post($post, $catID); | |
} | |
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