WordPress使用前置钩子同步文章更新到hexo
这是借助WordPress的钩子系统来实现的操作
由于WordPress官方对定义钩子的位置设定不够明确,我就自己在/wp-admin/post.php
文件位置自己做了一个引入
1 2 3 4
|
include dirname( __FILE__ ) . '/user/hooks.php';
|
并且在/wp-admin/user/
下新建了一个hooks.php
文件.
这样前期钩子的准备就准备好了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| add_action( 'save_post', 'usr_admin_add_content_before', $priority = 10, $accepted_args =1);
function usr_admin_add_content_before($post_ID) { $hexoRoot = ''; $more = ''; $content = get_post($post_ID); $path = $hexoRoot . '/source/_posts' . '/'.date('Y-m-d', strtotime($content->post_date)) . preg_replace('/[\x{4e00}-\x{9fa5}]+/u', '-', $content->post_title) . '.md'; $title = $content->post_title; $url = $post_ID.'.com'; $id = $post_ID; $date = $content->post_date;
$category = ''; foreach (get_the_category($post_ID) as $v) { $category .= ' - '.$v->name; }
$tags = ''; foreach (wp_get_post_tags($post_ID) as $v) { $tags .= ' - '.$v->name; }
$content = str_replace($more, '<!--more-->', $content->post_content_filtered); $con = <<<con --- title: $title tags: $tags url: $url id: $id categories: $category date: $date --- $content con;
if(file_exists($path)){ unlink($path); } file_put_contents($path, $con);
$reload = 'cd $hexoRoot &&' . ' hexo clean &&' . ' hexo d &&' . ' pm2 reload app'; exec($reload); }
|