Posted on Leave a comment

Quick Composer Tutorial – Learn Composer PHP in 10 Minutes

I’ve been writing code in PHP for more than 5 years and it’s a shame to admit that I didn’t know composer until recently. I’ve read some tutorials but couldn’t get my head around it. I  have to admin that since I code mostly small web projects (WordPress plugins…) I don’t find the need for a package manager. However, to call myself a PHP developer, I have to know its most popular package manager. In this post, I’m going to share my understanding with composer, how to get started with it so you might benefit from my experience too.

What is composer?

Composer is a package manager tool that helps you deal with PHP dependencies. Previously, when I didn’t use Composer, I mostly use include_once to include the libraries. In addition, I had to download the libraries and put to my project all by myself. It’s no longer the case with composer.

Let’s see how it works.

Installing Composer

Installing composer is simple for all environment. If you are on Windows, you can download the installation file and go through the process in just a minute. I’m not going too much into details here since there directions on https://getcomposer.org is detailed enough.

Using composer

Using composer is a very straightforward process. We first tell composer what library to include and it will get that library for us.

Start a fresh project with composer
Now we have composer installed, let’s get started and create an empty folder and type in

composer init

The console will walk you through various steps to get information about the project you want to create. I use the default option for most of the question. After finishing all the steps, composer will create a composer.json file. Open this up and you’ll see the content you’ve just entered.

Here is the content of the composer.json

{
    "name": "myn/lab",
    "description": "This is a test composer project",
    "license": "MIT",
    "require": {}
}

As you can see, the “require” element is just an empty object. When we include dependencies, all of them will be listed here.

Adding libraries to composer

Now, we have a composer project. Let’s add a library and use it. I’m going to include a logging library called monolog in the project. You can find details about that project here.

To get the monolog library, simply run:

composer require monolog/monolog

Composer will go ahead and get monolog and also its dependencies (if any) for us.

If you check the composer.json, you’ll see it is updated:

{
    "name": "myn/lab",
    "description": "This is a test composer project",
    "license": "MIT",
    "require": {
        "monolog/monolog": "^1.24"
    }
}

Now, let’s write some code to use monolog.

Include the autoload.php file

If you notice, composer also created a folder called vendor. Inside it lies the libraries and one file called autoload.php. When creating php script, you simply include the autoload.php file to have access to all dependencies you installed.

I’m going to create an index.php in the root folder and include the autoload.php file.

<?php

require 'vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\StreamHandler;


$log = new Logger('test-logger');
$log->pushHandler(new StreamHandler('C:\\xampp\\htdocs\\lab\\log.txt', Logger::WARNING));

$log->warning("Testing monolog");

If I run the file now, there will be a log.txt file created at C:\xampp\htdocs\lab with the below content:

 

[2018-12-10 09:55:15] test-logger.WARNING: I do some test [] []

You may wonder how do I know to type in the code in the php file? Well, the maker of monolog provides example code on the library page on packagist.

Conclusion

Now I have a basic understanding of composer, the PHP package manger. I hope this post can help you understand composer a bit better. I still have a lot to learn and I’ll share all my findings here with you.

 

Posted on Leave a comment

Seven days challenge to learn one new topic each day

So I decided to improve my skills as a developer. I’m going to do a 7 days challenge to learn the new topics that I always wanted to learn but couldn’t arrange the time to do so. This is the first 7 days. I’m going to learn the following topics:

Day 1: PHP Composer

Day 2: PHP Autoload

Day 3: PHP Namespace

Day 4: PHP Generators

Day 5: PHPUnit

Day 6: PHP Hashing

Day 7: WordPress Nonce

Here are the additional topics I would like to learn in later days:

  • PHP namespaces
  • Cross Site Request Forgery

At the end of each day, I’m going to summarize all the things I learned in one article.

Posted on 14 Comments

Hide Header and Footer In Elementor Within ONE minute

Hide Header and Footer In Elementor Within ONE minute 1

Elementor is awesome. That comes from a user has just begun to use the tool. However, when you start creating a page with Elementor, you may see the default header and footer from your theme. In my case, I use twenty seventeen theme and the editor would look like below. As I am going to create a dedicated page, I would like to hide header and footer in Elementor. What can I do about that?

default page template wordpress

Why does this happen?

Elementor uses the default template file from your theme. In the default template file of most themes, there are header and footer. That’s why you will see the header and footer of your theme in Elementor’s editor. If you deactivate Elementor, you would see the page template at the right sidebar:

Hide Header and Footer In Elementor Within ONE minute 2

Now, as you may guess, the solution would be using a different page template that doesn’t contain your theme’s header and footer. That’s what we are going to do.

How to hide header and footer in Elementor

Update Mar-24-2019

Elementor has enabled default support for this feature. All you need to do is following this picture:

Hide Header and Footer In Elementor Within ONE minute 3

If you still have problem following the method above, let’s continue:

 

Here are what we are going to do:

  • Install a plugin called Blank Slate
  • Go to your page/post created with Elementor
  • Edit its template to use Blank Slate

Let’s go into more details below.

As mentioned above, we need a template file that doesn’t have the theme’s header and footer. If you know a bit of PHP, you can do it easily. However, for people who don’t know code, that would be a difficult task. What we are going to do is simple for everyone. We are going to install a plugin that creates that template for us. The plugin is called Blank Slate.

After installing and activating the plugin, if you go back to edit your page and click on the page template select box, you will see there is a new template file called Blank Slate. That’s what we are going to use. All you need to do next is to click on publish (or save draft) and click on edit with Elementor:

Hide Header and Footer In Elementor Within ONE minute 4Hide header and footer in Elementor

 

Now, you can see that in Elementor, there isn’t header or footer:

Hide header and footer in Elementor

That’s it! You’ve successfully hidden the header and footer for your Elementor editor. It’s time to create some awesome pages with this page builder. If you enjoy my WordPress tips, don’t forget to share this post.

Posted on Leave a comment

How To Limit Max Order Quantity In WooCommerce

How To Limit Max Order Quantity In WooCommerce 5

As a shop owner, you may want to sell as many items as possible. However, there are cases you want to limit the number of items a single customer can purchase. The reasons can vary: maybe you have limited stock, maybe you don’t want a single customer stocks many items and reseller it later with much higher price… Whatever the reason, you will feel the need for a function to set the maximum number of item per product that a single customer can purchase. The good news is, there is a solution for that and it’s totally free.

Meet the plugin that enable max quantity of items that a single customer can purchase PER PRODUCT

Remember, the limit is PER PRODUCT (I’m sorry for the uppercase but this is important). For example, you have leather bag and plastic bag. You can set the quantity limit for leather bag is 2 and plastic bag is 3. So, your customer can order up to 5 items total but she cannot buy more than 2 leather bags or 3 plastic bags.

Now, let’s install this plugin called Woocommerce Max Quantity

Installing the plugin is very straightforward. You can search the plugin by entering its name in Plugins->Add new. You may need to scroll down a bit to see the plugin since at the time of this writing, it stays at the bottom of the first page.

After activation, go to the product you want to set the limit. Click on Inventory tab. You will see there is a new box appears to enable the max quantity. In the example below, a single customer can only purchase 4 fake lamborghini cars from my store:

Set max quantity for single customer on a product

Now, update the product and let’s go to the product page and try to order more than 4 items:

Max Quantity order per item Woocommerce

As you can see, the store sends a notice to customer when she tried to order more than the limit.

Conclusion

The plugin does a great job setting the limit for the order per single product. If any improvement needed, I think that’s a function to display the limit right below the order button so the customer can see the limit right before she enters the order quantity.

Posted on Leave a comment

How To Watch Movies, Listen to Music, Read Books On Your Computer From Your Phones

How To Watch Movies, Listen to Music, Read Books On Your Computer From Your Phones 6

Nowadays, having a phone that has hundreds GB of storage is not uncommon. In fact, Samsung Note 9 has 1TB of storage. That’s a lot of spaces to store all kind of movies, music and books that an average person need. However, there are two obvious problems:

  1. Not all people can afford a Note 9
  2. 1TB is big for a phone but when it comes to 4k movies, that’s not a lot.
  3. Storing a lot of data can make your phone runs slowly. Searching for your recorded videos, captured images may take much longer time.

Chances are we all have computer that can hold a lot more spaces. I have a PC that has 1TB external HDD, 2TB SATA HDD and that’s more than enough. My phone, sadly has only 16GB.

Sometimes I don’t want to watch movies on my PC, I want to watch them on my phone. As you can relate, 16GB of internal storage is too little, especially when I don’t have a SD card slot.

So, what’s the solution?

What if you can access all the files on your PC from your phone? You can lay back on your bed and enjoy the movies without worrying about waiting hours to copy the big media files between computer and phone.

Well, that’s actually possible and very easy.

How to access movies, music, books, files on your PC from your phone or other devices?

Before we begin, there is one requirement:

Both your phone and your pc (Mac) must connect to the same wifi network.

Now, let’s get start with step 1: Install XAMPP on your PC.

Downloading XAMPP is free and easy. You can download it here.

After that, you can run and install that program. It is recommended to keep your UAC settings at the lowest level.

After installing, you can now start apache server like this:

How To Watch Movies, Listen to Music, Read Books On Your Computer From Your Phones 7

As you can see, I have both Apache server and MySQL server started. However, you only need to click on the start button at the right of Apache. In the screenshot above, Apache was started successfully.

Step 2: Create a symbolic link of your media folder in C:\xampp\htdocs

Open your command prompt window and cd to C:\xampp\htdocs

How To Watch Movies, Listen to Music, Read Books On Your Computer From Your Phones 8

Now, we are going to create  a link to your media folder using the following command

mklink /d LINK TARGET

For example, I want to create a folder MEDIA in C:\xampp\htdocs that link to C:\Users\VIDEOS, I would type this:

How To Watch Movies, Listen to Music, Read Books On Your Computer From Your Phones 9

Notice that, you must not create the MEDIA folder. The mklink command will create that folder for you.

 

Now, you have successfully created a symbolic link to your media folder inside the htdocs fodler.

Step 3: Find your PC’s local IP address

Open command prompt and type

ipconfig

You will see a lot of data are printed on the screen. However, there is only one line you need to pay attention to, that is IP V4 address:

How To Watch Movies, Listen to Music, Read Books On Your Computer From Your Phones 10

In this case, the ip address is 192.168.1.11

Step 4: Access the media files from your phone

Now, you can access the media files from your phone. Let’s open your browser and enter the IP address you got from step 3 and slash(/)MEDIA. Here is what I see:

How To Watch Movies, Listen to Music, Read Books On Your Computer From Your Phones 11

As you can see, I have one movie file. I can click on that to start enjoying the movie.

Conclusion

This method works only when your phone and your PC connect to the same WiFi network. It’s a great convenience when your phone doesn’t have much space and you want to access to the library of entertainment on your PC. If you have any questions, please let me know.