Category Archives: Codes

WordPress: Quick Steps to Setup a Child Theme

WordPress has this neat feature called “Child theme” for a while now. I’m a big fan of this feature. It is important to use this when you are using a theme that you like but want some changes to it. If you work on the theme files directly, chances are you will overwrite the changes when new updates are pushed. I’ve done it before and since the child theme came out I’ve not had any issue with it anymore.

So, here I would show how to setup a child theme quick and easy. Say you are using “twentytwelve” theme as your primary theme. Now you (like me) want to make a change to the header file to add your own code – for example, some Google code to show translation bar. Here are the steps:

1. under your-wp-root/wp-content/themes/ create the directory you want to place the child theme files in

2. now go into your-wp-root/wp-content/themes/your-child-theme/ and create a file named style.css

3. in style.css you will need to put the following piece of comment:

/*
* Theme Name: YOUR CHILD THEME NAME
* Description: Modified 2012 Theme
* Author: Your Name
* Template: twentytwelve
* */

Line 5 is very important here. It tells WP which parent theme directory to use.

4. After the comment you will need to import the parent theme’s stylesheet in. You can either copy paste the entire style.css from twentytwelve directory, or put the following import code:

@import url(../twentytwelve/style.css);

5. Now you can copy header.php from your-wp-root/wp-content/themes/twentytwelve to your-wp-root/wp-content/themes/your-child-theme/. Make necessary changes to the file and you are good to go.

6. Last step (which you could have done before changing header.php file) is to activate the child theme. In WP admin, under Appearance > Themes look for the theme you’ve just created. Click on “Activate” to use it.

Only the modified files (like header.php) will be used from your child theme’s directory. You can always do these on your local machine, upload the directory via FTP and activate the theme; the steps above are just the way I do it – i.e. login via SSH and do these from command line.

For further reading you can click here.

 

My two WordPress plugins

I’ve been using WordPress since 2005 when I decided to move away from my custom made blog. I’ve – since then – modified and created bunch of themes and plugins. Customized many plugins/themes to work the way I want it to work.

I’ve also published two WordPress plugins. I wrote both of them for my own purpose, but later decided to publish them on WordPress.org (visit my profile page on WP to download the plugins).

1. Recently Updated Pages

This plugin will let users add a sidebar widget where you can list WP pages you’ve recently modified. The sole purpose of this plugin was to list ONLY the pages, but after some requests from the users I’ve also added feature to list posts. I’ve couple of more features in works right now, one is to exclude pages/posts from listing under this widget. So far I’ve added an option in the post/page editor where user can opt-in/out of listing it under RUP widget. These features will be published soon.

2. Feedback Extended

I’ve recently published this plugin. Downloads for this plugin isn’t as aggressive as the RUP plugin but today it reached 100 downloads (woohoo!!).

This plugin only works with the Contact Form enabled under Jetpack 1.3 or higher. FE will add an extra option to reply from within the administrative area of WP to any contact received via the Jetpack Contact Form. These contact messages are stored under Feedback section in WP and FE will allow you to reply to the emails sent to you via WP site right from the admin area as long as WP installation can send emails using the server’s send mail service. There is no further feature I’ve in mind for this plugin.

Do you’ve any idea for a new plugin? Do you want someone to help you build a plugin? Please drop a line here and let’s have a chat about it. 

jQuery: Select Multiple Dropdown Items

Back in 2006 I posted a simple piece of Javascript code that would let you select multiple items from a dropdown (or multiselect box) to a textarea. This code has been very popular and people liked it as I can see from different forums being linked to this post.

Here is the same code, but it uses jQuery to do the job. Smaller piece of code than what I’d before. HTML piece remains same and here is the example:

 
 

HTML Code:

<form method=POST name='testing'>  
  <select name='testsel' multiple id='testsel'>  
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>  
  </select>  
  <textarea id="txtEditions"></textarea>
</form>

jQuery Code:

<script type="text/javascript">
var glue = '+';
try {
	$('#testsel').change(function() {
		$('#txtEditions').val(($('#testsel').val()).join(glue));
	});
} catch(e) {
	console.log(e);
}
</script>

Rotate Logs for Apache on Linux using logrotate

Using Apache’s access_log and error_log files are useful. Not only a system administrator should use it, but also a developer should grow habit of using these log files for debugging purpose. But sometimes for high traffic web site/applications these files can grow HUGE and real fast.

In real life, you probably need log file up to a certain point. If anything goes wrong and you know about it immediately you will probably need the current log file plus few from past to compare. In case if you find out about an issue with the site/application a week later – then you should really think about how to fix that problem.

Anyhow, this article is to show you how you can tune pre-configured logrotate on linux system for apache (HTTPD) to meet your needs. Every path I’m going to refer here are based on CentOS 5.6 OS.

Usually the logrotate configuration file is located under /etc/logrotate.conf. But logroate also has individual configuration for different linux processed. One of them is Apache. By default the configuration looks like this:

/var/log/httpd/*log {
missingok
 notifempty
 sharedscripts
 postrotate
 /sbin/service httpd reload > /dev/null 2>/dev/null || true
 endscript
}

To understand what each of the items mean please refer to http://tinyurl.com/ansqol

For my purpose I just added two lines to have this:

/var/log/httpd/*log {
size=1024M
rotate 5
missingok
notifempty
sharedscripts
postrotate
/sbin/service httpd reload > /dev/null 2>/dev/null || true
endscript
}

size=1024M – The logs should be rotated when it is 1G in size.

rotate 5 – The logs are rotated 5 times before it is removed.

Thanks!

10 useful Linux command line tricks

When working on Linux on a day to day basis, there are times when you need to perform some tasks from command line. You can run combination of commands to get the desired result. Following are 10 of many useful Linux Command Line tricks I use almost everyday.

1. print column 2 of all tsv files in a directory and pipe it to a file:

for i in `ls`; do tail $i | tr "\t" "~" | cut -d"~" -f2 >> titles; done

2. Find unique string from a file:

sort -u filename

3. Calculate number of lines in a file:

wc -l

4. Send email from command line with attachment:

mutt -s 'my subject' -a /var/log/httpd/error_log -- youremail@domain.com < /dev/null

6. Read a file, print UNIQUE value in a specific column (column 5 in this case)

cat re | tr "\t" "~" | cut -d"~" -f5 | sort -u

7. Generate alphanumeric password

echo `< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c10`

8. Allow MySQL access in IPTable for a specific IP

-A INPUT -p tcp -s FROM_IP (IP1) --sport 1024:65535 -d TO_IP (IP2) --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT

-A OUTPUT -p tcp -s FROM_IP (IP2) --sport 3306 -d TO_IP (IP1) --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

9. While files are being copied/moved to a directory, run the following command to count number of files in that directory (in loop) and print it – acts like a timer.

printed=0; while [ `ls /some/directory/ | wc -l` -gt 0 ]; do output=`ls /some/directory/ | wc -l`; if [ $output -gt $printed ]; then clear; echo $output; elif [ $output == $printed ]; then echo $printed; printed=$output; fi; done

10. Print date in specific timezone

TZ=EST date