質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

Q&A

0回答

1471閲覧

ワードプレスのプラグイン「buddypress」のユーザー画面から画像が登録できません。

yuta1103

総合スコア8

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

0グッド

0クリップ

投稿2017/01/25 12:26

ワードプレスのプラグイン「buddypress」の
ユーザー画面から画像が登録できません。

文章は登録でき、ページに反映されるのですが、
画像が反映されません。
理由が全く分からない為、ご教授していただけないかと思います。
下記がコードになります。

<?php $mode = 'ins'; $submit_btn = '登 録'; $post_id = ''; // 投稿処理の場合 if( isset($_POST['_wpnonce']) //wpnonceチェック && wp_verify_nonce($_POST['_wpnonce'], 'store_regist') //wpnoceベリファイ && isset($_POST['mode']) //wpnonceチェック ){ global $post_error; global $upload_dir; global $image_url; echo "処理"; if( $_POST['mode'] == 'ins' or $_POST['mode'] == 'upd' ){ // バリデーション check_postdata(); // エラーが無ければ投稿処理 if(empty($post_error)){ // 新規登録 if( $_POST['mode'] == 'ins' ){ $post_id = wp_insert_post(array( 'post_title' => (string)$_POST['title'], 'post_content' => (string)$_POST['content'], 'post_status' => 'publish', 'post_author' => get_current_user_id(), 'post_type' => 'store', 'tags_input' => str_replace(",", ",", (string)$_POST['tag']), ), true); } // 更新 if( $_POST['mode'] == 'upd' ){ $submit_btn = '更 新'; $post_id = (string)$_POST['id']; $post_id = wp_update_post(array( 'ID' => $post_id, 'post_title' => (string)$_POST['title'], 'post_content' => (string)$_POST['content'], ), true); } // アイキャッチの登録 if (isset($_FILES['image']) && ($_FILES['image']['name'] != '')) { $image_data = file_get_contents($image_url); $filename = basename($image_url); if(wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; }else{ $file = $upload_dir['basedir'] . '/' . $filename; } file_put_contents($file, $image_data); $wp_filetype = wp_check_filetype($filename, null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($filename), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $file, $post_id ); require_once(ABSPATH . 'wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); wp_update_attachment_metadata( $attach_id, $attach_data ); set_post_thumbnail( $post_id, $attach_id ); } // カスタムフィールドの登録 update_post_meta($post_id, 'pref' , $_POST["pref"]); update_post_meta($post_id, 'address' , $_POST["address"]); update_post_meta($post_id, 'map' , $_POST["map"]); update_post_meta($post_id, 'openclose' , $_POST["openclose"]); update_post_meta($post_id, 'holiday' , $_POST["holiday"]); update_post_meta($post_id, 'directions' , $_POST["directions"]); update_post_meta($post_id, 'tel' , $_POST["tel"]); update_post_meta($post_id, 'fax' , $_POST["fax"]); update_post_meta($post_id, 'pr' , $_POST["pr"]); update_post_meta($post_id, 'establishment' , $_POST["establishment"]); update_post_meta($post_id, 'owner' , $_POST["owner"]); update_post_meta($post_id, 'occupation' , $_POST["occupation"]); } else { /* 投稿に失敗した時の処理等を記述 */ } //データの挿入に成功していたら移動 if(!is_wp_error($post_id)){ $edit_url = "http://". $_SERVER["HTTP_HOST"]. $_SERVER["REQUEST_URI"]; $edit_url = str_replace( "/store_regist/?id=151".$post->ID, '/store_list', $edit_url ); ?> <a href="<?php echo get_permalink($post_id); ?>"><?php echo $_POST["title"]; ?></a> を登録しました。<br /> <br /> <a href="<?php echo $edit_url; ?>">一覧に戻る</a><br /> <?php //ページを移動 // header('Location: '.get_permalink($post_id)); die(); } } else { show_post_error(); } }else if( $_POST['mode'] == 'ins' ){ // 削除 $post_id = $_POST['id']; wp_delete_post($post_id, false); } }else if( isset($_GET['id']) && !isset($_GET['mode']) ){ // 更新画面を表示 echo " 更新画面を表示"; $mode = 'upd'; $post_id = $_GET['id']; $post = get_post( $post_id ); $custom = get_post_custom( $post_id ); } /*--------------------------- * バリデーション *--------------------------*/ function check_postdata(){ global $post_error; global $upload_dir; global $image_url; if(!isset($_POST['title']) || empty($_POST['title'])){ $post_error[] = '販売店名が空白です。'; } if (isset($_FILES['image']['error']) && is_int($_FILES['image']['error'])) { // ファイルバリデーション if (!$_FILES['image']['error']) { // サイズ上限チェック if ($_FILES['image']['size'] > 100000) { $post_img_error[] = 'ファイルサイズが大きすぎます。'; } // getimagesizeを利用しMIMEタイプをチェック $imageInfo = getimagesize($_FILES['image']['tmp_name']); list($orig_width, $orig_height, $image_type) = $imageInfo; if ($imageInfo === false) { $post_error[] = '画像ファイルではありません。'; } else { $ext = substr($_FILES['image']['name'], strrpos($_FILES['image']['name'], '.') + 1); if (false === $ext = array_search( $imageInfo['mime'], array( 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', ), true )) { $post_error[] = '画像形式が未対応です。'; } } $user = wp_get_current_user(); $upload_dir = wp_upload_dir(); $image_url = $upload_dir['path'] . '/'. $user->get('user_login').'-'. date(YmdHis) .'.'. $ext; if (!move_uploaded_file($_FILES['image']['tmp_name'],$image_url)) { $post_error[] = 'ファイル保存時にエラーが発生しました。'; } } } } /*--------------------------- * エラーメッセージ表示セット *--------------------------*/ function show_post_error(){ global $post_error; if(!empty($post_error)){ echo '<div id="error">'; echo implode('<br />', $post_error); echo '</div>'; } } ?> <form action="<?php the_permalink();?>" method="post" enctype="multipart/form-data"> <input type="hidden" name="mode" id="mode" value="<?php echo $mode; ?>" /> <input type="hidden" name="id" id="id" value="<?php echo $post_id; ?>" /> <?php wp_nonce_field('store_regist'); ?> <table> <tr> <th>販売店名</th> <td><input type="text" name="title" id="title" class="" value="<?php echo $post->post_title; ?>" /></td> </tr> <tr> <th>トップイメージ</th> <td><input type="file" name="image" /></td> </tr> <tr> <th>企業イメージ</th> <td><input type="file" name="shoptopphoto" /></td> </tr> <tr> <th>アピール80文字</th> <td> <textarea type="text" name="appeal" id="appeal" class="" rows="5" style="width:98%;" /><?php echo $custom['appeal'][0]; ?></textarea> </td> </tr> <tr> <th>お店トップ画像</th> <td><input type="file" name="storephoto" /></td> </tr> <tr> <th>おすすめ画像①</th> <td><input type="file" name="storephoto1" /></td> </tr> <tr> <th>おすすめ画像①の説明(最大38文字) </th> <td> <textarea type="text" name="detail1" id="detail1" class="" rows="5" style="width:98%;" /><?php echo $custom['detail1'][0]; ?></textarea> </td> </tr> <tr> <th>おすすめ画像②</th> <td><input type="file" name="storephoto2" /></td> </tr> <tr> <th>おすすめ画像②の説明(最大38文字) </th> <td> <textarea type="text" name="detail2" id="detail2" class="" rows="5" style="width:98%;" /><?php echo $custom['detail2'][0]; ?></textarea> </td> </tr> <tr> <th>おすすめ画像③</th> <td><input type="file" name="storephoto3" /></td> </tr> <tr> <th>おすすめ画像③の説明(最大38文字) </th> <td> <textarea type="text" name="detail3" id="detail3" class="" rows="5" style="width:98%;" /><?php echo $custom['detail3'][0]; ?></textarea> </td> </tr> <tr> <th>おすすめ画像④</th> <td><input type="file" name="storephoto4" /></td> </tr> <tr> <th>おすすめ画像④の説明(最大38文字) </th> <td> <textarea type="text" name="detail4" id="detail4" class="" rows="5" style="width:98%;" /><?php echo $custom['detail4'][0]; ?></textarea> </td> </tr> <tr> <th>企業ギャラリー①</th> <td><input type="file" name="photo1" id="photo1" class="" value="<?php if( get_field('photo1') ): ?><img src="<?php the_field('photo1'); ?>" width="175" /><?php endif; ?>" /></td> </tr> <tr> <th>企業ギャラリー②</th> <td><input type="file" name="photo2" /></td> </tr> <tr> <th>企業ギャラリー③</th> <td><input type="file" name="photo3" /></td> </tr> </table> <p class="submit" style="text-align:center;"> <input type="submit" value="<?php echo $submit_btn; ?>" /> <?php if( $mode=='upd' ) { ?> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <button type="button"onclick="location.href='<?php the_permalink();?>?mode=del&id=<?php echo $post_id; ?>'">削 除</button> <?php } ?> </p> </form>

ではよろしくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問