# Simple "Like"-feature

**URL:** <https://forum.getkirby.com/t/simple-like-feature/3492>\
**Category:** Questions\
**Tags:** v2\
**Created:** [March 3, 2016, 8:51am UTC](https://forum.getkirby.com/t/simple-like-feature/3492 "2016-03-03T08:51:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 3, 2016, 8:51am UTC](https://forum.getkirby.com/t/simple-like-feature/3492/1 "2016-03-03T08:51:17Z")

</div>

Good morning everyone! I was wondering if it was easy to build a simple “Like”-feature with Kirby? Basically I have a blog and on the blogpost-page a user should be able to click a “Like”-button that increments a field in blogpost.txt by 1. I’m not worried about multiple clicks for now. I’ll take care of that later.

Is that possible? Any tips that point me in the right direction would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![tnViking](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tnviking/32/4407_2.png) [@tnViking](https://forum.getkirby.com/u/tnViking)\
**Post date:** [March 3, 2016, 8:52am UTC](https://forum.getkirby.com/t/simple-like-feature/3492/2 "2016-03-03T08:52:59Z")

</div>

Hello @rppld, there’s a plugin for Kirby called [Kudos](https://github.com/hemorej/kirby-kudos), you should check it out.

---

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 3, 2016, 8:55am UTC](https://forum.getkirby.com/t/simple-like-feature/3492/3 "2016-03-03T08:55:51Z")

</div>

Hey, thanks! I’ve already looked into that, but I wanted to check if there’s an easy way to get by without jQuery first 🙂 If it turns out to be too much effort I’ll go for the plugin.

---

<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 3, 2016, 9:00am UTC](https://forum.getkirby.com/t/simple-like-feature/3492/4 "2016-03-03T09:00:41Z")

</div>

A very basic approach is to just call a script that updates the field on every click with the `$page->update()` method.

---

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 3, 2016, 9:10am UTC](https://forum.getkirby.com/t/simple-like-feature/3492/5 "2016-03-03T09:10:22Z")

</div>

Ah, nice! Thanks for that. Didn’t know about the `update()`-method. I’ll just wrap it in a form and post via ajax then. Hope it’s really that easy, hehe.

---

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 3, 2016, 10:38am UTC](https://forum.getkirby.com/t/simple-like-feature/3492/6 "2016-03-03T10:38:46Z")

</div>

@texnixe Would you suggest it’s best to put this update function in a plugin? E.g. like so:

**site/plugins/like/like.php**

```
<?php

  function like() {
    // Update function
  }

?>

```

How do I call the `like()` function via the form in the template then?

---

<div class="post-metadata">

**Author:** ![pedroborges](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/pedroborges/32/7038_2.png) [@pedroborges](https://forum.getkirby.com/u/pedroborges)\
**Post date:** [March 3, 2016, 11:09am UTC](https://forum.getkirby.com/t/simple-like-feature/3492/7 "2016-03-03T11:09:24Z")

</div>

Since you are using ajax, you could register a route in your plugin and call the like function from there passing the uri as argument:

```auto
kirby()->routes(array(  
    array(
        'pattern' => '(:all)/like',
        'method' => 'POST',
        'action' => function($uri) {
            // check if page exists

            like($uri);

            // ...
        }
    )
));

```

---

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 3, 2016, 11:32am UTC](https://forum.getkirby.com/t/simple-like-feature/3492/8 "2016-03-03T11:32:13Z")

</div>

Oh, that would be elegant, you’re right @pedroborges, thanks! Let me see if I can make this work!

---

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 3, 2016, 1:15pm UTC](https://forum.getkirby.com/t/simple-like-feature/3492/9 "2016-03-03T13:15:31Z")

</div>

Alright, I feel like I’m almost there, but it doesn’t work yet.

**site/plugins/like/like.php**

```
<?php

kirby()->routes([

  [
    'pattern' => "/like:(:any)",
    'method' => 'POST',
    'action' => function($uid) {
      $post = page('blog/' + $uid);
      try {
        $post->update(array(
          'likes' => 1000
        ));
        echo 'The page has been updated';
      } catch(Exception $e) {
        echo $e->getMessage();
      }
    }
  ]

]);

```

I’m making the Ajax call like that:

```
const btnLike = document.querySelector('.js-like');
if (btnLike) {
  btnLike.addEventListener('click', function() {
    console.log('like clicked');
    request
      .post('/like:super-t')
      .end(function(err, res) {
        if (!err) console.log('liked');
        else console.log('error');
      });
  });
}

```

and it’s logging “liked” in the console, so that should be fine. “super-t” is the uid of a blog post. Any idea what I’m missing here?

---

<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 3, 2016, 1:26pm UTC](https://forum.getkirby.com/t/simple-like-feature/3492/10 "2016-03-03T13:26:53Z")

</div>

You are using the `+` operator instead of `.` (probably because you wrote JS before, happens to me all the time 😃):

```auto
$post = page('blog/' . $uid);

```

If it still doesn’t work, could you please request the URL directly in your browser without AJAX and check if there is any output?

---

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 3, 2016, 1:33pm UTC](https://forum.getkirby.com/t/simple-like-feature/3492/11 "2016-03-03T13:33:15Z")

</div>

Ah, true! Fixed the operator, still no luck with updating the likes count though. If I access `http://localhost:3000/like:super-t` in the browser it shows the homepage. I’ve set the blog-page to be the homepage and I’m omitting “blog” in the URLs of post-pages as explained [here](https://getkirby.com/docs/advanced/routing#omitting-the-blog-folder-in-urls). Just in case that’s relevant.

---

<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 3, 2016, 1:37pm UTC](https://forum.getkirby.com/t/simple-like-feature/3492/12 "2016-03-03T13:37:15Z")

</div>

Ah, I think Kirby parses that URL as a param.  
The pattern `"/like/(:any)"` should work.

---

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 3, 2016, 2:09pm UTC](https://forum.getkirby.com/t/simple-like-feature/3492/13 "2016-03-03T14:09:56Z")

</div>

Ah okay, I’ve changed that accordingly, but now when I open `http://localhost:3000/like/super-t` in the browser it redirects to the error page. I also get a 404 in the AJAX request.

---

<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 3, 2016, 2:19pm UTC](https://forum.getkirby.com/t/simple-like-feature/3492/14 "2016-03-03T14:19:24Z")

</div>

Well, that probably depends on your other route that redirects blog URLs. Make sure to add the new route before that.

---

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 3, 2016, 2:34pm UTC](https://forum.getkirby.com/t/simple-like-feature/3492/15 "2016-03-03T14:34:59Z")

</div>

Hmm, okay strange. I removed all other routes, but still no luck unfortunately. I still get the 404 when I access `http://localhost:3000/blog/like/super-t` and in the AJAX call. Here’s the current plugin file:

**site/plugins/like/like.php**

```
<?php

kirby()->routes([

  [
    'pattern' => "/blog/like/(:any)",
    'method' => 'POST',
    'action' => function($uid) {
      $post = page('blog/' . $uid);
      try {
        $post->update(array(
          'likes' => 1000
        ));
        echo 'The page has been updated';
      } catch(Exception $e) {
        echo $e->getMessage();
      }
    }
  ]

]);

```

and this the AJAX call:

```
const btnLike = document.querySelector('.js-like');
if (btnLike) {
  btnLike.addEventListener('click', function() {
    console.log('like clicked');
    request
      .post('/blog/like/super-t')
      .end(function(err, res) {
        if (!err) console.log('liked');
        else console.log('error');
      });
  });
}

```

If things worked correctly, what am I supposed to see instead of the 404 error?

---

<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 3, 2016, 3:08pm UTC](https://forum.getkirby.com/t/simple-like-feature/3492/16 "2016-03-03T15:08:59Z")

</div>

Have you tried to remove the post method and also the slash before ‘blog’ in your pattern?

---

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 3, 2016, 3:59pm UTC](https://forum.getkirby.com/t/simple-like-feature/3492/17 "2016-03-03T15:59:10Z")

</div>

Nice, that was it! It works! Removed the method in the plugin and changed the AJAX call from POST to GET also. So it successfully updates the likes count if I pass a static value (e.g. 20) in the update function, but if I attempt to increment the count on each page reload, it doesn’t update at all. This is my current attempt:

**site/plugins/like/like.php**

```
<?php

kirby()->routes([

  [
    'pattern' => "blog/like/(:any)",
    'action' => function($uid) {
      $post = page('blog/' . $uid);
      $likesCount = $post->likes();
      $likesCount++;
      try {
        $post->update(array(
          'likes' => $likesCount
        ));
        echo 'The page has been updated';
        echo $likesCount;
      } catch(Exception $e) {
        echo $e->getMessage();
      }
    }
  ]

]);

```

I’m sure I’m overlooking something super easy, but I’m having the hardest time with this, haha.

---

<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 3, 2016, 4:20pm UTC](https://forum.getkirby.com/t/simple-like-feature/3492/18 "2016-03-03T16:20:41Z")

</div>

Does the following work?

```auto
$likesCount = $post->likes()->int();
$likesCount++;

```

---

<div class="post-metadata">

**Author:** ![rppld](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rppld/32/2981_2.png) [@rppld](https://forum.getkirby.com/u/rppld)\
**Post date:** [March 3, 2016, 4:26pm UTC](https://forum.getkirby.com/t/simple-like-feature/3492/19 "2016-03-03T16:26:33Z")

</div>

No, unfortunately not. I’ve also tried something like

```
$post->update(array(
  'likes' => function($likesCount) {
    return $likesCount++;
  }
));

```

but that breaks the whole thing.

---

<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 3, 2016, 5:03pm UTC](https://forum.getkirby.com/t/simple-like-feature/3492/20 "2016-03-03T17:03:42Z")

</div>

No, functions are not currently supported (but that’s a great idea, I have [created a feature request issue](https://github.com/getkirby/kirby/issues/397)).

We unfortunately can’t really help with line-by-line debugging. You need to test it yourself by adding `dump` statements here and there to check what the value is and where it gets wrong.

[Next page](https://forum.getkirby.com/t/simple-like-feature/3492.md?page=2)
