回答編集履歴

2

追記

2020/01/13 09:21

投稿

退会済みユーザー
test CHANGED
@@ -37,3 +37,203 @@
37
37
  [関数リファレンス/add shortcode](https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/add_shortcode)
38
38
 
39
39
  > **注意**: ショートコードのコールバック関数は何も出力してはいけません。ショートコードを置き換えるために使われるテキストを返す必要があります。何か出力すると予期せぬ結果につながります。これはフィルター関数についても同様です。いつ、どこから呼び出されるかをあなたは制御できないので、呼び出されたときに副作用を引き起こしてはいけません。
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+ **更に追記**
48
+
49
+ かなり適当ですが、私ならメタボックスを設けてしまいます。
50
+
51
+ ```
52
+
53
+ function extraction_test_func() {
54
+
55
+ #$html = $atts['html'];
56
+
57
+ #$start_leng = $atts['start'];
58
+
59
+ #$end_leng = $atts['end'];
60
+
61
+ $post_id = get_the_ID();
62
+
63
+ $html = get_post_meta( $post_id, 'html', true );
64
+
65
+ $start_leng = get_post_meta( $post_id, 'start', true );
66
+
67
+ $end_leng = get_post_meta( $post_id, 'end', true );
68
+
69
+
70
+
71
+ $ch = curl_init();
72
+
73
+ curl_setopt($ch, CURLOPT_URL, $html);
74
+
75
+ curl_setopt($ch, CURLOPT_HEADER, false);
76
+
77
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
78
+
79
+ curl_setopt($ch, CURLOPT_TIMEOUT, 60);
80
+
81
+ $json = curl_exec($ch);
82
+
83
+ curl_close($ch);
84
+
85
+ $result = json_decode($json, true);
86
+
87
+
88
+
89
+ $start = mb_strpos($json,$start_leng);
90
+
91
+ $end = mb_strpos($json,$end_leng);
92
+
93
+ $extraction = mb_substr($json, $start, $end-$start);
94
+
95
+
96
+
97
+ return $extraction;
98
+
99
+
100
+
101
+ }
102
+
103
+ add_shortcode( 'extraction_test', 'extraction_test_func' );
104
+
105
+
106
+
107
+ add_action( 'add_meta_boxes', function () {
108
+
109
+ add_meta_box(
110
+
111
+ 'custom_fields_meta_box',
112
+
113
+ 'Fields',
114
+
115
+ 'my_custom_fields_meta_box',
116
+
117
+ 'post',
118
+
119
+ 'normal',
120
+
121
+ 'high'
122
+
123
+ );
124
+
125
+ } );
126
+
127
+
128
+
129
+ function my_custom_fields_meta_box() {
130
+
131
+ global $post;
132
+
133
+ $html = get_post_meta( $post->ID, 'html', true );
134
+
135
+ $start = get_post_meta( $post->ID, 'start', true );
136
+
137
+ $end = get_post_meta( $post->ID, 'end', true );
138
+
139
+ ?>
140
+
141
+ <p>HTML: <input type="text" name="html" value="<?php echo $html; ?>" style="width:30%;"></p>
142
+
143
+ <p>START: <input type="text" name="start" value="<?php echo $start; ?>" style="width:30%;"></p>
144
+
145
+ <p>END: <input type="text" name="end" value="<?php echo $end; ?>" style="width:30%;"></p>
146
+
147
+ <?php
148
+
149
+ }
150
+
151
+
152
+
153
+ add_action( 'save_post', function( $post_id ) {
154
+
155
+ if ( isset( $_POST['html'] ) ) {
156
+
157
+ update_post_meta( $post_id, 'html', $_POST['html'] );
158
+
159
+ } else {
160
+
161
+ delete_post_meta( $post_id, 'html' );
162
+
163
+ }
164
+
165
+ if ( isset( $_POST['start'] ) ) {
166
+
167
+ update_post_meta( $post_id, 'start', $_POST['start'] );
168
+
169
+ } else {
170
+
171
+ delete_post_meta( $post_id, 'start' );
172
+
173
+ }
174
+
175
+ if ( isset( $_POST['end'] ) ) {
176
+
177
+ update_post_meta( $post_id, 'end', $_POST['end'] );
178
+
179
+ } else {
180
+
181
+ delete_post_meta( $post_id, 'end' );
182
+
183
+ }
184
+
185
+ } );
186
+
187
+ ```
188
+
189
+
190
+
191
+
192
+
193
+ **釈然としないようなので更に追記しおきます。**
194
+
195
+ 上手くいかないコードの`$atts`の中身はこのようになっています。
196
+
197
+ ```
198
+
199
+ Array
200
+
201
+ (
202
+
203
+ [html] => https://www.yahoo.co.jp/
204
+
205
+ [start] =>
206
+
207
+ [end] =>
208
+
209
+
210
+
211
+ <tbody>
212
+
213
+ <tr>
214
+
215
+ )
216
+
217
+
218
+
219
+ ```
220
+
221
+ これは`shortcode_parse_atts()`関数によって不正なHTMLタグが切り取られた結果で
222
+
223
+ [shortcode_parse_atts() | Function | WordPress Developer Resources](https://developer.wordpress.org/reference/functions/shortcode_parse_atts/)
224
+
225
+ **Reject any unclosed HTML elements.**と書かれおり、仕様なので仕方ありません。
226
+
227
+
228
+
229
+ 回避策は
230
+
231
+ 独自に`the_content`のフィルターフックを作成しその中で独自の`shortcode_parse_atts()`関数もどきを作成して不正なHTMLも通してしまう方法。
232
+
233
+ または
234
+
235
+ []内の`<`や`>`を`&lt;`や`&gt;`に変換して保存してショートコード実行時に元に戻す方法。
236
+
237
+
238
+
239
+ が考えられますが、カスタムフィールドを使用した方が操作性はいいでしょうね。

1

追記

2020/01/13 09:21

投稿

退会済みユーザー
test CHANGED
@@ -1 +1,39 @@
1
1
  `echo $extraction;`ではなく`return $extraction;`でしょうね
2
+
3
+
4
+
5
+ **追記**
6
+
7
+ ちゃんと読んでませんでした。
8
+
9
+
10
+
11
+ 修正すべき点は以下
12
+
13
+ ```
14
+
15
+ $html = $atts[html];
16
+
17
+ $start_leng = $atts[start];
18
+
19
+ $end_leng = $atts[end];
20
+
21
+
22
+
23
+ $html = $atts['html'];
24
+
25
+ $start_leng = $atts['start'];
26
+
27
+ $end_leng = $atts['end'];
28
+
29
+ ```
30
+
31
+ きちんと表示されるソースコードとの違いは変数だけみたいなので変数の中身が問題無いかvar_dumpで確認してください。
32
+
33
+
34
+
35
+ ちなみにechoでも出力されたとのことですがショートコードはreturnしてください。
36
+
37
+ [関数リファレンス/add shortcode](https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/add_shortcode)
38
+
39
+ > **注意**: ショートコードのコールバック関数は何も出力してはいけません。ショートコードを置き換えるために使われるテキストを返す必要があります。何か出力すると予期せぬ結果につながります。これはフィルター関数についても同様です。いつ、どこから呼び出されるかをあなたは制御できないので、呼び出されたときに副作用を引き起こしてはいけません。