Skip to content

Instantly share code, notes, and snippets.

@pistatium
Created September 18, 2014 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pistatium/0915b367ae0513bf3fcb to your computer and use it in GitHub Desktop.
Save pistatium/0915b367ae0513bf3fcb to your computer and use it in GitHub Desktop.
[WP]アイキャッチの設定とデフォルト画像の登録
<?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 );
}
});
@pistatium
Copy link
Author

attach_idを一意にしたい

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment