Blog

Mark parent category menu active on single custom post view in WordPress

Recently I developed a theme for one of my clients and I had to highlight the menu item of the parent category in the main menu when one of its associated single custom posts was viewed. For that, I had to add an action in my functions.php file for nav_menu_css_class. It returns the ‘active’ class, which WordPress adds to the parent category menu item in the main menu. You can see the code at Gist here or below.

<?php

add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2 );

function add_current_nav_class($classes, $item) {
// Getting the current post details
global $post;

// Getting the post type of the current post
$current_post_type = get_post_type_object(get_post_type($post->ID));
$current_post_type_slug = $current_post_type->rewrite['slug'];

// Getting the URL of the menu item
$menu_slug = strtolower(trim($item->url));

// If the menu item URL contains the current post types slug add the current-menu-item class
if (strpos($menu_slug,$current_post_type_slug) !== false) {
$classes[] = 'active';
}

// Return the corrected set of classes to be added to the menu item
return $classes;
}
?>

Adding above code in you functions.php file will do the trick.

Click here to read more on WordPress.

Why we moved from OpenShift to Google Cloud

The good old days

When OpenShift was in its version 1 it was great from the customer’s point of view with a low budget. OpenShift v1 had free offers to deploy apps and add a custom domain to it. There was no SSL support but it could be handled via the CloudFlare solution, making the overall solution a great one.

Then there comes OpenShift v2

RedHat then launched OpenShift v2 and added a restriction that gears will be shut down after a certain period of inactivity and also removed the support for custom domain.

That was the time that we had to move to some other cloud platform that could support our apps with low resources and without investing anything. Because all good things in life are free.

Google Cloud to the rescue

So we came across the Google Cloud platform offering and we had $300 credit in a free trial for 6 months. That was more than enough for us to start with. And in fact, they have micro virtual machine instances that are free for life with 30GB storage. That is more than enough for small apps to run for free that are under experiment or don’t have any monetization plans.

So, we migrated several of our apps from OpenShift to Google Cloud and started leveraging free credits provided by Google.

Just this week we consumed all our credit and our trial expired. Our virtual machine instances were shut down, but it didn’t take long before we enabled billing on our project and downgraded our instances to micro ones which are free. That gave us such a relief that our apps are still running although with low resources but it is fine as they are not our revenue-generating apps and are just experiments.

Click here to read more about Google.

Codenvy and BitBucket Integration

OK, this post is a quick one about BitBucket integration. Let’s go!

In your Codenvy workspace go to Profile > Preferences > SSH > VCS and generate a key and give it the name bitbucket.org. This will generate a key for you. Copy this key by viewing it which we will use later on BitBucket.

Now go to BitBucket, select your repository, and then go to repository settings. Under the Access Keys menu click Add Key button. Give a label as bitbucket.org and paste the key copied in the previous step here and save.

Now on the Codenvy project go to the Git menu and add remote. You need to give the SSH URL to your BitBucket repo here. Add it and you can now pull the repo from BitBucket. Awesome!

Click here to read more about git on the Ibexoft blog.

Displaying Multiline Title in WordPress

In WordPress when the title is written it is usually displayed in a single line on front-end until it is too long to display in one line. But if you want to write the title in multiline and want to line break the title after a certain word how can you do that?

It is pretty simple. Since we know the fact that when the title is displayed it is a heading which is displayed using an HTML tag of course. So, taking benefit of HTML we can just insert an HTML line break element <br/> just after the word in our title where we want the line break. Like this:

“This is my title <br/> with my own line break”

WordPress Multiline Title

This will be displayed in two lines when you publish the post.

Read more about WordPress.

Different color for each menu item in WordPress

In a recent project, I got a requirement that each menu item should be highlighted in a different color when visited. The menu items and their required active colors were:

  • Home – Green
  • Portfolio – Blue
  • Team – Yellow
  • Contact – Red

These colors were to be applied only when that page is being visited otherwise color of the menu item should be the default black color.

So, if a user is visiting the home page then the menu item should something like this

home_menu.png

And if the user is visiting the Portfolio page then the menu should be something like this

portoflio_menu.png

Considering that this was a WordPress theme project where we were using Understrap as a base theme which is based on Twitter Bootstrap. So, when user visits, for example, a home page WordPress will attach a .active CSS class to it. Taking advantage of that we added different classes for each menu item and then used the following rule to make menu item colors different:

.navbar-nav > .home.active > a {
    color: green!important;
}
.navbar-nav > .portfolio.active > a {
    color: blue!important;
}
.navbar-nav > .team.active > a {
    color: yellow!important;
}
.navbar-nav > .connect.active > a {
    color: red!important;
}

CSS Class Chaining Method

We utilized the class chaining method here. If you note that .home.active classes are chained together without space and which means it will select an element with both these classes.
That did the trick and all menu items were in a different color.

Rollover image – Change image on hover/mouse over

Often when designing websites static or dynamic, PHP or ASP.Net, Laravel or WordPress, you have to design in a way that if the user hovers an image it gets changed and an alternate image is displayed. This can be easily achieved via simple HTML events. Here is the trick:

<img src="FIRST IMAGE URL GOES HERE"
onmouseover="this.src='SECOND IMAGE URL GOES HERE'"
onmouseout="this.src='FIRST IMAGE URL GOES HERE - AGAIN'" />

It is simple as that.

Click here to read more tips.

Sharing data between Laravel and Angular

When building applications with Laravel and Angular you might come across a problem where you want to print data using AngularJS brackets {{}} but before it can be parsed by Angular, Laravel blade engine parses it and tries to replace the value if it finds one. Otherwise, Laravel will start complaining about the variables. To solve that you just need to prepend brackets with @ sign so the blade engine knows that you just need to ignore this expression and AngularJS will take care of it. And AngularJS will parse it and replace the variables with actual data.

Below is a sample snippet to do this:

@{{ article.body }}

In this snippet, the Laravel blade engine will ignore this and AngularJS will parse it and replace it with the article.body data it has.

Click here to read more about Laravel and Angular.

Laravel: Specified key was too long error on migration

When you install a new Laravel project with ‘laravel new’ and run the migration that comes with it you might get the following error:

#php artisan migrate
Migration table created successfully.


 [Illuminate\Database\QueryException]
 SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))

[PDOException]
 SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

To solve this error edit your app/Providers/AppServiceProvider.php file.

Add the namespace:

use Illuminate\Support\Facades\Schema;

and then add the following line in boot() method of your AppServiceProvider class.

use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
 /**
 * Bootstrap any application services.
 *
 * @return void
 */
 public function boot()
 {
   Schema::defaultStringLength(191);
 } 
}

This should solve your problem. Just delete all the tables and rerun the migration.

Click here to read more about Laravel.

How to start E-commerce business in Pakistan? Part-1

It is very impressive how Pakistan is progressing in the field of IT, telecom and broadband penetration. Pakistan’s e-commerce market is growing tremendously and presents great opportunities for everyone.

Pakistan’s broadband penetration has grown to over 40 million (38 million are on 3G/4G), up from just 2 million in April 2014. With more than 45 million smartphones in the market, Pakistan is on track to become one of the largest internet population in the world.

Foreign players and giants like Alibaba group are planning to enter Pakistan’s huge potential market. It is still a rising market and there is a massive potential for small businesses alike.

Why e-commerce?

E-commerce has several advantages over the traditional physical business. A few major benefits are:

  • Low investment as compared to a physical business
  • Can target a wider audience
  • You can start business immediately

Physical vs Online

For a physical business initial investment is huge. You need a place, a shop or an office. If you are going to buy it will cost you millions. If you go for the rental, you will still have to pay a considerable amount in advance and your monthly operational cost will be high. Then there are other one-time costs involved like furniture, lighting, and other stuff. You have to spend a big amount even before you start selling.

As compared to starting an e-commerce business you just need to buy a domain and hosting or go for platforms like Shopify or BigCommerce. You can get your site developed at very cheap rates from freelancers or if you are a power user then you can set up your own site that will cost you free. Few ready-made solutions that are available are:

  • WordPress/WooCommerce
  • OpenCart
  • Prestashop
  • Magento

There are even lot of other solutions available that you can search and try. If you do this on your own it will cost you free.

Setup and Operations cost is low

The next step would be to enter your inventory in your e-commerce store. That shouldn’t take more time. It could even be done sooner than setting up a physical shop. Due to this, you can start an online business immediately as compared to a physical store.

Target wider audience

If you have a physical store then you can target only the people in your area or city depending on where you are located. If you are a small businessman with a shop in your locality, your customers could only be from your nearby areas. If you have a shop in a bigger mall or market then your audience would be wider. But if you open an online e-commerce store you can target not only your city but your country, or you can even go international, resulting in huge potential for great revenue.

This is just the tip of the iceberg. But remember you must have some business sense since an online store is also a business. Without business strategy and marketing since you won’t be successful.

In the coming episodes, we’ll talk more about setting up an e-commerce store, the role of technology, business and marketing tips, how to grow your online business and much more.

If you feel this is a good read and want to read more about a topic you are interested in then feel free to leave me feedback and I’ll add it to my upcoming topics list.

Setup CodeIgniter Docker container for development

Docker

Docker is the world’s leading software container platform. Developers use Docker to eliminate “works on my machine” problems when collaborating on code with co-workers. Operators use Docker to run and manage apps side-by-side in isolated containers to get better compute density. Enterprises use Docker to build agile software delivery pipelines to ship new features faster, more securely and with confidence for both Linux and Windows Server apps. If you want to learn more about Docker head to their What is Docker section here.

CodeIgniter

CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications. If you want to learn more then head to their official website which has great documentation as well at https://codeigniter.com/

CodeIgniter on Docker

If you’re excited about using Docker for your development and you are working on a CodeIgniter (CI) based PHP project then you are lucky. It is very easy to setup CI based project with Docker. Just follow the instructions below to setup a fresh CI project with Docker.

docker-compose up -d

It will spin up two containers—one for the app itself with Nginx and another for MariaDB for your DB. App container will create a directory in your project folder and install CodeIgniter in it.

Now browse http://localhost:8000 in your browser and you’ll see the CodeIgniter page. It’s that easy.

Click here to read more about Docker.