Sunday, October 26, 2008

Useful tips to turn your Wordpress into a CMS - 3

WP Pluggin - List Category Posts


List Category Posts allows you to list posts from a category into a post/page using the [catlist] shortcode. This shortcode accepts a category name or id, the order in which you want the posts to display, and the number of posts to display. You can use [catlist] as many times as needed with different arguments


My requirement was this.My site has a knowledge base for which a lot of articles are created as posts. All these posts come under the category "articles". I downloaded the plugin, copied it into the wp-content/plugins directory and activated it form admin panel. Then I created a page with title "Articles" (no need to be the same as the category, it was my choice) where I wanted to list all the posts under this category. So I just added this line to the page content:



[catlist name="articles" numberposts=-1 orderby=title]
name - category name
numberposts - number of posts to be displayed per page
orderby - sorting order

This worked fine for me as all my posts were within the "article" category, but as soon as I created some posts in another category called "faq" and created a sperate page with shortcode [catlist name="faq" numberposts=-1 orderby=title] it listed all my post that come under both articles and faq. So I checked back the articles page and now it is also listing all the posts form all categories. So I decided to check the plugin code for the file wp-content/plugins/list-category-posts/list_cat_posts.php. I found out an if condition with a wrong test in list_category_posts() function:

if($atts['name']!='default' && $atts['id']!='0') which will never be satisfied as we are giving either category name or category id but not both. So I just changed the "&&" joiner to "||" and everything started working fine. I am going to notify the author of the plugin. But apart from this issue, it is a very nice one. Thanks to him.

You can see this in action here or here

Monday, October 20, 2008

Useful tips to turn your Wordpress into a CMS - 2

WP Plugin - Dagon Design Sitemap Generator



If you want to create a sitemap for your site which will automatically update whenever you add a new post or page, this plugin is a good candidate.

This plugin creates a sitemap for your WordPress powered site. This is not just another XML sitemap plugin, but rather a true sitemap generator which is highly customizable from its own options page in the WordPress admin panel. Some of its features include: support for multi-level categories and pages, category/page exclusion, multiple-page generation with navigation, permalink support, choose what to display, what order to list items in, show comment counts and/or post dates, and much more.

So., I created a top level page, in the content editor chose the HTML editor and added a single line.
<!-- ddsitemapgen -->
This will give you a basic site map. To further tune this go to:
Settings->DDSitemapGen

My settings are:

Items per page: 50

Sitemap page slug: sitemap (if you are using permalinks)

What to show: Pages and posts (You can chose to show either your pages or posts or both)

If showing both, which first: Pages first (Pages is defining my site layout)

Page sort order: By menu order (ascending) - ( This will give the structure of pages and sub pages.)

Excluded categories: 1 (Excluding "Uncategorized" category)

Other setting of interest are: Sitemap navigation method,Show sitemap navigation etc...

See this in action here

Thursday, October 16, 2008

Useful tips to turn your Wordpress into a CMS -1

Worpress is a wonderful blogging tool which i have using for last two years. When I planned to create a new website recently, my first consideration was a good CMS and I chose joomla as the one. Joomla is one of the best open source CMS's in the world and I did design a moderate(not bad at least) web site. But still I didn't feel the comfort that I had when I was using wordpress - nothing technical, just my inertia to changes. At last I decided to go back to wordpress and tweak it for my needs. With some basic tweaking and many plugins, I made a website easily. I will post them here for anybody want to make use of this.

Disabling comments and pings

One of the primary requirement when converting a blog software to cms is to disable comments and pings (this depends on how you want your site to be) . To do this we will alter the WP database a little.

If you have already created lot of pages, you will have to first disable them. This is what I did for my site.

mysql> update wp_posts set ping_status='closed',comment_status='closed' where post_type in ('post','page');
Query OK, 36 rows affected (0.00 sec)
Rows matched: 55 Changed: 36 Warnings: 0

Now we should set the default value for these settings to closed.

mysql> alter table wp_posts alter column ping_status set default 'closed';
Query OK, 0 rows affected (0.21 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table wp_posts alter column comment_status set default 'closed';
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

This alone will not help to disable comments/pings because the editor to create page/post by default will have the options to allow comments/pings as enabled. To change it

Go to: Settings -> Discussion -> Default article settings and clear all settings

You are all set. Comments will be disable in your web site now.

Friday, October 3, 2008

Flushing linux memory cache

I was looking for a fix to free the unused RAM in Linux. After searching for sometime and doing some research, I found this solution.

root@enjoyfast-lx:~# free -m
total used free shared buffers cached
Mem: 1002 984 18 0 89 386
-/+ buffers/cache: 508 494
Swap: 2047 0 2047
root@enjoyfast-lx:~# sync
root@enjoyfast-lx:~# echo 3 > /proc/sys/vm/drop_caches
root@enjoyfast-lx:~# free -m
total used free shared buffers cached
Mem: 1002 600 402 0 0 112
-/+ buffers/cache: 487 515
Swap: 2047 0 2047

root@enjoyfast-lx:~# echo 0 > /proc/sys/vm/drop_caches

The sync command will write the contents of cache into hard disk. But the cached pages will still remain in memory for quick access. This will actually speed up the execution of programs that are currently accessing this pages. But if you are running out of memory and need to free up some RAM, you can remove this pages from cache permanently bye changing the value of /proc/sys/vm/drop_caches to 3 . Now a quick analysis of what happens when we do this can be done by using the /proc/meminfo file.

Before running the above fix:

root@enjoyfast-lx:~/temp# cat /proc/meminfo > mem.b4

After running the fix

root@enjoyfast-lx:~/temp# cat /proc/meminfo > mem.af8r

root@enjoyfast-lx:~/temp# diff -y mem.b4 mem.af8r|tr -d '|'|awk '{ print $2"\t"$5"\t"expr $5 - $2"\t"$1 }'|awk '{if($3 != 0)print $0}'|sort -k4
474136 395276 -78860 Active:
346992 347096 104 AnonPages:
96172 452 -95720 Buffers:
396076 115380 -280696 Cached:
953836 954052 216 Committed_AS:
316 80 -236 Dirty:
1608 69064 67456 HighFree:
365060 67608 -297452 Inactive:
13992 342888 328896 LowFree:
15600 411952 396352 MemFree:
2928 2932 4 PageTables:
35480 15528 -19952 Slab:
25088 6308 -18780 SReclaimable:
10392 9220 -1172 SUnreclaim:

As you can see, its freeing up buffers and cache. For a complete reference of this values, go to:

http://www.redhat.com/advice/tips/meminfo.html

Monday, September 29, 2008

Unix soft link and hard link

To simply put it, a Unix link is a convenient way to access data of a file. It is similar to shortcuts in windows. But in UNIX there are two types of links, soft link and hard link. To understand the difference between these two, we should first know some basics of Unix file structure.

In UNIX file system, a file contains two parts, data part and filename part. Filename part contains the file name and indoe. Data part contains the actual data stored in the file. Inode is a data structure that contains the information about the file like permissions, group, owner, pointer to the data part etc….Each inode (and hence the data it points to) is identified by an inode number. Data part contains the actual data stored in the file. A user accesses a file using its name while the underlying file system access it with its inode number.

Different files can point to the same inode number. These files are called hard links. They will have different names and may be located in different paths, but under the same file system. Hard links can’t be created across different file systems, this is because the inode number is specific to a file system not the OS as a whole. Each file system maintains a table of inodes within that file system.

A soft link is a special file that contains the file system path to another file as its data. But as soft link (symlink) is a special file, any read/write/modify operation on this file will be redirected to the target file. Since symlinks refer to their target using absolute path and file name, it can cross file system boundaries and can be located anywhere. Soft links can point to both files and directories.

Difference b/w soft and hard links


Soft link

Hard Link

Points to file name of original file

Points to inode of original file

Can link between file systems

Can link only within same file system

Both for file & directory

Only for files

Default permission 777

Default permission 644

Can’t access data after original file is deleted

Can access data even after original file is deleted

Can’t change owner/group/permission

Can change owner/group/permission

Inode number not same as original file

Inode number same as original file


Now create a text file

[root@penguin007 links]# touch link-test.orig
[root@penguin007 links]# echo “Original file created” > link-test.orig
[root@penguin007 links]# cat link-test.orig
Original file created

The command to create link is "ln". Let us create a hard link to the file link-test.orig.

[root@penguin007 links]# ln link-test.orig link-test.hard

Now let us create a soft link to the file link-test.orig.
[root@penguin007 links]# ln -s link-test.orig link-test.soft
[root@penguin007 links]# ls -li
total 8
163774475 -rw-r–r– 2 root root 22 Jul 19 13:38 link-test.hard
163774475 -rw-r–r– 2 root root 22 Jul 19 13:38 link-test.orig
163774476 lrwxrwxrwx 1 root root 14 Jul 19 13:47 link-test.soft -> link-test.orig

Now you can make changes to the original file/soft link/hardlink and the change will be reflected on all others. This is because all of these are pointing to the same data location

Edit from soft link

[root@penguin007 links]# echo “Change from soft link” > link-test.soft
[root@penguin007 links]# cat link-test.orig
Change from soft link
[root@penguin007 links]# cat link-test.hard

Change from soft link

Edit from hard link

[root@penguin007 links]# echo “Change from hard link” > link-test.hard
[root@penguin007 links]# cat link-test.orig
Change from hard link
[root@penguin007 links]# cat link-test.soft

Change from hard link

Edit from original file

[root@penguin007 links]# echo “Change from original” > link-test.orig
[root@penguin007 links]# cat link-test.soft
Change from original
[root@penguin007 links]# cat link-test.hard

Change from original

From the output of "ls -li" you can observe following points.

Both the original file and hard link are having same inode (163774475 - column 1 ). In fact both these files are referred to as hard links and you can't distinguish which file is original and which one is link - there is no need to do that either.

Note the permission on the files, hard links are having standard 644 permissions while soft link has 777 permission. Hard links are treated as regular files but soft links are treated as links- note the first character in the 2nd column of "ls" output above ("-" for regular file/hard link and "l" for soft link)

When you change permission/ownership of a hard link the change is reflected on all hard links. But when you change permission/ownership of a softlink the change is applied to the original file (and its hard links if any) while soft link retains the permission and ownership.

[root@penguin007 links]# chmod 755 link-test.hard

[root@penguin007 links]# ls -li
total 8
163774475 -rwxr-xr-x 2 root root 22 Jul 19 13:38 link-test.hard
163774475 -rwxr-xr-x 2 root root 22 Jul 19 13:38 link-test.orig
163774476 lrwxrwxrwx 1 root root 14 Jul 19 13:47 link-test.soft -> link-test.orig
[root@penguin007 links]# chmod 644 link-test.soft
[root@penguin007 links]# ls -li
total 8
163774475 -rw-r–r– 2 root root 22 Jul 19 13:38 link-test.hard
163774475 -rw-r–r– 2 root root 22 Jul 19 13:38 link-test.orig
163774476 lrwxrwxrwx 1 root root 14 Jul 19 13:47 link-test.soft -> link-test.orig
[root@penguin007 links]# chown safeer link-test.orig
[root@penguin007 links]# ls -li
total 8
163774475 -rw-r–r– 2 safeer root 22 Jul 19 13:38 link-test.hard
163774475 -rw-r–r– 2 safeer root 22 Jul 19 13:38 link-test.orig
163774476 lrwxrwxrwx 1 root root 14 Jul 19 13:47 link-test.soft -> link-test.orig
[root@penguin007 links]# chown root link-test.soft
[root@penguin007 links]# ls -li
total 8
163774475 -rw-r–r– 2 root root 22 Jul 19 13:38 link-test.hard
163774475 -rw-r–r– 2 root root 22 Jul 19 13:38 link-test.orig
163774476 lrwxrwxrwx 1 root root 14 Jul 19 13:47 link-test.soft -> link-test.orig

The third column in the output shows the hard links count, ie; how many hard links point to that file (2 in our case).

Links do not cause any additional hard disk usage. We can check it now:

[root@penguin007 links]# du -hs ./*
4.0K ./link-test.hard

0 ./link-test.soft

You may note that only one file (that comes alphabetically first) of the hard links is displayed in disk usage result (this happens if both hard links are in the same directory)

Now let us delete the original file.
[root@penguin007 links]# rm -rf link-test.orig
[root@penguin007 links]# cat link-test.hard
Change from hard link
[root@penguin007 links]# cat link-test.soft

cat: link-test.soft: No such file or directory

As you can see, soft link will not work if the original file is deleted, because it points to the name of the original file. But hard links are pointing directly to the inode, so it can continue accessing the data. When a file has multiple hard links, the data part of the file will not be deleted until all the hard links to the file are deleted. The count of hard links is stored in the inode which will be decremented every time a hard link to that data is deleted.