WordPress Customization

WordPress Customization to achieve minimalist, user friendly and fast response site!

WordPress Limit Post Excerpt By Characters Without Truncation
Customizing Arrow for Newer Older Entries
Auto Resizing Posts Pages Image by Max Width Height
Customizing Breadcrumb NavXT WordPress Theme
Optimizing Robots.txt for Better SEO

WordPress Limit Post Excerpt By Characters Without Truncation

Ayumilove.net homepage post excerpt had some makeover to reduce text bloat. Post excerpt is a snippet of your post first few sentences. The excerpt is Twitter-styled by limiting it to 240 characters. Below is the code I used in my theme functions.php which might be useful for your WordPress-based site.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// [AYUMILOVE_START] Twitter Style Post Excerpt
function get_twitter_style_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 240);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt . '...';
return $excerpt;
}
add_filter('the_excerpt', 'get_twitter_style_excerpt');
// [AYUMILOVE_END] Twitter Style Post Excerpt

Customizing Excerpt Notes
1. The code above does not truncate last word if meets max characters. Example:
1a. Original Statement = The quick brown fox jumps over the lazy dog.
1b. Truncated Statement = The quick brown fox jumps ove …
1c. Truncation fix = The quick brown fox jumps …
2. Change character length 240 to the length you are comfortable for your excerpt.
3. If the excerpt is not styled with css, add the paragraph tags < p >
4. Implementing this code into Theme functions.php avoids installing plugin.

Customizing Arrow for Newer Older Entries

Instead of using mini triangle .gif pictures to highlight the direction of your newer and older entries, you could use the HTML arrows to do it. Below is an example of adding arrows to your main index.php and your post.php
Continue reading

Factory Design Pattern Example

Factory Design Pattern is used to centralizes all new object instantiation under 1 class, or in other words, having 1 class responsible for creating objects! The object created by Factory will use a common interface to be compatible with other related finer detail classes.

The objective of migrating creation responsibility is to prevent client class being exposed to many classes. Client class are the ones who uses these objects by instantiating them within itself. This posses a major risk where when a used class is removed or updated to a new name, the client class will crash unless updated to be in sync with them.

Imagine if there were 20 client classes using a class which frequently updates itself, you will need to re-update all 20 classes! The solution is to have an intermediary, such as a Factory Pattern to build those objects for the client. Those created objects will have a common interface (example: Weapon) for finer details (example: Sword, Bow, Axe, Polearm, Spear, Gun, Claw, Wand, Staff).

Before Implementing Factory Pattern

The code below shows a Client who couples to 4 weapon classes (sword, spear, bow and crossbow). Couples or coupling means having a relationship with another foreign class via instantiation or reference. If more classes were coupled between clients, it will multiply the coupling substantially.

public class ClientA
{
   public function ClientA
   {
      var sword:Sword = new Sword();
      var spear:Spear = new Spear();
      var bow:Bow = new Bow();
      var crossbow:Crossbow = new Crossbow();
   }
}
public class ClientB
{
   public function ClientB
   {
      var sword:Sword = new Sword();
      var spear:Spear = new Spear();
      var bow:Bow = new Bow();
      var crossbow:Crossbow = new Crossbow();
   }
}

As the example displayed above, 1 client has reference to 4 classes. 2 client makes it 8 (2 x 4 = 8). Now when the referenced classes increased to 6, you will have a total of 12 couplings (2 x 6 = 12). So how does this affects in software or game development? If 2 referenced class were to be updated/removed, those 2 clients have to be re-updated.

After Implementing Factory Pattern

Here is the code with Factory Pattern implemented. Noticed that both client A and B has instantiated a WeaponFactory to produce the weapons they need. They do not need to know the finer details of the weapon, but they get the weapon to be used. Similarly if you were purchasing a car, you drive it but not into manufacturing the car engine parts. As a user, you interact with the Car interface.

public class WeaponFactory
{
   public function WeaponFactory(){};
   public function create(type:String):Weapon
   {
      var weapon:Weapon;
      switch(type)
      {
          default:
          case 'Sword' : weapon = new Sword(); break;
          case 'Spear' : weapon = new Spear(); break;
          case 'Bow' : weapon = new Bow(); break;
          case 'Crossbow' : weapon = new Crossbow(); break;
      }
   }
}
public class ClientA
{
   public function ClientA
   {
      var weaponFactory:WeaponFactory = new WeaponFactory();
      var sword:Weapon = weaponFactory.create('Sword');
      var spear:Weapon = weaponFactory.create('Spear');
      var bow:Weapon = weaponFactory.create('Bow');
      var crossbow:Weapon = weaponFactory.create('Crossbow');
   }
}
public class ClientB
{
   public function ClientB
   {
      var weaponFactory:WeaponFactory = new WeaponFactory();
      var sword:Weapon = weaponFactory.create('Sword');
      var spear:Weapon = weaponFactory.create('Spear');
      var bow:Weapon = weaponFactory.create('Bow');
      var crossbow:Weapon = weaponFactory.create('Crossbow');
   }
}

In the program above, each client is interacting with Weapon Interface, not the Sword, Bow, Crossbow and Spear directly. These creates a loose coupling between client and weapon class. Any changes made in Sword affects 1 class, which is the Factory class. Since all instantiation or changes are done in 1 class, its easier to do maintenance without crashing other client classes.

Strategy Design Pattern Example

Strategy Design Pattern is used to manage multiple subclasses (each implements various behaviors and shares some common behaviors) via composition instead of inheritance. This design pattern encapsulates each algorithm to be used interchangeably during runtime depending on client usage.

Basic Coding

This is what I used to code in my previous works. It’s simple and gets the thing done. When there are more weapons added, this class will be modified again. This poses risks where changing a class after testing might break it. Also, if there is complex formula involved in calculating the output for each weapon, this Weapon Class will be very difficult to read with hundreds or thousands lines of codes.

public class Weapon extends Object
{
   public var attack:int;
   public var soundEffect:String;
   public function getDamage(weaponType:String):int
   {
      if (weaponType == 'Sword' || weaponType == 'Spear')
      {
         return attack * 10;
      }
      if (weaponType == 'Bow' || weaponType == 'Crossbow')
      {
         return attack * 8;
      }
      return 0;
   }
   public function getSound(weaponType:String):int
   {
      if (weaponType == 'Sword' || weaponType == 'Bow')
      {
         return 'Boing';
      }
      if (weaponType == 'Spear' || weaponType == 'Crossbow')
      {
         return 'Boom';
      }
      return '';
   }
}

Advance Coding

Here I breakdown the main code into Interfaces and Multiple Classes. It seems that it requires more effort and more lines of codes compared to the basic coding. However, in the long term, it is easier to maintain the code as each class only holds a small portion of code (easier to read and find). Advance coding applies polymorphism (object with same method but does different things) and adheres to the principal of “Open for Extension, Close for Modification”.

public interface DamageFormula
{
   function getDamage():int;
}
public class MeleeDamageFormula implements DamageFormula
{
   public function getDamage(attack:int):int { return attack * 10; }
}
public class RangeDamageFormula implements DamageFormula
{
   public function getDamage(attack:int):int { return attack * 5; }
}
public interface SoundEffect
{
   function getSound():String;
}
public class BoingSoundEffect implements SoundEffect
{
   public function getSound():String { return 'Boing'; }
}
public class BoomSoundEffect implements SoundEffect
{
   public function getSound():String { return 'Boom'; }
}
public interface Weapon
{
   function getDamage():int;
   function getSound():String;
}
public class Sword implements Weapon
{
   public var attack:int;
   public var damageFormula:DamageFormula = new MeleeDamageFormula();
   public var soundEffect:SoundEffect = new BoingSoundEffect();
   public function getDamage():int { damageFormula.getDamage(attack); }
   public function getSound():String { soundEffect.getSound(); }
}
public class Spear implements Weapon
{
   public var attack:int;
   public var damageFormula:DamageFormula = new MeleeDamageFormula();
   public var soundEffect:SoundEffect = new BoomSoundEffect();
   public function getDamage():int { damageFormula.getDamage(attack); }
   public function getSound():String { soundEffect.getSound(); }
}
public class Bow implements Weapon
{
   public var attack:int;
   public var damageFormula:DamageFormula = new RangeDamageFormula();
   public var soundEffect:SoundEffect = new BoingSoundEffect();
   public function getDamage():int { damageFormula.getDamage(attack); }
   public function getSound():String { soundEffect.getSound(); }
}
public class Crossbow implements Weapon
{
   public var attack:int;
   public var damageFormula:DamageFormula = new RangeDamageFormula();
   public var soundEffect:SoundEffect = new BoomSoundEffect();
   public function getDamage():int { damageFormula.getDamage(attack); }
   public function getSound():String { soundEffect.getSound(); }
}

Summary

If I had to add more weapons and sound effects, basic coding becomes really messy and difficult to read with all of the if-statements conditions whereas for Advance Coding, it requires some time to properly manage these classes into folders.

The codes in Advance Coding section can be further simplified by implementing Factory Pattern which will be discussed in my subsequent post!

How to Setup uTorrent Port Forward Settings Correctly to fix Closed Ports Issues

After installing uTorrent, you will need restart the application after configuring 4 important steps listed below:

(A) Setup Static IP Address for you Computer

The reason why you want to set this up because if your router is being connected to multiple devices through wired or wireless, the dynamic ip address given will always differ depending which devices is start up first. Therefore, to avoid this hassle of changing ip addresses, set this once and for all.

1) Start > Settings > Control Panel > Network Connections > Local Area Connection
2) Click “Properties”
3) In the list, scroll down and select “Internet Protocol (TCP/IP)
4) Click “Properties” to adjust the settings for your TCP/IP
5) Tick “Use the following IP address” instead of “Obtain an IP address automatically”.
6) Enter your settings for your static IP address for your PC:
6a) IP Address : 192.168.1.5 (Example)
6b) Subnet Mask: 255.255.255.0
6c) Default Gateway: 192.168.1.1 (This is always same as its refers to your router)
7) You can use Google DNS Server to speed up website loading.
7a) Preferred DNS Server: 8.8.8.8
7b) Alternate DNS Server: 8.8.4.4

(B) uTorrent

The most important step here to get the Closed Ports open is to have the bind_ip matching with your static ip. If you have not configured static ip for your computer, you can also use the dynamic ip address. To get your dynamic ip address (Start > Run > Type: “cmd /k ipconfig”). This will open up Command Dos Prompt Window, look for IP Address: (Example: 192.168.1.5), plug that value into net.bind_ip as shown below.

1) Run uTorrent.exe
2) Click “Options” > “Preferences”
3) Select “Advanced” and set following settings below:
3a) net.bind_ip: 192.168.1.5 (Example) (Recommended: Reset to DEFAULT = BLANK)
3b) net.outgoing_ip: 192.168.1.5 (Example) (Recommended: Reset to DEFAULT = BLANK)
3b) isp.primary_dns: 8.8.8.8 (Google DNS Server)
3c) isp.secondary_dnds: 8.8.4.4 (Google DNS Server)
3d) net.max_halfopen: 50 (Optimizing for Windows XP SP2)
4) Click “Apply” Button.
5) Select “Connection” and set following settings below:
5a) Untick “Enable uPnP Port Mapping”
5b) Untick “NAT-PMP Port Mapping”
5c) Untick “Randomize Port Each Start”
5d) Tick “Add Windows Firewall Exception”
5e) Insert port number: 55000 (Example) Use number between 10000-65000
6) Click “Apply” Button.
7) Select “BitTorrent”
7a) Tick all except “Limit Local peer bandwidth”.
7b) Protocol Encryption Outgoing: Enabled.
7c) Tick “Allow incoming legacy connections”.

(C) Setup Router

This is to save your computer static IP in router for Port Forwarding. If this isn’t setup correctly, you will receive status “Closed” when you use any Open Port Check Tool.

1) On your router
2) Open your Internet Browser
3) Type in “192.168.1.1″ into address box as the URL (This will access your router settigns)
4) Type in your username and password to access
5) For D-Link Router:
5a) Under “Advanced Tab” (Horizontal Menu), click “Lan Clients”
5b) Enter your Static IP address that you made (Example: 192.168.1.5 and your pc name)
5c) Click “Apply” Button.
6) Save router settings:
6a) Under “Tools” (Horizontal Menu), click “System”. Click “Save and Reboot”.

(D) Anti Virus Firewall

You need to customize your firewall settings to enable Incoming/Outgoing uTorrent Access! Otherwise, you will not be receiving or sharing any files with uTorrent since its blocked by your computer firewall.

If you are using Nod32 ESET Smart Security:
1) Right-click on the icon, select “Advanced Setup”
2) On the left side, under Personal Firewall Category, click “Rules and Zones”
3) Click “Setup” for the Zone and Rule Editor.
4) Search for uTorrent Application, and click the + symbol beside it to expand.
5) Select “Allow Communication for uTorrent.exe” and click “Edit” button.
6) Edit Rule with the ones below then Click OK till you exit the setup.
6a) Direction: Both (Incoming and Outgoing)
6b) Action: Allow
6c) Protocol: TCP and UDP
6d) Profile: For every

(E) Open Port Check Tool

Visit any of the sites below and enter the port that you have selected: Example Port: 55000. If the site says “OPEN”, that means you have setup Port Forwarding correctly!

1) CanYouSeeMe.org
2) www.yougetsignal.com/tools/open-ports/

Saving Loading Flash Game Save Files (Shared Object)

Tired of losing your hard-work Flash Game save files? There is a way you save them safely!
Here is how to do it! First, identify the operating system that you are currently using.
(Example: Windows 7) Next, use the list below to get to your Flash game save folder.
The file extension is .sol, save them in a place to avoid your Internet Browsers clearing
them off as garbage. CCleaner can remove your .sol files too!
Continue reading