Skip to content

Instantly share code, notes, and snippets.

What would you like to do?

Simple Kirby Like Counter


  1. Add like.php to your site/templates folder
  2. Create a new invisible like folder in content/blog
  3. Place a like.txt in content/blog It's enough to put title: Like Updater or something like that as content in there.


Go to This will invoke the Like Counter Updater. Since no parameter has been passed, you will get back an error.

{"status":"error","msg":"The post could not be found"}

Pass an UID of a blog article like this:

If the article has been found, the like counter will increase and add a Likes field to your article's text file. On each reuquest, the like counter will increase again.

The response will look like:

{"status":"success","msg":"The likes have been updated","likes":5}


Here's a simple example how to call the like counter via ajax (jquery):

$.getJSON('/blog/like/post:my-awesome-article', function(r) {

  if(r == undefined || r.status == 'error') {
    return alert('Likes could not be updated');
  } else {
    return alert('Likes have been updated. New Likes count: ' + r.likes);


// get the post UID from the URL
$uid = param('post');
if(!$uid) die(a::json(array(
'status' => 'error',
'msg' => 'The post could not be found',
// get the full post object
$post = $pages->find('blog/' . $uid);
if($post->uid() != $uid) die(a::json(array(
'status' => 'error',
'msg' => 'The post could not be found',
// get current likes count
$likes = intval((string)$post->likes());
// get all variables from the content text file
$variables = $post->content()->variables();
// add the updated likes counter
$variables['likes'] = $likes;
// set the filename
$file = $post->root() . '/' . $post->intendedTemplate() . '.txt';
// overwrite the file
$write = write($file, $variables);
// react on errors
if(error($write)) die(a::json($write));
'status' => 'success',
'msg' => 'The likes have been updated',
'likes' => $likes
// writer function
function write($file, $values) {
if(file_exists($file) && !is_writable($file)) return array(
'status' => 'error',
'msg' => 'The file could not be written'
$break = false;
$result = "\xEF\xBB\xBF";
$keys = array();
foreach($values AS $k => $v) {
$k = str::urlify($k);
$k = str::ucfirst(str_replace('-', '_', $k));
if(in_array($k, $keys) || empty($k)) continue;
$keys[] = $k;
$result .= $break . $k . ': ' . trim($v);
$break = "\n\n----\n\n";
$write = f::write($file, $result);
if(!$write || !file_exists($file)) return array(
'status' => 'error',
'msg' => 'The file could not be written'
return array(
'status' => 'success',
'msg' => 'The file has been written'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment