# Parenthesis in image caption is jackin' things up

**URL:** <https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623>\
**Category:** Questions\
**Tags:** v2\
**Created:** [March 17, 2016, 1:54pm UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623 "2016-03-17T13:54:55Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [March 17, 2016, 1:54pm UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623/1 "2016-03-17T13:54:55Z")

</div>

The use of parenthesis in a caption is confusing Kirby. I understand why. Consider the following.

`(image: lobster-boat-blockade.jpg caption: From Left to Right: Jay O'Hara (holding photo), Attorney Matt Pawa (speaking to reporters))`

The parenthesis in the caption are closing the “shortcode”. I could tell the client to use brackets, but that’s just another thing they have to remember. I’d be nice if the “shortcodes” could use some other less common character to open and close them.

Are there any workarounds for this?

---

<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:** [March 17, 2016, 1:58pm UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623/2 "2016-03-17T13:58:27Z")

</div>

Unfortunately, there still is no way to escape parenthesis inside Kirbytags, at least not any I know of. So, you can’t use parenthesis inside Kirbytags. A work around would be to use some other characters like square brackets and replace those with a Kirbytext post filter, but as you said, it’s more difficult for clients to remember.

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [March 17, 2016, 2:00pm UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623/3 "2016-03-17T14:00:18Z")

</div>

This issue has come up a [few times](https://github.com/getkirby/kirby/issues/45) before and there was a fix, but that fix was removed again because it wasn’t reliable.

See [this topic](https://forum.getkirby.com/t/way-to-use-parenthesis-inside-kirbytag/861?u=lukasbestle) for workarounds and discussion.

---

<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:** [March 17, 2016, 2:01pm UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623/4 "2016-03-17T14:01:19Z")

</div>

See also [this post](https://forum.getkirby.com/t/way-to-use-parenthesis-inside-kirbytag/861/7)

---

<div class="post-metadata">

**Author:** ![illycz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/illycz/32/5190_2.png) [@illycz](https://forum.getkirby.com/u/illycz)\
**Post date:** [February 21, 2019, 10:15am UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623/5 "2019-02-21T10:15:00Z")

</div>

This is not working with Kirby 3. Any suggestion?

Thanks

---

<div class="post-metadata">

**Author:** ![illycz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/illycz/32/5190_2.png) [@illycz](https://forum.getkirby.com/u/illycz)\
**Post date:** [February 21, 2019, 10:18am UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623/6 "2019-02-21T10:18:47Z")

</div>

OK I solve it by adding caption meta field to image so image kirbytag get it from…

---

<div class="post-metadata">

**Author:** ![manuelmoreale](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/manuelmoreale/32/839_2.png) [@manuelmoreale](https://forum.getkirby.com/u/manuelmoreale)\
**Post date:** [February 21, 2019, 5:28pm UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623/7 "2019-02-21T17:28:24Z")

</div>

So I’m not sure this is the most efficient way of doing it but this solution seems to be working in my tests

```auto
# Fix the () nested inside the kirbytags
'kirbytags:before' => function ($text, array $data = [], array $options = []) {

    // KirbyTags have not been parsed
    $text = preg_replace_callback('/\((?:[^()]+|(?R))*+\)/', function ($matches) {
        
        // Loop through each tag and perform a second serach
        foreach ($matches as $match) :

            // First trim the () at the beginning and at the end
            $match = substr($match , 1 , -1);

            // Then look for () inside the string and replace them with another character
            $match = str_replace('(', '⎣', $match);
            $match = str_replace(')', '⎦', $match);

            // Add back the two () at the beginning and the end
            $match = "({$match})";

            // Return the string
            return $match;

        endforeach;

        # Return al the kirbytags
        return $matches;

    }, $text);

    return $text;
},

# Add back the () we swapped previously
'kirbytags:after' => function ($text, array $data = [], array $options = []) {

    $text = preg_replace_callback('/\⎣|\⎦/', function ($matches) {
        
        // Loop through each tag and swap the characters
        foreach ($matches as $match) :

            // Swap back the correct ()
            $match = ($match == "⎣" ) ? "(" : ")";
            
            // Return the string
            return $match;

        endforeach;

        # Return al the kirbytags
        return $matches;

    }, $text);

    return $text;
},

```

Pretty sure it can be improved though

---

<div class="post-metadata">

**Author:** ![parislettau](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/parislettau/32/8524_2.png) [@parislettau](https://forum.getkirby.com/u/parislettau)\
**Post date:** [March 9, 2020, 9:18am UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623/8 "2020-03-09T09:18:55Z")

</div>

How/Where did you include this snippet? I tried as a plugin but couldn’t get it to work (new to Kirby!)

---

<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:** [March 9, 2020, 9:30am UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623/9 "2020-03-09T09:30:58Z")

</div>

@parislettau Which version of Kirby are you using? The solution in this thread is for Kirby 2 and the syntax and way to include plugins has changed.

---

<div class="post-metadata">

**Author:** ![parislettau](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/parislettau/32/8524_2.png) [@parislettau](https://forum.getkirby.com/u/parislettau)\
**Post date:** [March 9, 2020, 10:05am UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623/10 "2020-03-09T10:05:14Z")

</div>

@texnixe thanks for quick response! I’m using Kirby 3 – is there a solution for Kirby 3?

---

<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:** [March 9, 2020, 10:26am UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623/11 "2020-03-09T10:26:06Z")

</div>

For Kirby 3 you can use a kirbytags:before hook:

For example, you could add this hook in `config.php`:

```auto
 'hooks' => [
        'kirbytags:before' => function ($text, $data, $options) {
               // KirbyTags have not been parsed
    $text = preg_replace_callback('/\((?:[^()]+|(?R))*+\)/', function ($matches) {
        
        // Loop through each tag and perform a second serach
        foreach ($matches as $match) :

            // First trim the () at the beginning and at the end
            $match = substr($match , 1 , -1);

            // Then look for () inside the string and replace them with another character
            $match = str_replace('(', '⎣', $match);
            $match = str_replace(')', '⎦', $match);

            // Add back the two () at the beginning and the end
            $match = "({$match})";

            // Return the string
            return $match;

        endforeach;

        # Return al the kirbytags
        return $matches;

    }, $text);

    return $text;
        }
    ]

```

(not tested)

Alternatively, you can register this hook in a plugin.

---

<div class="post-metadata">

**Author:** ![parislettau](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/parislettau/32/8524_2.png) [@parislettau](https://forum.getkirby.com/u/parislettau)\
**Post date:** [March 9, 2020, 11:19am UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623/12 "2020-03-09T11:19:58Z")

</div>

Perfect, thank you so much @texnixe

I made one small change to replace each `$match` with square brackets (which then render as round brackets) instead of the `'⎣'` and `'⎦'` characters:

So for anyone interested my final plugin in site/plugins/kirby-captions-brackets/index.php (which is working after some brief testing) is:

```
<?php

 Kirby::plugin('alias/kirby-captions-brackets', [
  
  'hooks' => [
        'kirbytags:before' => function ($text, $data, $options) {
               // KirbyTags have not been parsed
    $text = preg_replace_callback('/\((?:[^()]+|(?R))*+\)/', function ($matches) {
        
        // Loop through each tag and perform a second serach
        foreach ($matches as $match) :

            // First trim the () at the beginning and at the end
            $match = substr($match , 1 , -1);

            // Then look for () inside the string and replace them with another character
            $match = str_replace('(', '[', $match);
            $match = str_replace(')', ']', $match);

            // Add back the two () at the beginning and the end
            $match = "({$match})";

            // Return the string
            return $match;

        endforeach;

        # Return al the kirbytags
        return $matches;

    }, $text);

    return $text;
        }
    ]

]);
```

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:23pm UTC](https://forum.getkirby.com/t/parenthesis-in-image-caption-is-jackin-things-up/3623/13 "2025-02-06T17:23:04Z")

</div>

Archived on February 6, 2025, 5:23pm UTC


