Category: wordpress

quick wordpress tip: how to get all tags with the number of posts

This is very useful in certain situations, where you want to draw your own tag cloud or show the number of posts beside each tags in your wordpress blog, i.e (php [10], javascript [3]). I was searching for the solution while ago and found wp_tag_clouds but that doesn’t return the number of posts for each of these tags. Shortly after that, found get_terms function which was a great relief actually. Because you can fetch all types of taxonomies with useful data. And the following code snippet solved my problem. Don’t forget to check the reference in codex.

[sourcecode lang=”php”]
$tags = get_terms( array("post_tag"), array("orderby"=>"count","order"=>"DESC"));
[/sourcecode]

This function will output something like the following
[sourcecode lang=”php”]
Array
(
[0] => stdClass Object
(
[term_id] => 11
[name] => php
[slug] => php
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 2
)
[1] => stdClass Object
(
[term_id] => 10
[name] => bingo
[slug] => bingo
[term_group] => 0
[term_taxonomy_id] => 10
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
)
[2] => stdClass Object
(
[term_id] => 12
[name] => javascript
[slug] => javascript
[term_group] => 0
[term_taxonomy_id] => 12
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
)
)
[/sourcecode]

Shameless Plug

We develop beautiful Admin Panel templates for the web application developers. Our recent product is “Bolt” which is responsive and built on top of Twitter’s bootstrap. Give it a try and let us know how do you like it.

Bolt Responsive Admin Panel Template for Developers
Bolt Responsive Admin Panel Template for Developers

how to add custom meta boxes in wordpress

Custom meta boxes are very useful for collecting arbitrary information from the post authors, and then take decisions based on those information. For example, if you have a custom post type as “stores” and you want to collect “latitude” and “longitude” for each “store” type post, meta boxes are the way to do it conveniently. So how you can create a meta box and collect meta information from the users? The whole workflow is divided into three parts

1. Create the meta box design/markup
2. Attach them with the post/custom-post
3. Save the user input (and reuse it when required)

Creating the metabox markup: Inside your theme folder, lets create a new folder named “metaboxes” to organize all the metaboxes in same place. So once the folder is created, create a new file name coordinates.php inside the metabox folder. So the final path to the coordinates.php is your.theme.folder/metaboxes/coordinates.php. This file contains the simple markup to accept latitude and longitude from the users for our custom post type “stores”. Here comes the markup for that file

[sourcecode lang=”html”]
<fieldset id="coordinates">
<table width="100%">
<tr>
<td>
Latitude:
</td>
<td>
<input style="width:140px;" type="text" name="store_latitude" id="store_latitude" />
</td>
<tr>
<td>
Longitude:
</td>
<td>
<input style="width:140px;" type="text" name="store_longitude" id="store_longitude" />
</td>
</tr>
</table>
</fieldset>
[/sourcecode]

It’s just a simple form – no big deal, eh? At this moment this form can only take user input. But to retain the previously entered value we need to work on it and add some extra lines. I will come back later to this file.

Registering the custom post: There is a nifty tool out there which can help you to create the essential code to register a custom post type, unless you want to write all of them by yourself (I prefer writing my own, always). Create a new file named “custom-post-stores.php” inside your theme folder and include this file in the functions.php of your theme. Here is the code of the custom-post-stores.php

[sourcecode lang=”php”]
<?php
add_action(‘init’, ‘createPostType’);
function createPostType(){
register_post_type(‘stores’,
array(
‘labels’ => array(
‘name’ => __(‘Stores’),
‘singular_name’ => __(‘Store’),
‘add_new’ => __("Add New Store"),
‘add_new_item’ => __("Add New Store"),
),
‘public’ => true,
‘has_archive’ => true,
‘rewrite’ => array(‘slug’ => ‘stores’),
‘supports’ => array("title", "editor", "thumbnail")
)
);
}
[/sourcecode]

I am not going to explain what all of these parameters do in the register_post_type function, but of course, please, have a look at the beautiful reference in codex.

The code above will register a new custom post “stores” and once you logged into the wordpress admin panel, you will notice a new “Stores” menu visible on the left side. From there you can add new stores type posts.

Attaching metaboxes to the custom post: Lets attach the previously created metabox with this new “stores” type posts. In the same “custom-post-stores.php” append the following codeblock

[sourcecode lang=”php”]
/*this function will include the metabox in current posts scope*/
function customPostGeoinfo()
{
global $post;
include_once("metaboxes/coordinates.php");
}

/*this function will save the user input from the meta box*/
function customPostSave($postID)
{
// called after a post or page is saved
if ($parent_id = wp_is_post_revision($postID)) {
$postID = $parent_id;
}

$items = array("store_latitude","store_longitude"); //form elements of the meta box

foreach ($items as $item) {
if ($_POST[$item]) {
updateCustomMeta($postID, $_POST[$item], $item);
}
}

}

function updateCustomMeta($postID, $newvalue, $field_name)
{
// To create new meta
if (!get_post_meta($postID, $field_name)) {
add_post_meta($postID, $field_name, $newvalue);
} else {
// or to update existing meta
update_post_meta($postID, $field_name, $newvalue);
}
}

/* now make them working via wordpress action hooks */

function myPostOptionsBox()
{
add_meta_box(‘geoinformation’, ‘Geo Info’, ‘customPostGeoinfo’, ‘stores’, ‘side’, ‘high’);
}

add_action(‘admin_menu’, ‘myPostOptionsBox’);
add_action(‘save_post’, ‘customPostSave’);
[/sourcecode]

Tada, we are done. But only one thing left and that is revising our metabox to retain the old user value. So lets rewrite the metaboxes/coordinates.php with the following code

[sourcecode lang=”html”]
<fieldset id="coordinates">
<table width="100%">
<tr>
<td>
Latitude:
</td>
<td>
<input style="width:140px;" type="text" name="store_latitude" id="store_latitude" value="<?php echo get_post_meta($post->ID, ‘store_latitude’, true); ?>"/>
</td>
<tr>
<td>
Longitude:
</td>
<td>
<input style="width:140px;" type="text" name="store_longitude" id="store_longitude" value="<?php echo get_post_meta($post->ID, ‘store_longitude’, true); ?>"/>
</td>
</tr>
</table>
</fieldset>
[/sourcecode]

That’s it. The out put will look like the following one. Please notice the “Geo Info” metabox on the top right corner and “Stores” menu on the left menu.

Custom post with meta box
Custom post with meta box

Shameless Plug: We develop beautiful themes and admin panel templates for your web applications. Why don’t have a look at our portfolio in themeforest and purchase a few to support us on the long run.

Force wordpress sub categories to use the same template of the parent category

WordPress is a beautiful cms, from top to bottom, for both the developers or authors. It’s action and filter hooks made the developers life a lot easier who develop custom solutions on top of wordpress. Today I went throuh a problem where I had to define category template for a parent category type, and then had to make sure that all the child categories under that parent must inherit the same template used by the parent category.

Defining template for a particular category is easy. All you have to do is create a file named as “category-.php”. So if your caregory slug is “stores” then the template name will be “category-stores.php” and as soon some viewers come to http://your.wp.app.url/category/stores that particular template will be used to render the request.

Now the problematic part is I need a few subcategories under the “stores” category which must inherit that same template “category-stores.php”, because the look-n-feel and content are mostly similar. And also there is no default template naming convention for the sub categories. So I had to write an action hook which hooks the “template_redirect” action and then renders the same template used by parent category. Here is the code I used inside functions.php

[sourcecode lang=”php”]
add_action(‘template_redirect’, ‘inheritParentTemplate’);

function inheritParentTemplate() {
if (is_category()) {
$catid = get_query_var(‘cat’); //current category id
$category = get_category($catid);
$parent = $category->category_parent; //immediate parent
if ($parent){
$parentCategory = get_category($parent);
if("stores"==$parentCategory->name){
if ( file_exists(TEMPLATEPATH . ‘/category-‘ . $parentCategory->slug . ‘.php’) ) {
include (TEMPLATEPATH . ‘/category-‘ . $parentCategory->slug . ‘.php’);
}
return true;
}
}
}
}
[/sourcecode]

Thats it. From now on, every child category will be rendered using the same template “category-stores.php“.

Shameless Plug

We develop beautiful Admin Panel templates for the web application developers. Our recent product is “Bolt” which is responsive and built on top of Twitter’s bootstrap. Give it a try and let us know how do you like it.

Bolt Responsive Admin Panel Template for Developers
Bolt Responsive Admin Panel Template for Developers

A week full of fun in Leevio, with one new product and two big updates.

This week it was full of fun in Leevio. Our new wing ThemeStudio is now fully operational and working on exciting themes every month, and has already released a cool theme “StoneHenge“. ThemeStudio is working hard to update admin panel and bring new features to make our wordpress themes more usable everytime.

And there were two new updates on MiproApps this week. We have added support for Box.net and that means you can embed files from your box.net account directly in your Facebook fanpages. And we’ve also added a new widget for creating polls and collecting feedback from your Facebook fanpage visitors.

Releasing new updates and products are always fun! But that’s not the all for this week. We went to Bandarban (A nice place with lots of mountains and forest) and spent three days over there. It was just beautiful and refreshing. I will update some pics of this event on my Facebook profile, very soon.

StoneHenge – our new wordpress theme

We’ve released StoneHenge, a beautiful two colored and custom home page powered wordpress theme today. This theme comes with a image slider, featured posts, featured links and advertisement section and you can configure all of them using the easy to understand admin panel of StoneHenge

This is a list of available features in StoneHenge
* jQuery based Image Slider, fully configurable
* Widget Enabled
* Custom Homepage with featured posts, advertisement & featured links
* Links to your lifestream sites on homepage
* Custom footer, sidebar and categories
* Easy to use and not-feature-bloated admin panel
* Beautiful page navigation
* Sidebar with tabified latest, commented and popular posts.
* Simple yet elegant, Completeley Free!

Here is a screen shot

StoneHenge wordpress theme
StoneHenge wordpress theme

You can download this theme including manual, without manual or you can download just the manual

For details and updates please check out http://themestudio.leevio.com

converting standard wordpress into a SQLite powered multi user blogging platform

wordpress is one of the very popular blogging platforms which is not only free but also released as a open source project. officially wordpress runs only on mysql. so if you want to run it with SQLite, you will not find any official support for that. but fortunately justin addie has written an excellent plugin (pdo for wordpress) which will work as an adapter just between the wordpress DAL and mysql. this plugin hooks all the SQL queries which are sent to execute on mysql by wordpress, convert those into PDO compatible statements and then execute them. currently PDO for WordPress is written to work smoothly with mysql and sqlite.

on the other hand, WPMU (wordpress mu or multi-user) is another version of wordpress which uses the core wp with some modifications and convert any single user wordpress blog into a multi user blogging platform. in this blog post i will show you how you can convert a general installation of wordpress into multi user blogging platform (like WPMU, but fully featured) and take advantage of SQLite 🙂 – so let the fun begin.

step 1: check if your domain support wildcard A record. if so, then create a “*” A record and point it to your server. if it works, now you can point http://.. to your server 🙂 – that is very much required. alo please check that in your webserver it has “pdo” and “pdo_sqlite” php extensions enabled. you can check that using phpinfo(); – needless to say, you need PHP5

step 2: install wordpress

step 3: install pdo for wordpress
download the pdo_for_wordpress plugin. inside it you will find one file named “db.php” and a folder named “pdo”.
put the db.php inside “wp-content” directory
now put the “pdo” folder inside “wp-content” directory

open wp-config.php
find the following line
[sourcecode lang=”php”]
define(‘DB_COLLATE’, ”);
[/sourcecode]

add the following line just below that line
[sourcecode lang=”php”]
define(‘DB_TYPE’, ‘sqlite’);
[/sourcecode]

step 4: create a directory named “userdb” anywhere in your server, make sure to give it write access. in this example we’ve created one as /opt/userdb – DONT FORGET TO ASSIGN WRITE PERMISSION to this “userdb” directory.

step 5: now we will be modifying that “db.php” which we had copied into “wp-content” folder.

find the line in db.php where it says
[sourcecode lang=”php”]
define (‘FQDBDIR’, ABSPATH .’/wp-content/database/’);
define (‘FQDB’, FQDBDIR .’MyBlog.sqlite’);
[/sourcecode]

now change those lines as below
[sourcecode lang=”php”]
define (‘FQDBDIR’, ‘/opt/userdb/’);
define (‘FQDB’, FQDBDIR ."{$_SERVER[‘SERVER_NAME’]}.sqlite");
[/sourcecode]

and tada – you are done. now point your browser to any subdomain under your domain and it will comeup with registration page. register some blog and check it out 🙂 (at the bottom of this post you can find the link of a live demo)

now you can extract some themes in the wp-content/themes folder or some plug-ins into wp-content/plugins folder. all users will be able to choose only from those pre installed themes and plugins, and everyones data and configuration will remain separate from others, as everyone is running from their own database. set only “read” access to “wp-content/themes” and “wp-content/plugins” folders to avoid any security loophole.

happy blogging.

[note – i’ve checked it against latest wordpress 2.8.4 and it runs okay]

To check out a live example of a running wordpress 2.8.4 on SQLite – click on http://anything.twistats.com/wp/ – and to create your own – just go to this url http://<any_subdomain>.twistats.com/wp and register your one :). You can select from six pre installed themes too 🙂

WordPress Bangla Date Plugin

Yesterday I developed a plug in for wordpress users. Using this plug in you can display all the dates for your posts and comments in Bengali. So download the plug in from the following link, place it in plugins folder and enable it from “plugins” menu in wordpress administration panel. You are done.

I have released under Ekushey. Thanks to Omi Azad for the work.
http://www.ekushey.org/?page/Bangla_Date_Plugin_for_Wordpress

The project was inspired by Humayun bhai from e-Kobi

My book "WordPress Complete" has been slashdotted 8

Today I get the news from my Editor at Packt Publishing, David Barnes that my book “WordPress Complete” has been reviewed today in slashdot and scored 8 out of 10 — I am very happy today.

You can visit it from here http://books.slashdot.org/article.pl?sid=07/04/25/1554235

And you can buy the book from here WordPress Complete at Amazon

Thanks to all my friends, colleagues, editors and friends in packt, pageflakes and somewherein – I am very happy today.

WordPress Blogrolls Importer – Opensource

Today I developed this tool to import wordpress blog rolls as XML document. You know when you export data from wordpress.com that doesn’t include the blog rolls data. So if you want to keep a backup of your blog rolls, you can use this tool to import your blog rolls data.. This one is developed using PHP and Curl

importer.gif

You can doewnload it and see the code in action here

Opensource WordPress Blogrolls Importer

################

The trick is lying here —
;

    

    $ch curl_init();

    
curl_setopt($chCURLOPT_COOKIEJAR“./login.jar”);

    

    

    
curl_setopt($chCURLOPT_RETURNTRANSFER,1);

    //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  // this line makes it work under https

    
curl_setopt($chCURLOPT_URL,“{$url}/wp-login.php”);

    curl_setopt($chCURLOPT_POST1);

    
curl_setopt($chCURLOPT_FOLLOWLOCATION,1);

    
curl_setopt($chCURLOPT_POSTFIELDS“log={$username}&pwd={$password}&&redirect_to=wp-admin/link-manager.php&wp_submit=”);

    

    $buffercurl_exec ($ch); // execute the curl command