Nasrul Hazim Bin Mohamad

Migrate from WordPress to Laravel – Part 1

Dec
22

Assalamualaikum / Hi,

I’ve been using WordPress, since 2012 – 5 years, of service. I think it’s time to migrate to another platform – Laravel. I’ve been reading how murze.be did.

Basically, I got the idea how he did. But, as my sense of explorer, want to learn something new, I read few more articles, packages available out there for migrate from WordPress to Laravel.

I ended up with my own solution.

The idea – use the WordPress REST API.

Of course, I need to prepare my database schema, but I left that part later. What’s important is to grab all the contents from my blog – posts, pages, comments, media, tags, categories and users(since i’m the only author, I don’t have to fetch the user).

Here what I did:

Create the service to call WordPress REST API.

Create an artisan command to fetch all contents.

Do register the artisan command in `Kernel.php`

Then create a `wp` directory in `storage` directory.

So, what I need to do, just call php artisan import:wp http://blog.nasrulhazim.com and I will get as following, store in storage/wp in JSON Pretty Format.

And that’s it for Part 1.

Next, I need to grab all the media, download it to my storage directory.

See you guys on next post.

Training: WordPress Theme Development

Mar
17

Training by Cleanique Coders Resources (002507773-X)

WordPress Theme Development is a course for an eye opener, how easy is to develop WordPress theme. Cleanique Coders will provide every essentials aspect for WordPress Theme Development.

In this course, we are going to LEARN how to develop WordPress theme using a WordPress skeleton and HTML theme.

And we are NOT going to customize WordPress ready-made theme

Our Trainer

Fauzan Rani

We have Fauzan Rani in the house, who has experience in WordPress theme development. He is well verse in following programming languages.

HTML/CSS/Bootstrap
jQuery/AJAX
PHP/MySQL
Wordpress, Magento
Joomla, Drupal, CodeIgniter, Laravel, AngularJS

He has involved in numbers of WordPress training:

SkillsMalaysia Invite
– Facilitator in training how to manage content for SkillsMalaysia website.
– Platform : WordPress
-Venue : CIAST, Shah Alam, Selangor

AgrobIS Mini Portal
-Trainer during AgrobIS website user training.
-Venue : MARDI, Serdang, Selangor

WordPress User Training
– 2 day training for Basic Web Development Using WordPress.As a sole trainer.Volunteer for Community.
-Time : July 21-22, 2014
-Venue : KKLW, Putrajaya, Selangor

WordPress For Non-Techies
– 1 day training for Basic Web Development Using WordPress.As a co-trainer.
– Community event organized by JomWeb and MaGIC
-Time : Nov 22, 2014
-Venue : MaGIC, Cyberjaya, Selangor

** He may show best of the best of this artworks during the class!

Course Details

Date: 26 & 27 March 2016

Venue: Hour Loft Cafe, Seksyen 7, Shah Alam – http://bit.ly/hour-loft-cafe

Fee: RM 300

Syllabus Details

1) Starting the WordPress theme
– Training Overview
– Template Hierarchy/Structure
– Setup a WordPress Folder
– Activating a WordPress Theme

2) CSS and JS Integration
– Adding a CSS,JS to header and footer
– Working with CSS and JS in WordPress themes

3) Header and Footers

4) Homepage
– Sliders
– Feature post

5) Page Templates
– single post
– list page
– wordpress loop
– custom page template
– shortcodes

6) Navigation
– Adding new Menu
– Reposition and update menu
– Remove menu

7) Custom Post Type
– add new CPT
– list page
– WP_Query function
– custom field
– single page
– permalink

8) Blog
– list/index page
– adding comments

9) Gallery
– using plugin
– add, update, delete album
– add, update, delete, import photos

10) Contact Form
– Add Form using plugin
– Add Maps
– Address

11) Search
– Search Form
– Search Result Page

12) Finishing a WP theme
– add widget areas
– adding shortcodes
– Option Page
– Adding Analytics to Your Website

13) Migrate WordPress Website to Live Server
– Configure the database
– Folder permission
– Security options

 

REGISTER NOW AT http://goo.gl/forms/CUljXIeEPy

WordPress: Handling Plugin Activation

Feb
27

A simple way to handle WordPress plugin activation.


function activate_background_slideshow() {
	// do something during activation
	$plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/background-slideshow/background-slideshow.php',false,false);
    
    // WordPress Version
    $version = $plugin_data['Version'];
    if ( version_compare( get_bloginfo( 'version' ), '3.1', '<' ) )
    {
      wp_die("You must use at least WordPress 4.0 to use this plugin!");
    }

    // Plugin Version
    if ( get_option( 'dk_op_version' ) === false )
    {
      // initial state
      add_option( 'dk_op_version', $version );
    }
    else if(get_option( 'dk_op_version') < $version)
    {
      update_option( 'dk_op_version', $version );
      // do update based on version
    }

    // Database version
    if( get_option('dk_op_db') === false ) {
      add_option('dk_op_db', $version);
     
    } else if( get_option('dk_op_db') != $version ) {
      // add more upgrade database if needed
      update_option( 'dk_op_db', $version );
    }
}

WordPress: ORM Model Class

Feb
17

Object Relational Mapping or ORM is something that WordPress is missing, but that doesn’t mean we can’t create an abstract Model that act similar to ORM behavior.

Here are the first version to implement the ORM in WordPres – far from completions.

<?php 

abstract class Model {

	static $table_name = '';

	public static function table() {
		global $wpdb;

		return $wpdb->prefix.static::$table_name;
	}
	
	public static function all($conditions = '') {

		if(!empty(self::table())) {
			global $wpdb;

			$sql = "SELECT * FROM ".self::table() . $conditions;

			return $wpdb->get_results($sql);
		}

		return false;
	}

	public function row($conditions = '') {

		if(!empty(self::table())) {
			global $wpdb;

			$sql = "SELECT * FROM ".self::table() . $conditions;

			return $wpdb->get_row($sql);
		}

		return false;
	}

	public function findById($id) {
		global $wpdb;

		$sql = "SELECT * FROM ".self::table() . " WHERE id = '".$id."'";

		return $wpdb->get_row($sql);
	}

	public static function find($type, $parameters) {
		if(empty($type)) {
			$type = 'all';
		}

		$conditions = isset($parameters['conditions']) ? (' WHERE ' . join(' AND ',$parameters['conditions'])) : null;

		if($type == 'all') {
			return self::all($conditions);
		} else if($type == 'row') {
			return self::row($conditions);
		}

		return false;
	}

	public static function create($data) {
		global $wpdb;

		if($wpdb->insert(self::table(),$data)) {
			$id = $wpdb->insert_id;

			$data = self::findById($id);

			return $data;	
		}

		return false;		
	}

	public static function update($data, $where, $format = null, $where_format = null) {
		global $wpdb;

		return $wpdb->update( self::table(), $data, $where, $format, $where_format );
	}

	public static function delete($data) {
		global $wpdb;

		return $wpdb->delete( self::table(), $data );	
	}
}

Here are the implementation:

<?php

// make sure to include the abstract class first before extending the Model Class

class State extends Model {
	static $table_name = 'states';
}

The usage:

<?php

// include the State class in your plugin first

$states = State::all(); // fetch all available states
$state = State::findById(1); // find state with id = 1
$deleted = State::delete(['id' => 1]); // delete state record with id = 1

More features to add in – to have columns properties, what to hide, what to show, column alias and many more. But this is my first attempt to make the WordPress Plugin Development much easier (an attempt to replicate any famous PHP framework such Laravel, CakePHP and so on. Prefer to have something like Eloquent ORM)

Training: WordPress for Beginners

Dec
31

Location: Medina Arabic, Seksyen 9, Bangi (Above Melaka Kitchen Restaurant)

Date: 10th January 2016

Available Seats: 28 seats

Brochure:

Training-WordPress-Basic-NHM-20151231

 

Registration closed!

InsyAllah before end of January 2016 will be another free training for ‘WordPress For Beginners’! Stay tuned!

WordPress: Using Bootstrap In WordPress Admin

Dec
31

First of all – you need LESS compiler – for Windows, I use winless – please download & install it.

Next, grab the Bootstrap ( I use version 3.3.4, newer version broken when compiling the LESS ).

Extract the Bootstrap to a folder and then create a LESS file called bootstrap.less and paste in the following code:

.bs-wrapper {
   @import (less) url('bootstrap.css');
}

Now open up winless, and add folder where your Bootstrap CSS directory, and then on file bootstrap.less output file, you may name it as wp-admin-bootstrap.css.

Winless - Choosing the output

Winless – Choosing the output

Next, you just need to use wp_enqueue_style() to include the generated CSS – wp-admin-bootstrap.css

WordPress: Customize Polylang Language Switcher

Dec
24

A default language switcher for Polylang it’s a bit out of date. Below are the customized Polylang Language Switcher.

Customized Polylang Language Switcher

Customized Polylang Language Switcher

Below are the codes used for the customization.

 


include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

// check if polylang exist & enabled
if ( is_plugin_active( 'polylang/polylang.php' ) ) {
  	//plugin is activated
	add_filter('pll_the_languages', 'my_dropdown', 10, 2);
	function my_dropdown($output, $args) {
	    $translations = pll_the_languages(array('raw'=>1));
	    $output = '';
	    $output .= '<div class="btn-group" role="group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      '.$args['title'].'
      <span class="caret"></span>
    </button>
    <ul class="dropdown-menu">';

	    foreach ($translations as $key => $value) {
	    	$output .= '<li><a href="'.$value['url'].'"><img src="'.$value['flag'].'" alt="'.$value['slug'].'"> ' .$value['name'].'</a></li>';
	    }

	    $output .= '</ul></div>';
	    return $output;
	}
} 

References:

  1. Check if a plugin is active or not
  2. Polylang Functions Reference

Y2016

Dec
18

Year 2016, InsyaAllah, next year to be a year of sharing knowledge of what I’ve learnt so far. Following are the trainings I would like to conduct:

1. Mobile Apps Development with jQuery Mobile
2. Mobile Apps Development with Intel XDK (incoming)
3. API Development with Slim Framework (incoming)
4. PHP Basic / Advanced
5. Web Development with CakePHP 3 (incoming)
6. Web Development with Laravel 5 (incoming)
7. Web Development with Yii Framework 2 (incoming)
8. WordPress for Developers – Beginner Level
9. WordPress Theme Development
10. WordPress Plugin Development

Those status incoming, it’s the one I’m preparing the syllabus & materials, summarizing the process, and so on. Hope more things can be share other than mentioned above such as the tools I’ve used along the process of the development – Sublime Text 3, SQLYog, Putty, Git and so on

WordPress: Theme Development

Dec
17

I’ve been learning WordPress Development recently – both plugins & themes. Until today got a chance to summarize the WordPress Theme Development.

You have two options when developing WordPress Theme – either from scratch or extend(using child theme) the existing theme. Below is the illustration that I have on choosing the theme development method.

WordPress Theme Development Options

WordPress Theme Development Options

I’ve added some other requirement during the development process including how you want to design your layout, icons, colors. These are essentials things that you need to know / have.

Hope these two options could speed up your development process and of course these options for developers and not for non-developers (which I suggest to buy a 3rd Party Plugin for drag-and-drop theme development).

And of course the text editor – my favourite is Sublime Text 3.

WordPress: Migration / Deployment

Dec
15

During my first time using WordPress, migrate from one domain to another, it’s a headache for me. Too many steps need to take care off.

Until recently, working in a new company (Rocket Web Sdn Bhd), learning new things using WordPress – hardening, migration, tips & tricks, paid & free plugins.

You may read the summary of deployment(to few servers) from my previous.

The point of this post to highlight the plugins required for deployment/migration.

Duplicator

Duplicator

Create a backup of your WordPress files and database. Duplicate and move an entire site from one location to another in a few steps.

Using Duplicator helps a lot with setting up dumping database and renaming the domain name. Save a lot of your time. You just need to create MySQL user, password and database manually. That’s all you need. I bet there’s much better plugin, but this one work fine for my current tasks.

Better Search and Replace

Better Search and Replace

A small plugin for running a search/replace on your WordPress database.

I use this plugin after the deployment – where I need to rename all the old domain name or any keyword I want to updates. You can select which tables you want to search the keyword and making update. This plugin provide `Run as Dry` – no changes being made unless you un-check this option.

So, these two plugins are useful and easy to use. Hopes will help others doing migration from one server to another.