Array Dereference in PHP 5.4

Found this out when deploying my new app into server. I used PHP 5.4 in my local machine, and unfortunately its 5.3 in my server. The app was blank and the log inform me:

PHP Parse error:  syntax error, unexpected '[', expecting ')' in /some/file.php on line 114

We need a temporary variable to fix it:

// a function returning array()
getAll() {
    return array( 1, 2, 3 );
}

// in PHP 5.4, we can simply:
$first_number = getAll()[0];

// my fix:
$numbers = getAll();
$first_number = $numbers[0];

Fix Missing Mysqld.sock on Linux

Hi! It’s been a long time since my last post. Why I wrote this? It’s because I got this error last night and it annoyed me so much. Well, I’ve ever got the same error, several months ago.

Alright, the error is kind like this:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

We could find that error from PHP error notice or from Terminal when we try to open MySQL connection.

After some hours of Googling, and have no result, I tried my own way. I took it from several sources. And here’s how:

  1. You have to use your root access
  2. Change the directory to /var/run/mysqld
    cd /var/run/mysqld
  3. Create an empty mysqld.sock file
    touch mysqld.sock
  4. Give executable permission to sock file
    chmod +x mysqld.sock
  5. Change permission to mysql user
    chown mysql:mysql -R *
  6. Change the working directory to /tmp
    cd /tmp
  7. Create a symlink to the sock file
    ln -s /var/run/mysqld/mysqld.sock mysqld.sock
  8. And at last, run the MySQL /etc/init.d/mysql start.

Yeah, my db is up now. 🙂

New Year!

I know its been a while since new years eve. But, whooa, its been a long time not writing on my (own) blog too! The last post I wrote was on August of 2012 when I checked. Yeah, I’d been busy with my school tasks and some school organizations duty. On another side, I was having some side project for my friend and learning WordPress plugin development, and still working on it.

Oh, almost forgot, its new year right? Welcome to 2013! Hope it would be a good year for me, and good to for ya’ll. 🙂

Last Saturday was a really nice day. When i checked my email in the morning, I saw a mail from Caroline Moore (sixhours), and unbelievably, Minimalizine was live at WordPress.com. I’d have been got an email from her few weeks ago, she told me that Minimalizine would be remarked to be live at WordPress.com directory. I was so glad and happy to know that 🙂

You can read the announcement of it from WordPress.com’s blog and/or start a new blog there with Minimalizine.

By now, i’m working for updating Minimalizine on WordPress.org directory and hope it’ll available soon. One more theme will cooming after. 🙂

Let Contributor Edit Their Published Posts on WordPress

Yesterday, I asked by a friend to publish his post at Blogazinist (dead). I published it then, and realized that the post was not the final yet. He wanted to edit it, so I pending that post again. And had been doing those activity for many times.

By default, we set registered user on Blogazinist as a contributor. After reading a while at WordPress Codex about User Roles and Capabilities, I knew that contributor can edit their posts, but not after it’s published. Then I scrolling down edit_published_posts capability. Then I thought to add that capability to contributor role.

We all knew, WordPress gives easy way to modify its core functions by hooking to that function. So, to achieve that, I hooked to admin_init function. Also using get_role function to get contributor role and add a new capability on it.

function contributor_edit_published() {
    $contributor = get_role('contributor');
    $contributor->add_cap('edit_published_posts');
}
add_action('admin_init', 'contributor_edit_published');

By now, Blogazinist’s contributor could edit their published posts. :)

References: