Last active
August 29, 2015 14:01
-
-
Save snoise/89b1ca3a3354644d11a5 to your computer and use it in GitHub Desktop.
定期的(1日おきに)に、記事を自動で投稿する。#WordPressプラグイン(functions.phpに書く場合)
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 | |
//functions.phpに書く場合等(プラグインディレクトリを指定しない方法) | |
add_action( 'wp', 'prefix_setup_schedule' ); | |
function prefix_setup_schedule(){ | |
if ( ! wp_next_scheduled( 'paka3_task_post_hook' ) ) { | |
//時差を求める | |
$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 ); | |
} | |
} | |
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