Created
September 18, 2014 03:00
-
-
Save pistatium/0915b367ae0513bf3fcb to your computer and use it in GitHub Desktop.
[WP]アイキャッチの設定とデフォルト画像の登録
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 | |
// アイキャッチの設定とデフォルト画像の登録 | |
add_action('init', function() { | |
add_theme_support('post-thumbnails'); | |
$filename = dirname(__FILE__) . "/img/no_image.png"; | |
if (file_exists($filename)) { | |
$wp_upload_dir = wp_upload_dir(); | |
$upload_path = $wp_upload_dir['path']; | |
$new_path = $upload_path . "/no_image.png"; | |
rename($filename, $new_path); | |
$filetype = wp_check_filetype( basename( $new_path ), null ); | |
$attachment = array( | |
'guid' => $wp_upload_dir['url'] . '/' . basename( $new_path ), | |
'post_mime_type' => $filetype['type'], | |
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $new_path ) ), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
$attach_id = wp_insert_attachment( $attachment, $new_path); | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $new_path ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
attach_idを一意にしたい