PHP wordpress

Add categories and tags support to custom post types in WordPress

Adding a custom post type in WordPress is pretty easy and while it can be done and managed via external plugins, it’s pretty easy to do it by adding a little code in the functions.php file of your theme. All we need to do is take advantage of the “register_post_type” method that’s provided by wp. You can see more details and all the params on the Codex.

Here is a simple example that creates a custom post type named “project”:

By default, this new custom post type doesn’t has the ability to use taxonomies (categories, tags, or any other custom taxonomies you might need), but that can be easily added. Simply add ‘taxonomies’ => array(‘category’, ‘post_tag’) to the $args variable then initialize the new taxonomies. The code will look like so:

If anyone knows an easier way (without using a plugin) of achieving this, please use the comments section below!


Magento tip : Image Upload HTTP error

There are a few possible causes including problems with mod_security.. however, if you are working behind a .htaccess/.htpasswd files to protect your site, you need to allow POST calls to go through; they’re being blocked b/c the AJAX call doesn’t know to use your cookie.


Require valid-user

to your .htaccess and you should be good to go.


Sublime text 2 Unity Launcher

I’ve looked into Sublime text 2 today, mostly unhappy with the speed Java based IDEs have on linux 64bit. When launching it, although I chose “keep in launcher”, the icon does not stay there, so i created a custom launcher.

Create a file sublime2.desktop and save it in ~/.local/share/applications. The file should look like this:

#!/usr/bin/env xdg-open

[Desktop Entry]
Name=Sublime Text Editor
Comment=Sublime Text Editor
Exec="/usr/bin/sublime" %F

You can find the link to the icon here:

Make sure to change the Exec path, to where you have installed Sublime2. I’ve installed in /opt but i’ve symlinked in /usr/bin


First and last day of the month in PHP

I’ve been working with dates today and i’ve found a pretty neat snippet to show the first and the last day of the month using php

		$firstDoM = date("Y-m-01"); //01 -> first day of month
		$lastDoM = date("Y-m-t"); //t -> last day of month

Magento get all products

Here is an easy way to return all the products from a magento store.

$collection = Mage::getModel('catalog/product')

foreach ($collection as $product) {
    echo $product->getName() . "
"; }
PHP zend framework

HTTP response headers in Zend Framework

I needed to display a Zend Framework generated PDF using the browser plugins and after a bit of looking around it turns out that some headers need to be set in order to achieve that.

     ->setHeader('Content-Disposition', 'inline; filename=invoice.pdf')
     ->setHeader('Content-type', 'application/x-pdf');

If you’d want to automatically download the PDF instead of displaying it using the browser plugins, you need to change the ‘Content-Disposition’ as follows:

     ->setHeader('Content-Disposition', 'attachment; filename=invoice.pdf')
     ->setHeader('Content-type', 'application/x-pdf');
PHP zend framework

Switch layouts in Zend Framework

If you have a larger Zend Framework application, you probably want to you use a layout for more of your pages. For instance you’d need at least two layouts: one for a backend and another one for frontend. In order to tell ZF to use layouts, you’ll need to set the layout paths in your application.ini

In order to store the layouts in /application/layouts/scripts, add the following to /application/configs/application.ini

resources.layout.layoutpath = APPLICATION_PATH "/layouts/scripts"

You can now create several layouts, something around this model:

    headTitle() ?>
    headScript() ?>
    headStyle() ?>

    render('header.phtml') ?>

layout()->content ?>
render('footer.phtml') ?>

By default, your application will use the layout in /application/layouts/scripts/layout.phtml, but if you want some pages to be rendered using another layout you can tell ZF to do so like this:

// Within controller
// note that the file that's going to be rendered is /application/layouts/scripts/backend.phtml

//Within view script
$this->layout()->setLayout('other-layout'); ?>

More info on using Zend_Layout:


Remove MS Word formatting using PHP regular expressions

Recently i’ve had a client which would only paste into the CMS text from MS Word. Normally a JS should have been in place to automatically strip the crazy tags Word adds, but on this project this was not the case. Here’s hos to remove MS Word formatting using PHP regular expressions:

// it will remove all  comments
preg_replace('//i', '', $string);


// it will remove all  comments
preg_replace('//i', '', $string);

Magento top navigation show products in category

I had a structure like this in admin

– Category
– – SubCategory1
– – – Product 1
– – – Product 2
– – – Product 3
– – SubCategory2
– – – Product 1
– – – Product 2
– – – Product 3

but the default magento navigation would only list my categories and subcategories, not the products. after a bit of tweaking around i noticed that both SubCategory1 and SubCategory2 are level 1 so i only needed to find a way to display products for those categories. That is done easily by extending /core/Mage/Catalog/Block/Navigation.php into your local (/local/Mage/Catalog/Block/Navigation.php) and replacing the function _renderCategoryMenuItemHtml with the one below:

    protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
        $isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
        if (!$category->getIsActive()) {
            return '';
        $html = array();

        // get all children
        if (Mage::helper('catalog/category_flat')->isEnabled()) {
            $children = (array)$category->getChildrenNodes();
            $childrenCount = count($children);
        } else {
            $children = $category->getChildren();
            $childrenCount = $children->count();
        $hasChildren = ($children && $childrenCount);

        // select active children
        $activeChildren = array();
        foreach ($children as $child) {
            if ($child->getIsActive()) {
                $activeChildren[] = $child;
        $activeChildrenCount = count($activeChildren);
        $hasActiveChildren = ($activeChildrenCount > 0);

        // prepare list item html classes
        $classes = array();
        $classes[] = 'level' . $level;
        $classes[] = 'nav-' . $this->_getItemPosition($level);
        if ($this->isCategoryActive($category)) {
            $classes[] = 'active';
        $linkClass = '';
        if ($isOutermost && $outermostItemClass) {
            $classes[] = $outermostItemClass;
            $linkClass = ' class="'.$outermostItemClass.'"';
        if ($isFirst) {
            $classes[] = 'first';
        if ($isLast) {
            $classes[] = 'last';
        if ($hasActiveChildren) {
            $classes[] = 'parent';

        // prepare list item attributes
        $attributes = array();
        if (count($classes) > 0) {
            $attributes['class'] = implode(' ', $classes);
        if ($hasActiveChildren && !$noEventAttributes) {
             $attributes['onmouseover'] = 'toggleMenu(this,1)';
             $attributes['onmouseout'] = 'toggleMenu(this,0)';

        // assemble list item with attributes
        $htmlLi = ' $attrValue) {
            $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
        $htmlLi .= '>';
        $html[] = $htmlLi;

        $html[] = '';
        $html[] = '' . $this->escapeHtml($category->getName()) . '';
        $html[] = '';

		// Grabbing the products for the category if it's level is 1
		if ($level == 1) {
			$catId = $category->getId();
			$categorie = new Mage_Catalog_Model_Category();
			$categorie->load($catId); // this is category id
			$collection = $categorie->getProductCollection()->addAttributeToSort('name', 'asc');
			$html[] = '
    '; foreach ($collection as $pc) { $p = new Mage_Catalog_Model_Product(); $p->load($pc->getId()); $data = $p->_data; $html[] = '
  • '.$data['name'] .'
  • '; } $html[] = "
\n"; } // Done // render children $htmlChildren = ''; $j = 0; foreach ($activeChildren as $child) { $htmlChildren .= $this->_renderCategoryMenuItemHtml( $child, ($level + 1), ($j == $activeChildrenCount - 1), ($j == 0), false, $outermostItemClass, $childrenWrapClass, $noEventAttributes ); $j++; } if (!empty($htmlChildren)) { if ($childrenWrapClass) { $html[] = '
'; } $html[] = '
    '; $html[] = $htmlChildren; $html[] = '
'; if ($childrenWrapClass) { $html[] = '
'; } } $html[] = ''; $html = implode("\n", $html); return $html; }

The rest of the file stays the same. Note this works on magento > 1.4

PHP zend framework

Zend Framework disable layout and view rendering

When using Zend Framework, sometimes you need to create a controller action which just does something, doesn’t need to display anything to the user. To disable the layout and the view rendering add the following in your action:

		// disable layout
		$this -> _helper -> layout() -> disableLayout();

		// disable view rendering
		$this -> _helper -> viewRenderer -> setNoRender();