質問編集履歴

4

タイトルが質問内容とあってなかったため

2022/01/22 17:29

投稿

captain-coco
captain-coco

スコア10

test CHANGED
@@ -1 +1 @@
1
- WordPress 投稿した画像をaタで囲みたい
1
+ WordPress 投稿した画像をトリミンたい
test CHANGED
File without changes

3

そもそもの解決できてない場所がもっと遡った場所だったため修正

2022/01/22 17:29

投稿

captain-coco
captain-coco

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,27 +1,8 @@
1
1
  **<前提・実現したいこと>**
2
- ブロックエディタで追加した画像に対して、aタグで囲んで
2
+ ブロックエディタで追加した画像に対して、
3
- 独自のクラス追加とhrefにimgのsrc定したい
3
+ サイズ定しトリミングしたい
4
- figure > a > img ← figure と img の間に aタグを入れたい
5
4
 
6
5
  **<発生している問題・エラーメッセージ>**
7
- functions.php にadd_filterを記述している
8
- imgをaタグで囲むことはできたが、
9
- imgのsrcの内容を取得してaのhrefに設定するやり方がわからない
6
+ 投稿た画像に対しトリミング可能かどうかわからない
10
7
 
11
- **<該当のソースコード>**
12
- ```functions.pphp
13
- add_filter( 'the_content', 'the_content_filter', 10 );
14
- function the_content_filter( $content ) {
15
- if(is_single()) {
16
- $content = preg_replace('/(<img .*?wp-image.*?>)/i', '<a class="hoge">$1</a>', $content);
17
- return $content;
18
- }
19
- }
20
- ```
21
8
 
22
- **<実現したいコード>**
23
- ```php
24
- <a href="https://************/assets/images/hoge.jpg"" class="hoge">
25
-  <img src="https://************/assets/images/hoge.jpg" alt="">
26
- </a>
27
- ```

2

実現したいコードを追記

2022/01/21 16:02

投稿

captain-coco
captain-coco

スコア10

test CHANGED
File without changes
test CHANGED
@@ -13,10 +13,15 @@
13
13
  add_filter( 'the_content', 'the_content_filter', 10 );
14
14
  function the_content_filter( $content ) {
15
15
  if(is_single()) {
16
- $content = preg_replace('/(<img .*?wp-image.*?>)/i', '<a class="modal">$1</a>', $content);
16
+ $content = preg_replace('/(<img .*?wp-image.*?>)/i', '<a class="hoge">$1</a>', $content);
17
17
  return $content;
18
18
  }
19
19
  }
20
20
  ```
21
21
 
22
-
22
+ **<実現したいコード>**
23
+ ```php
24
+ <a href="https://************/assets/images/hoge.jpg"" class="hoge">
25
+  <img src="https://************/assets/images/hoge.jpg" alt="">
26
+ </a>
27
+ ```

1

最初のコードから変更しました

2022/01/21 15:58

投稿

captain-coco
captain-coco

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,21 +1,22 @@
1
1
  **<前提・実現したいこと>**
2
- ブロックエディタで追加した画像に対して、aタグで囲んで独自のクラスを付けたい
2
+ ブロックエディタで追加した画像に対して、aタグで囲んで
3
+ 独自のクラス追加とhrefにimgのsrcを設定したい
3
4
  figure > a > img ← figure と img の間に aタグを入れたい
4
5
 
5
6
  **<発生している問題・エラーメッセージ>**
6
- functions.php にimage_send_to_editorをフックするよう記述しているのですが、
7
+ functions.php にadd_filterを記述している
8
+ imgをaタグで囲むことはできたが、
7
- 投稿記事を検証画面で見てもimgタグをaタグ囲んでいない
9
+ imgのsrcの内容取得してaのhrefに設定するやり方わからない
8
10
 
9
11
  **<該当のソースコード>**
10
12
  ```functions.pphp
11
- function my_image_wrap( $html, $id, $caption, $title, $align, $url, $size, $alt) {
13
+ add_filter( 'the_content', 'the_content_filter', 10 );
14
+ function the_content_filter( $content ) {
15
+ if(is_single()) {
12
- $html = '<a class="hoge" href="' .$url. '">' .$html .'</a>';
16
+ $content = preg_replace('/(<img .*?wp-image.*?>)/i', '<a class="modal">$1</a>', $content);
13
- return $html;
17
+ return $content;
18
+ }
14
19
  }
15
- add_action( 'image_send_to_editor', 'my_image_wrap', 100 ,8);
16
20
  ```
17
- **<自分で調べたことや試したこと>**
18
- 上記コードは、
19
- https://www.saka-en.com/wordpress/wordpress-insert-img-in-div/
20
- こちらを参考に書きました。
21
21
 
22
+