質問編集履歴

4

誤字修正

2018/01/23 16:11

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -342,6 +342,8 @@
342
342
 
343
343
  ```php
344
344
 
345
+ <div class="twareadayo">
346
+
345
347
  <?php
346
348
 
347
349
  function post_twitter_field() {
@@ -364,7 +366,83 @@
364
366
 
365
367
  echo '<div style="margin-bottom:5px;"><label><input type="checkbox" name="whether_to_post" id="whether_to_post" value="checked" '.$checked.'>Twitterに投稿する</label></div>';
366
368
 
367
- echo '<textarea name="twitter_body" id="twitter_body" cols="50" rows="5" style="width: 100%;" readonly>'.$twitter_body.'</textarea>'; ?>
369
+ echo '<textarea name="twitter_body" id="twitter_body" cols="50" rows="5" style="width: 100%;" readonly>'.$twitter_body.'</textarea>';
370
+
371
+ echo <<< EOF
372
+
373
+ <script>
374
+
375
+ (function($) {
376
+
377
+ // ロード時にチェックが入っていれば編集可に、入っていなかったら可に。
378
+
379
+ if($("#whether_to_post").prop('checked')) {
380
+
381
+ $("#twitter_body").prop('readonly', false);
382
+
383
+ $('#twitter_body').css({'background-color':'','color':'', 'pointer-events':''});
384
+
385
+ } else {
386
+
387
+ $("#twitter_body").prop('readonly', true);
388
+
389
+ $('#twitter_body').css({'background-color':'#fff','color':'#ccc', 'pointer-events':'none'});
390
+
391
+ }
392
+
393
+
394
+
395
+ // タイトルを自動入力(フォーカスが外れた時に反映)
396
+
397
+ $('#title').change(function() {
398
+
399
+ if ($('#twitter_body').val() == '') {
400
+
401
+ var title = $('input[name="post_title"]').val();
402
+
403
+ $('#twitter_body').val(title + "\n");
404
+
405
+ }
406
+
407
+ });
408
+
409
+
410
+
411
+ // チェックを入れると編集可能に、外すと不可に。
412
+
413
+ $('input[name="whether_to_post"]').change(function() {
414
+
415
+ if($("#whether_to_post").prop('checked')) {
416
+
417
+ $("#twitter_body").prop('readonly', false);
418
+
419
+ $('#twitter_body').css({'background-color':'','color':'', 'pointer-events':''});
420
+
421
+ } else {
422
+
423
+ $("#twitter_body").prop('readonly', true);
424
+
425
+ $('#twitter_body').css({'background-color':'#fff','color':'#ccc', 'pointer-events':'none'});
426
+
427
+ }
428
+
429
+ });
430
+
431
+
432
+
433
+ })(jQuery);
434
+
435
+ </script>
436
+
437
+ EOF;
438
+
439
+
440
+
441
+ }
442
+
443
+ ?>
444
+
445
+ </div>
368
446
 
369
447
  ```
370
448
 

3

誤字修正

2018/01/23 16:11

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -334,11 +334,11 @@
334
334
 
335
335
  ###■問題(ユーザー投稿画面で表示出来ない)
336
336
 
337
- ユーザー投稿型のサイトなので、【single.php】でも表示させたいのですけれど、これがうまくいきません。
337
+ ユーザー投稿型のサイトなので、【page.php】でも表示させたいのですけれど、これがうまくいきません。
338
-
339
-
340
-
338
+
339
+
340
+
341
- single.php】
341
+ page.php】
342
342
 
343
343
  ```php
344
344
 
@@ -372,4 +372,6 @@
372
372
 
373
373
  長くなってしまってすみません。
374
374
 
375
+
376
+
375
- ユーザー投稿画面に表示するためには、上記のどこを直せばいいか、ご指摘いただけませんでしょうか?
377
+ ユーザー投稿画面に表示するためには、【page.php】のどこを直せばいいか、ご指摘いただけませんでしょうか?

2

コードの修正

2018/01/23 15:56

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- カスタムフィールドを2所に表示したい。
1
+ wordpressでカスタムフィールドの「入力エリア」を2所に表示したい。
test CHANGED
@@ -1,26 +1,340 @@
1
- wordpressでカスタムフィールドを表示したいです。
2
-
3
-
4
-
5
- ###機能
6
-
7
- 「記事をツイッターに投稿するかどうか」のチェックボックスを表示するカスタムフィールドです。
8
-
9
-
10
-
11
- ###現状
12
-
13
- 管理画面には【functions.php】の記入で表示できています。
14
-
15
-
16
-
17
- ただユーザー投稿型のサイトなので、【single.php】でも表示させたいですけれど、うまくいきません
18
-
19
-
20
-
21
- どこを直せばいか、ご指摘いただけませんでしょうか?
22
-
23
-
1
+ ###■目的
2
+
3
+ ユーザー投稿サイトです。
4
+
5
+
6
+
7
+ 「記事をツイッターに投稿するかどうか」のチェックボックスを
8
+
9
+
10
+
11
+ ①管理画面
12
+
13
+ ②ユーザー投稿画面
14
+
15
+
16
+
17
+ 2ヵ所に表示たいです。
18
+
19
+
20
+
21
+ ###■現状(①管理画面は表示できてる)
22
+
23
+ 管理画面への表示は次のコードを【functions.php】と【post-twitter.php】に書くことで表示できています。
24
+
25
+
26
+
27
+ 【functions.php】
28
+
29
+ ```php
30
+
31
+ /**
32
+
33
+ * ツイッター自動投稿
34
+
35
+ * ----------------------------------------------------------------------------
36
+
37
+ */
38
+
39
+
40
+
41
+ // ここではTwitterの投稿だけを行う
42
+
43
+ add_action('transition_post_status', 'post_twitter', 10, 3);
44
+
45
+ function post_twitter($new_status, $old_status, $post) {
46
+
47
+ if ($new_status == 'publish') {
48
+
49
+ // コマンドでTwitterに投稿するファイルを実行(引数にpost_idを渡している)
50
+
51
+ $command = "/usr/bin/php ".__DIR__."/post-twitter.php $post->ID > /dev/null &";
52
+
53
+ exec($command);
54
+
55
+ }
56
+
57
+ }
58
+
59
+
60
+
61
+ // 管理画面が開いたときに実行
62
+
63
+ add_action('admin_menu', 'add_post_twitter_fields');
64
+
65
+ // カスタムフィールドを投稿画面に追加
66
+
67
+ function add_post_twitter_fields() {
68
+
69
+ // フィールドを追加
70
+
71
+ add_meta_box('post_twitter', 'Twitter', 'post_twitter_field', 'post', 'side', 'high');
72
+
73
+ }
74
+
75
+
76
+
77
+ // 投稿画面に表示するフォームのHTMLソース
78
+
79
+ function post_twitter_field() {
80
+
81
+ global $post;
82
+
83
+
84
+
85
+ $twitter_body = get_post_meta($post->ID, 'twitter_body', true);
86
+
87
+ $checked = get_post_meta($post->ID, 'whether_to_post', true);
88
+
89
+ if ($checked == 'checked') {
90
+
91
+ $checked = 'checked="'.$checked.'""';
92
+
93
+ }
94
+
95
+
96
+
97
+ echo '<div style="margin-bottom:5px;"><label><input type="checkbox" name="whether_to_post" id="whether_to_post" value="checked" '.$checked.'>Twitterに投稿する</label></div>';
98
+
99
+ echo '<textarea name="twitter_body" id="twitter_body" cols="50" rows="5" style="width: 100%;" readonly>'.$twitter_body.'</textarea>';
100
+
101
+ echo <<< EOF
102
+
103
+ <script>
104
+
105
+ (function($) {
106
+
107
+ // ロード時にチェックが入っていれば編集可に、入っていなかったら可に。
108
+
109
+ if($("#whether_to_post").prop('checked')) {
110
+
111
+ $("#twitter_body").prop('readonly', false);
112
+
113
+ $('#twitter_body').css({'background-color':'','color':'', 'pointer-events':''});
114
+
115
+ } else {
116
+
117
+ $("#twitter_body").prop('readonly', true);
118
+
119
+ $('#twitter_body').css({'background-color':'#fff','color':'#ccc', 'pointer-events':'none'});
120
+
121
+ }
122
+
123
+
124
+
125
+ // タイトルを自動入力(フォーカスが外れた時に反映)
126
+
127
+ $('#title').change(function() {
128
+
129
+ if ($('#twitter_body').val() == '') {
130
+
131
+ var title = $('input[name="post_title"]').val();
132
+
133
+ $('#twitter_body').val(title + "\n");
134
+
135
+ }
136
+
137
+ });
138
+
139
+
140
+
141
+ // チェックを入れると編集可能に、外すと不可に。
142
+
143
+ $('input[name="whether_to_post"]').change(function() {
144
+
145
+ if($("#whether_to_post").prop('checked')) {
146
+
147
+ $("#twitter_body").prop('readonly', false);
148
+
149
+ $('#twitter_body').css({'background-color':'','color':'', 'pointer-events':''});
150
+
151
+ } else {
152
+
153
+ $("#twitter_body").prop('readonly', true);
154
+
155
+ $('#twitter_body').css({'background-color':'#fff','color':'#ccc', 'pointer-events':'none'});
156
+
157
+ }
158
+
159
+ });
160
+
161
+
162
+
163
+ })(jQuery);
164
+
165
+ </script>
166
+
167
+ EOF;
168
+
169
+
170
+
171
+ }
172
+
173
+
174
+
175
+ // 下書き・公開・更新ボタンが押されたときに実行される(ゴミ箱に移動したときも呼ばれるかも?)
176
+
177
+ add_action('save_post', 'save_post_twitter', 10, 3);
178
+
179
+ // 設定したカスタムフィールドの値をDBに書き込むコード
180
+
181
+ function save_post_twitter($post_id, $post) {
182
+
183
+ if (isset($_POST['whether_to_post']) || isset($_POST['twitter_body'])) {
184
+
185
+ $post_type = $post->post_type;
186
+
187
+ if ($post_type == 'post') {
188
+
189
+ $whether_to_post = $_POST['whether_to_post'] == '' ? '': $_POST['whether_to_post'];
190
+
191
+ update_post_meta($post_id, 'whether_to_post', $whether_to_post);
192
+
193
+
194
+
195
+ $twitter_body = $_POST['twitter_body'] == '' ? '': $_POST['twitter_body'];
196
+
197
+ update_post_meta($post_id, 'twitter_body', $twitter_body);
198
+
199
+ }
200
+
201
+ }
202
+
203
+ }
204
+
205
+ ```
206
+
207
+ 【post-twitter.php】
208
+
209
+
210
+
211
+ ```php
212
+
213
+ <?php
214
+
215
+ require __DIR__. '/../../../wp-load.php';
216
+
217
+ require_once __DIR__."/twitteroauth/autoload.php";
218
+
219
+ use Abraham\TwitterOAuth\TwitterOAuth;
220
+
221
+
222
+
223
+ class Twitter {
224
+
225
+ private static $consumer_key = "hogehoge";
226
+
227
+ private static $consumer_secret = "hogehoge";
228
+
229
+ private static $access_token = "hogehoge";
230
+
231
+ private static $access_token_secret = "hogehoge";
232
+
233
+
234
+
235
+ public function post($post) {
236
+
237
+
238
+
239
+ // Twitterへ投稿
240
+
241
+ $connection = new TwitterOAuth(self::$consumer_key, self::$consumer_secret, self::$access_token, self::$access_token_secret);
242
+
243
+ $result = $connection->post("statuses/update", ["status" => $post]);
244
+
245
+
246
+
247
+ if ($connection->getLastHttpCode() == 200) {
248
+
249
+ echo "succes";
250
+
251
+ return;
252
+
253
+ } else {
254
+
255
+ // 投稿に失敗したらエラー内容をメールで送る
256
+
257
+ mb_language("Japanese");
258
+
259
+ mb_internal_encoding("UTF-8");
260
+
261
+
262
+
263
+ $to = 'to@example.com';
264
+
265
+ $subject = 'Twitter Debug';
266
+
267
+ $message = "Twitterへの投稿に失敗しました。\n$post\n\nエラーコード:".$result->errors[0]->code."\nエラーメッセージ:".$result->errors[0]->message;
268
+
269
+ $headers = 'From: from@example.com' . "\r\n";
270
+
271
+
272
+
273
+ mb_send_mail($to, $subject, $message, $headers);
274
+
275
+ }
276
+
277
+ }
278
+
279
+ }
280
+
281
+ // functions.phpに書いたtransition_post_statusは、
282
+
283
+ // save_postより先に実行されるので、念のためここで2秒遅らせてる。
284
+
285
+ sleep(2);
286
+
287
+
288
+
289
+ // post_id取得
290
+
291
+ $post_id = isset($argv[1]) ? $argv[1] : null;
292
+
293
+
294
+
295
+ $whether_to_post = get_post_meta($post_id, 'whether_to_post', true);
296
+
297
+
298
+
299
+ // checkedじゃなかったら処理中断
300
+
301
+ if ($whether_to_post != 'checked') return;
302
+
303
+
304
+
305
+ $twitter = new Twitter();
306
+
307
+
308
+
309
+ // 投稿内容を取得
310
+
311
+ $post = get_post_meta($post_id, 'twitter_body', true);
312
+
313
+ // post_url取得
314
+
315
+ $post_url = get_permalink($post_id);
316
+
317
+ if(strpos($post_url,'http://') !== false){
318
+
319
+ $post_url = str_replace('http://', 'https://', $post_url);
320
+
321
+ }
322
+
323
+ // 投稿内容とpost_urlを結合
324
+
325
+ $post .= "\n".$post_url;
326
+
327
+ $twitter->post($post);
328
+
329
+ ```
330
+
331
+
332
+
333
+
334
+
335
+ ###■問題(ユーザー投稿画面で表示出来ない)
336
+
337
+ ユーザー投稿型のサイトなので、【single.php】でも表示させたいのですけれど、これがうまくいきません。
24
338
 
25
339
 
26
340
 
@@ -28,192 +342,34 @@
28
342
 
29
343
  ```php
30
344
 
345
+ <?php
346
+
31
- <div class="auto_tw">
347
+ function post_twitter_field() {
348
+
32
-
349
+ global $post;
350
+
351
+
352
+
33
- <?php echo get_post_meta( $post->ID , 'twitter_body' , true ); ?>
353
+ $twitter_body = get_post_meta($post->ID, 'twitter_body', true);
354
+
34
-
355
+ $checked = get_post_meta($post->ID, 'whether_to_post', true);
356
+
35
- </div>
357
+ if ($checked == 'checked') {
358
+
359
+ $checked = 'checked="'.$checked.'""';
360
+
361
+ }
362
+
363
+
364
+
365
+ echo '<div style="margin-bottom:5px;"><label><input type="checkbox" name="whether_to_post" id="whether_to_post" value="checked" '.$checked.'>Twitterに投稿する</label></div>';
366
+
367
+ echo '<textarea name="twitter_body" id="twitter_body" cols="50" rows="5" style="width: 100%;" readonly>'.$twitter_body.'</textarea>'; ?>
36
368
 
37
369
  ```
38
370
 
39
371
 
40
372
 
41
- 【functions.php】
373
+ 長くなってしまってすみません。
42
-
43
- ```php
374
+
44
-
45
- /**
46
-
47
- * ツイッター自動投稿
48
-
49
- * ----------------------------------------------------------------------------
50
-
51
- */
52
-
53
-
54
-
55
- // ここではTwitterの投稿だけを行う
56
-
57
- add_action('transition_post_status', 'post_twitter', 10, 3);
58
-
59
- function post_twitter($new_status, $old_status, $post) {
60
-
61
- if ($new_status == 'publish') {
62
-
63
- // コマンドでTwitterに投稿するファイルを実行(引数にpost_idを渡している)
64
-
65
- $command = "/usr/bin/php ".__DIR__."/post-twitter.php $post->ID > /dev/null &";
66
-
67
- exec($command);
68
-
69
- }
70
-
71
- }
72
-
73
-
74
-
75
- // 管理画面が開いたときに実行
76
-
77
- add_action('admin_menu', 'add_post_twitter_fields');
78
-
79
- // カスタムフィールドを投稿画面に追加
80
-
81
- function add_post_twitter_fields() {
82
-
83
- // フィールドを追加
84
-
85
- add_meta_box('post_twitter', 'Twitter', 'post_twitter_field', 'post', 'side', 'high');
86
-
87
- }
88
-
89
-
90
-
91
- // 投稿画面に表示するフォームのHTMLソース
92
-
93
- function post_twitter_field() {
94
-
95
- global $post;
96
-
97
-
98
-
99
- $twitter_body = get_post_meta($post->ID, 'twitter_body', true);
100
-
101
- $checked = get_post_meta($post->ID, 'whether_to_post', true);
102
-
103
- if ($checked == 'checked') {
104
-
105
- $checked = 'checked="'.$checked.'""';
106
-
107
- }
108
-
109
-
110
-
111
- echo '<div style="margin-bottom:5px;"><label><input type="checkbox" name="whether_to_post" id="whether_to_post" value="checked" '.$checked.'>Twitterに投稿する</label></div>';
112
-
113
- echo '<textarea name="twitter_body" id="twitter_body" cols="50" rows="5" style="width: 100%;" readonly>'.$twitter_body.'</textarea>';
114
-
115
- echo <<< EOF
116
-
117
- <script>
118
-
119
- (function($) {
120
-
121
- // ロド時チェックが入っていれば編集可に、入ってら可に。
375
+ ザー投稿画面表示するため上記のどこを直せば、ご指摘いだけませんでしょうか?
122
-
123
- if($("#whether_to_post").prop('checked')) {
124
-
125
- $("#twitter_body").prop('readonly', false);
126
-
127
- $('#twitter_body').css({'background-color':'','color':'', 'pointer-events':''});
128
-
129
- } else {
130
-
131
- $("#twitter_body").prop('readonly', true);
132
-
133
- $('#twitter_body').css({'background-color':'#fff','color':'#ccc', 'pointer-events':'none'});
134
-
135
- }
136
-
137
-
138
-
139
- // タイトルを自動入力(フォーカスが外れた時に反映)
140
-
141
- $('#title').change(function() {
142
-
143
- if ($('#twitter_body').val() == '') {
144
-
145
- var title = $('input[name="post_title"]').val();
146
-
147
- $('#twitter_body').val(title + "\n");
148
-
149
- }
150
-
151
- });
152
-
153
-
154
-
155
- // チェックを入れると編集可能に、外すと不可に。
156
-
157
- $('input[name="whether_to_post"]').change(function() {
158
-
159
- if($("#whether_to_post").prop('checked')) {
160
-
161
- $("#twitter_body").prop('readonly', false);
162
-
163
- $('#twitter_body').css({'background-color':'','color':'', 'pointer-events':''});
164
-
165
- } else {
166
-
167
- $("#twitter_body").prop('readonly', true);
168
-
169
- $('#twitter_body').css({'background-color':'#fff','color':'#ccc', 'pointer-events':'none'});
170
-
171
- }
172
-
173
- });
174
-
175
-
176
-
177
- })(jQuery);
178
-
179
- </script>
180
-
181
- EOF;
182
-
183
-
184
-
185
- }
186
-
187
-
188
-
189
- // 下書き・公開・更新ボタンが押されたときに実行される(ゴミ箱に移動したときも呼ばれるかも?)
190
-
191
- add_action('save_post', 'save_post_twitter', 10, 3);
192
-
193
- // 設定したカスタムフィールドの値をDBに書き込むコード
194
-
195
- function save_post_twitter($post_id, $post) {
196
-
197
- if (isset($_POST['whether_to_post']) || isset($_POST['twitter_body'])) {
198
-
199
- $post_type = $post->post_type;
200
-
201
- if ($post_type == 'post') {
202
-
203
- $whether_to_post = $_POST['whether_to_post'] == '' ? '': $_POST['whether_to_post'];
204
-
205
- update_post_meta($post_id, 'whether_to_post', $whether_to_post);
206
-
207
-
208
-
209
- $twitter_body = $_POST['twitter_body'] == '' ? '': $_POST['twitter_body'];
210
-
211
- update_post_meta($post_id, 'twitter_body', $twitter_body);
212
-
213
- }
214
-
215
- }
216
-
217
- }
218
-
219
- ```

1

誤字修正

2018/01/23 15:49

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
  <div class="auto_tw">
32
32
 
33
- <?php echo get_post_meta( $post->ID , 'Twitter' , true ); ?>
33
+ <?php echo get_post_meta( $post->ID , 'twitter_body' , true ); ?>
34
34
 
35
35
  </div>
36
36