PHP

ブログ記事のスラッグを乱数化

 

乱数を自動生成

記事のスラッグを乱数化。
※絶対に他の記事と被ることの無い投稿IDのMD5値をスラッグとして設定。

functions.phpに追記。

add_action('transition_post_status', function($new_status, $old_status, $post){
	if($new_status == 'publish' && $old_status != 'publish'){
		$slug = md5($post->ID);
		$newpost = array();
		$newpost['post_name'] = $slug;
		$newpost['ID'] = $post->ID;
		wp_update_post($newpost);
 
	}
}, 10, 3);

 

参考:https://wood-roots.com/web/wordpress/2291