質問編集履歴

1

実際のコード

2016/07/13 12:41

投稿

chNOBUNAGA
chNOBUNAGA

スコア41

test CHANGED
File without changes
test CHANGED
@@ -37,3 +37,393 @@
37
37
  全くどこが悪いかわかりません。
38
38
 
39
39
  ちなみに、wo-loginを読み込んでもダメでした。
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+ ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
56
+
57
+ 追記
58
+
59
+ ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
60
+
61
+
62
+
63
+ こちらが、投稿フォームになります
64
+
65
+ ```PHP
66
+
67
+ <?php
68
+
69
+ /**
70
+
71
+ * Template Name: スレッド投稿フォーム
72
+
73
+ */
74
+
75
+ get_header(); ?>
76
+
77
+ <?php
78
+
79
+ if(is_user_logged_in()): //Login check
80
+
81
+ $user = wp_get_current_user();
82
+
83
+ ?>
84
+
85
+ <?php //echo $title ?>
86
+
87
+ <form action="<?php the_permalink();?>" method="post" enctype="multipart/form-data">
88
+
89
+ <?php
90
+
91
+ // セッション処理
92
+
93
+ session_start();
94
+
95
+ require_once "functions/definition.php";
96
+
97
+ $content_txt = "";
98
+
99
+ ini_set("session.bug_compat_42", 0);
100
+
101
+ ini_set("session.bug_compat_warn", 0);
102
+
103
+ if(isset($_SESSION[TITLE])) $content_txt = $_SESSION[TITLE];
104
+
105
+ if(isset($_SESSION[CONTENT])) $content_txt = $_SESSION[CONTENT];
106
+
107
+ show_thread_error();
108
+
109
+ wp_nonce_field('create_thread');
110
+
111
+ ?>
112
+
113
+ <table class="form-table">
114
+
115
+ <tbody>
116
+
117
+ <tr>
118
+
119
+ <th><label for="title">タイトル</label></th>
120
+
121
+ <td><input id="title" type="text" name="title" value="<?php echo $title_txt; ?>" /></td>
122
+
123
+ </tr>
124
+
125
+ <tr>
126
+
127
+ <th><label>画像</label></th>
128
+
129
+ <td><input type="file" name="image" /></td>
130
+
131
+ </tr>
132
+
133
+ <tr>
134
+
135
+ <th><label>カテゴリー</label></th>
136
+
137
+ <td><?php wp_dropdown_categories(); ?></td>
138
+
139
+ </tr>
140
+
141
+ <tr>
142
+
143
+ <th><label for="content">コメント</label></th>
144
+
145
+ <td><textarea id="content" name="content"><?php echo $content_txt; ?></textarea></td>
146
+
147
+ </tr>
148
+
149
+ </tbody>
150
+
151
+ </table>
152
+
153
+ <p class="submit"><input type="submit" value="投稿する" /></p>
154
+
155
+ </form>
156
+
157
+ <?php else: //Login check else ?>
158
+
159
+ <p><a href="<?php echo wp_login_url(get_permalink()); ?>">ログイン</a>してください。</p>
160
+
161
+ <?php
162
+
163
+ endif; //Login check
164
+
165
+ get_footer();
166
+
167
+ ?>
168
+
169
+ ```
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+ こちらが、投稿内容を確認し、投稿します
178
+
179
+
180
+
181
+ ```PHP
182
+
183
+ <?php
184
+
185
+ /**
186
+
187
+ * テンプレートが読み込まれる直前で実行される
188
+
189
+ */
190
+
191
+ global $create_thread_error;
192
+
193
+ $create_thread_error = array();
194
+
195
+ // セッション処理
196
+
197
+ session_start();
198
+
199
+ $_SESSION[TITLE] = $_POST[TITLE];
200
+
201
+ $_SESSION[CONTENT] = $_POST[CONTENT];
202
+
203
+ // 投稿する
204
+
205
+ function _my_create_thread(){
206
+
207
+ if(
208
+
209
+ is_page('create-thread') //create-thredページチェック
210
+
211
+ &&
212
+
213
+ is_user_logged_in() //ログインチェック
214
+
215
+ &&
216
+
217
+ isset($_POST['_wpnonce']) //wpnonceチェック
218
+
219
+ &&
220
+
221
+ wp_verify_nonce($_POST['_wpnonce'], 'create_thread') //wpnoceベリファイ
222
+
223
+ ){
224
+
225
+ // バリデーション
226
+
227
+ global $create_thread_error;
228
+
229
+ if(!isset($_POST['title']) || empty($_POST['title'])){
230
+
231
+ $create_thread_error[] = 'タイトルが空白です。';
232
+
233
+ }
234
+
235
+ if(!isset($_POST['content']) || empty($_POST['content'])){
236
+
237
+ $create_thread_error[] = '本文が空です。';
238
+
239
+ }
240
+
241
+ if (isset($_FILES['image']['error']) && is_int($_FILES['image']['error'])) {
242
+
243
+ // ファイルバリデーション
244
+
245
+ if (!$_FILES['image']['error']) {
246
+
247
+ // サイズ上限チェック
248
+
249
+ if ($_FILES['image']['size'] > 1000000) {
250
+
251
+ $create_thread_error[] = 'ファイルサイズが大きすぎます。';
252
+
253
+ }
254
+
255
+ // getimagesizeを利用しMIMEタイプをチェック
256
+
257
+ $imageInfo = getimagesize($_FILES['image']['tmp_name']);
258
+
259
+ list($orig_width, $orig_height, $image_type) = $imageInfo;
260
+
261
+ if ($imageInfo === false) {
262
+
263
+ $create_thread_error[] = '画像ファイルではありません。';
264
+
265
+ } else {
266
+
267
+ $ext = substr($_FILES['image']['name'], strrpos($_FILES['image']['name'], '.') + 1);
268
+
269
+ if (false === $ext = array_search(
270
+
271
+ $imageInfo['mime'],
272
+
273
+ array(
274
+
275
+ 'jpg' => 'image/jpeg',
276
+
277
+ 'png' => 'image/png',
278
+
279
+ 'gif' => 'image/gif',
280
+
281
+ ),
282
+
283
+ true
284
+
285
+ )) {
286
+
287
+ $create_thread_error[] = '画像形式が未対応です。';
288
+
289
+ }
290
+
291
+ }
292
+
293
+ $user = wp_get_current_user();
294
+
295
+ $upload_dir = wp_upload_dir();
296
+
297
+ $image_url = $upload_dir['path'] . '/'. $user->get('user_login').'-'. date(YmdHis) .'.'. $ext;
298
+
299
+ if (!move_uploaded_file($_FILES['image']['tmp_name'],$image_url)) {
300
+
301
+ $create_thread_error[] = 'ファイル保存時にエラーが発生しました。';
302
+
303
+ }
304
+
305
+ } else {
306
+
307
+ $create_thread_error[] = 'ファイルが選択されていません。';
308
+
309
+ }
310
+
311
+ }
312
+
313
+ //エラーが無ければ投稿処理
314
+
315
+ if(empty($create_thread_error)){
316
+
317
+ $post_id = wp_insert_post(array(
318
+
319
+ 'post_title' => (string)$_POST['title'],
320
+
321
+ 'post_content' => (string)$_POST['content'],
322
+
323
+ 'post_status' => 'publish',
324
+
325
+ 'post_author' => get_current_user_id(),
326
+
327
+ 'post_type' => 'post',
328
+
329
+ 'post_category' => array(intval($_POST['cat']))
330
+
331
+ ), true);
332
+
333
+ //アイキャッチ設定
334
+
335
+ $image_data = file_get_contents($image_url);
336
+
337
+ $filename = basename($image_url);
338
+
339
+ if(wp_mkdir_p($upload_dir['path']))
340
+
341
+ $file = $upload_dir['path'] . '/' . $filename;
342
+
343
+ else
344
+
345
+ $file = $upload_dir['basedir'] . '/' . $filename;
346
+
347
+ file_put_contents($file, $image_data);
348
+
349
+ $wp_filetype = wp_check_filetype($filename, null );
350
+
351
+ $attachment = array(
352
+
353
+ 'post_mime_type' => $wp_filetype['type'],
354
+
355
+ 'post_title' => sanitize_file_name($filename),
356
+
357
+ 'post_content' => '',
358
+
359
+ 'post_status' => 'inherit'
360
+
361
+ );
362
+
363
+ $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
364
+
365
+ require_once(ABSPATH . 'wp-admin/includes/image.php');
366
+
367
+ $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
368
+
369
+ wp_update_attachment_metadata( $attach_id, $attach_data );
370
+
371
+ set_post_thumbnail( $post_id, $attach_id );
372
+
373
+ //データの挿入に成功していたら移動
374
+
375
+ if(!is_wp_error($post_id)){
376
+
377
+ //ページを移動
378
+
379
+ header('Location: '.get_permalink($post_id));
380
+
381
+ die();
382
+
383
+ } else {
384
+
385
+ $create_thread_error[] = '投稿時にエラーが発生しました。'.$post_id->get_error_message();
386
+
387
+ }
388
+
389
+ }
390
+
391
+ }
392
+
393
+ }
394
+
395
+ add_action('template_redirect', '_my_create_thread');
396
+
397
+ /**
398
+
399
+ * スレッド作成画面でエラーがあれば表示
400
+
401
+ * @global array $create_thread_error
402
+
403
+ */
404
+
405
+ function show_thread_error(){
406
+
407
+ global $create_thread_error;
408
+
409
+ if(!empty($create_thread_error)){
410
+
411
+ echo '<div id="error">';
412
+
413
+ echo implode('<br />', $create_thread_error);
414
+
415
+ echo '</div>';
416
+
417
+ }
418
+
419
+ }
420
+
421
+ ?>
422
+
423
+ ```
424
+
425
+
426
+
427
+
428
+
429
+ 投稿フォームを開くと、真っ白になります