# Get correct image path in kirbytext:before hook plugin

**URL:** <https://forum.getkirby.com/t/get-correct-image-path-in-kirbytext-before-hook-plugin/27706>\
**Category:** Questions\
**Tags:** v3\
**Created:** [January 29, 2023, 5:39pm UTC](https://forum.getkirby.com/t/get-correct-image-path-in-kirbytext-before-hook-plugin/27706 "2023-01-29T17:39:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oliver](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oliver/32/13482_2.png) [@Oliver](https://forum.getkirby.com/u/Oliver)\
**Post date:** [January 29, 2023, 5:39pm UTC](https://forum.getkirby.com/t/get-correct-image-path-in-kirbytext-before-hook-plugin/27706/1 "2023-01-29T17:39:05Z")

</div>

Hi there

I have written a plugin to parse traditional markdown image tags with the kirbytext:before hook. It gets the file name, caption text and alt text from `![Alt text](image.jpg "Alt text")` with a nifty regular expression and spits out the correct html.

```php

Kirby::plugin('ote/markdown-image', [

  'hooks' => [

    'kirbytext:before' => function ($text, $data) {

      $text = preg_replace_callback('/!\[(.*?)\]\((.*?)\)(?: "([^"]+)")?/', function ($matches) use ($data) {

        $alt = $matches[1] ?? null;
        $src = $matches[2];
        $caption = $matches[3] ?? null;

        return '<figure><img src="' . $src . '" alt="' . $alt . '"><figcaption>' . $caption . '</figcaption></figure>';

      }, $text);

      return $text;

    }

  ]

]);

```

However, I am having trouble figuring out how to use the $page variable correctly in the plugin, so I can build the correct image file path with something like `$page->image($src)->url()`, eg returning it like this:

```php
return '<figure><img src="' . $page->image($src)->url() . '" alt="' . $alt . '"><figcaption>' . $caption . '</figcaption></figure>';

```

Obviously this results in an error: Undefined variable $page.

Anyone have any tips?

Best Oliver

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 29, 2023, 5:53pm UTC](https://forum.getkirby.com/t/get-correct-image-path-in-kirbytext-before-hook-plugin/27706/2 "2023-01-29T17:53:53Z")

</div>

You can use `page()`, which refers to the current page.

---

<div class="post-metadata">

**Author:** ![Oliver](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oliver/32/13482_2.png) [@Oliver](https://forum.getkirby.com/u/Oliver)\
**Post date:** [January 29, 2023, 6:01pm UTC](https://forum.getkirby.com/t/get-correct-image-path-in-kirbytext-before-hook-plugin/27706/3 "2023-01-29T18:01:22Z")

</div>

Haha, that was a small fix! Thanks Sonja ☺

---

<div class="post-metadata">

**Author:** ![Oliver](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oliver/32/13482_2.png) [@Oliver](https://forum.getkirby.com/u/Oliver)\
**Post date:** [January 29, 2023, 6:03pm UTC](https://forum.getkirby.com/t/get-correct-image-path-in-kirbytext-before-hook-plugin/27706/4 "2023-01-29T18:03:22Z")

</div>

If anyone else needs to parse regular old markdown image tags — here is the working version with the fix from @texnixe!

```php

Kirby::plugin('ote/markdown-image', [

  'hooks' => [

    'kirbytext:before' => function ($text, $data) {

      $text = preg_replace_callback('/!\[(.*?)\]\((.*?)\)(?: "([^"]+)")?/', function ($matches) use ($data) {

        $alt = $matches[1] ?? null;
        $src = $matches[2];
        $caption = $matches[3] ?? null;

        return '<figure><img src="' . page()->image($src)->url() . '" alt="' . $alt . '"><figcaption>' . $caption . '</figcaption></figure>';

      }, $text);

      return $text;

    }

  ]

]);

```
