How To Add Custom Rewrite Rules In WordPress

WordPress rewrite API used to convert URLs from something programmatically convenient to something user and search engine friendly. This article will give you some background information about WordPress URL rewriting principles and API.

What Is Rewriting?

URL rewriting is when you change what content is displayed by the server, normally when you type in a URL the web server will simply search for any files in that location. But with rewriting you can still show the same URL but in the back-end change where the server will look for the content. Therefore we can have a URL of /website-1 but tell the server to display content that is located in /website-1/content/index.php

How WordPress Uses Rewrite For Custom URLs?

When you go to Settings -> Permalinks, you will need to select how you want your URLs to appear on WordPress. You have many different options to choose from the default WordPress will use is ?p={post_id}, this is the value that is stored in the wp_posts table and what the rewrite API will write to, when it wants to display the post data.

When you select a new permalink structure WordPress will store these rules in the wp_options table so it will know how to search the post table for the correct post. This means that we can have URLs with just the post title in them and WordPress will know that it needs to search on the post_name column to find the correct post to display.

WordPress Rewrite API

Imagine you need to integrate custom page in which you can list your products. How can we do this?..First we need to create a new rewrite rule so wordpress can translate example.com/products/2/edit to example.com/index.php?pagename=products&product_id=2&action=edit. We can use add_rewrite_rule() function which accepts 3 parameters. The first parameter is the URL pattern. The second parameter is the URL replacement and the third parameter is the priority. Let’s explore how to create this rule.

Catching the query variables

Using with plugin Activation and Deactivation

Copy Blog 🙂

Disable Update WordPress nags with Plugin’s and Theme’s Update Notifications

1. To Disable Update WordPress nag :

Insert the following code to the functions.php file of your active theme. It will remove the WordPress update nag e.g. WordPress 3.9.1 is available! Please update now, from the all users dashboard, admin dashboard & from Updates page as well.

2. To Disable Plugin Update Notifications :

Insert the following code to the functions.php file of your active theme. It will remove the update notifications of all the installed plugins.

3. To Disable all the Nags & Notifications :

Insert the following code the functions.php file of your active theme. This code disables all the updates notifications regarding plugins, themes & WordPress completely.

How To Install LAMP on Ubuntu

About LAMP

LAMP stack is a group of open source software used to get web servers up and running. The acronym stands for Linux, Apache, MySQL, and PHP. Since the virtual private server is already running Ubuntu, the linux part is taken care of. Here is how to install the rest.

First things first: update

LAMP Stack (Apache, Mysql, PHP)

This will install the LAMP stack in one command

Apache

MySQL

PHP 5

Want a more recent version of PHP 5 for Ubuntu? Then use the PPA for PHP5 offered by Ondřej Surý. PHP 5.4+

PHP 5.5+ note: there are significant differences between PHP 5.4. and PHP 5.5!

PHP 7.0 (prior to Ubuntu 16.04)

NGINX

NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.

NGINX doesn’t start on its own, so:

Learn about configuring NGINX as a front-end proxy with Apache

phpMyAdmin

phpMyAdmin allows you to manage your MySQL Database via web browser.

Choose Apache and then YES for dbconfig-common. If you ever need to edit phpMyAdmin config:

Webmin

Webmin is an open-source server management tool much like cPanel.

Scroll to the bottom and paste the following lines then save:

Import the key

Update the sources list

Run the install

Start Webmin

When it’s finished, open Firefox or Chrome and type:

If you’ve installed a fresh copy of Ubuntu (or if you don’t know your password) you can set a new one:

You’ll be prompted to enter your new password twice. Now, you can login to Webmin.

ProFTPd

ProFTPd is a high-performance FTP server.

(I always select “standalone”) Turn on Passive FTP via Webmin: Servers –> ProFTPD server –> Virtual Servers –> Default Server –> Networking Options

 Sendmail

PostFix Mail

Select “Internet Site” and then enter the domain name you want the Reverse DNS entry to be. BTW: To avoid your server being blacklisted, get a reverse DNS entry!

Alternative PHP Cache (APC)

APC is a PHP opcode cacher and works by caching PHP objects, functions, and database queries into your server’s RAM. If you run a WordPress website – then it takes full advantage of APC out-of-the-box. See my post on The Perfect APC Configuration Note, APC is no longer available in PHP 5.5+ as it’s now called OPCACHE.

By default, Ubuntu will install this from a repository which has an outdated version. To install the latest version of APC:

Uninstall APC

Memcached

Memcached is a high-performance, distributed memory object caching system. However, it can work together with multiple servers (unlike APC).

Check to see if Memcached is running

Fail2Ban

Fail2Ban scans log files (e.g. /var/log/apache/error_log) and automatically bans IPs that show malicious signs for exploits.

RSYNC

RSYNC is a open source utility that provides fast incremental file transfer.

Server-to-server transfers with RSYNC

ImageMagick

ImageMagick is a software suite to create, edit, compose, or convert bitmap images.

Icecast2

Icecast is a streaming audio server. If you ever wanted to have your own web radio station (like Shoutcast) this is the software.

Configure Icecast2. Mainly, setting up your passwords and default port.

Enable init.d script. Scroll to the bottom and change enable=true

Start icecast2

If you left the default port as 8000 then you can view your Icecast2 Server

Munin

Munin is a networked resource monitoring tool that can help analyze resource trends and “what just happened to kill our performance?” problems.

Now, this is a single server setup, so let’s install munin and munin-node

Configure Munin:

The first thing you should see is the operating directories. We need to change one of them:

to

Now let’s edit apache.conf

Delete everything inside apache.conf  and just add:

Move the web files to /var/www/munin

Set permissions

Restart Munin

Finally, restart Apache

http://your-server.com/munin

Cacti

Cacti graphical server monitor provides a fast poller, advanced graph templating, multiple data acquisition methods, and user management features out of the box.

Choose YES for dbconfig-common and Apache2. When finished you need to configure:

Default user & pass: admin / admin Remove cacti

BMON

bmon is a bandwidth monitor capable of retrieving statistics from various input modules.

When it’s finished installing:

Zip and Unzip

In my experience ZIP is great for creating archives for sharing via email or ftp. It’s a universal format that almost everyone can open. I would NOT use ZIP for file backups. For large backups, see 7ZIP or TAR below.

Zip up a folder:

Unzip (extract) an archive:

7ZIP

7ZIP is a very popular archiving program with excellent compression. Plus, it’s open source and supports multiple operating systems.

Create an archive

Extract an archive

TAR (Tape Archive)

TAR –  is the prefered way to handle file backups. I’ve read, the maximum allowed file size only depends on your hard drive. A disk formatted with FAT32 for example, only allows 2GB. You can also compress TAR using GZIP or BZ2.

GZIP – good compression, is very fast. Note: .tar.gz and .tgz are the same:

BZ2 – excellent compression, but slower. I find BZ2 works best if you’re archiving a smaller directory. Note: .tar.bz2 and .tbz are the same:

Untar (extract) an archive and if tarball already contains a directory name, strip it:

If you want to tarball the directory you’re currently in, with say, gzip:

Other handy commands:

View all running services

Restart PHP 7.0

Move files from one directory to another

Copy files from one directory to another

Set the server timezone

Add a user to the list of sudoers (you have to be logged in as root, or now the sudo password)

Download files

Server-to-server transfers with SCP

Server-to-server transfer with SCP into the current directory (Read more about SCP)

List size of directories

Set a password

Edit PHP.ini

Restart Apache

Set Recursive Permissions for your websites directory

Block IP address using IPTABLES

Single IP

IP Range

You can also manage IPTABLES (e.g., the linux firewall) via Webmin under Networking -> Linux Firewall

Manage packages

Remove LAMP

Happy Coding 🙂

Remove WordPress SEO columns from post table

The WordPress SEO plugin by Yoast is very popular, and for a good reason. It uses solid methodology, and is easy enough for folks to use who may not have a solid background in SEO.

That being said, there is a *lot* of data to be displayed. On the post editor, it’s not a big deal, as it’s contained in a metabox and can be minimized easily. But on the post table, it can crowd out other information, especially if you have other data being displayed in the post column. Below is a function that will remove the columns from the post table.

This same function can be used on any post type, including pages and other custom post types. Simply change the manage_edit-post_columns to manage_edit-YOUR_POST_TYPE_columns

That’s it. The great thing about this function is that you can use it to remove other columns as well, for example the author column (if you’re the only writer on the site).

*UPDATE*  You can also uncheck the boxes on the post table under the ‘screen options’ tab. This will only apply to the current user, however.

Importing Excel files into MySQL with PHP

If you have Excel files that need to be imported to MySQL using PHP, here are the steps. You need only the class file of PHPExcelReader to read the excel file, rest of the things you can do using simple functionalists of PHP. The PHPExcelReader class file is attached to the end of this document.

After including the class file to the required PHP file where the upload script will be written, you only need to initiate the class , set a upload folder and call the required function, that’s it.

Then simply initiate the class,

$data = new Spreadsheet_Excel_Reader();

Rest of the script you can get from the attached document. Importing Excel files into MySQL with PHP is a easy task. Click here to download the whole script. Happy Coding… 🙂

Installing ImageMagick for PHP on Ubuntu

iMagick is a wrapper class, wrapping the most common web image manipulation actions in easy to use functions, but allowing full access to iMagick’s power by issuing system calls to it’s command-line programs. There is no proper documentation for Installing ImageMagick for PHP on Ubuntu or any other LINUX based systems. Here is some simple steps for enabling ImageMagick library to work with PHP. Also there is no need to worry for MagickWand. iMagick will do everything for you.

First of all remove everything you have tried to make it work. Uninstall iMagick with the following command:

sudo apt-get remove imagemagick

Remove ImageMagick and all its dependencies with the following command:

sudo apt-get remove –auto-remove imagemagick

Purge the config file of ImageMagick with the following command:

sudo apt-get purge imagemagick

Or similarly, like this:

sudo apt-get purge –auto-remove imagemagick

Well, all your previous installation files are just gone. Now Install it purely and perfectly without any conflict or file modification. Enter the following two commands:

sudo apt-get install imagemagick
sudo apt-get install php5-imagick

That was way too easy – thanks interwebs! Remember to restart/reload your webserver..

sudo service apache2 graceful

That’s all. Check your php.ini or test with any simple script. For simple scripts to test iMagick you can refer to this post.

How To Get Current Page URL In PHP

Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your visitors submit a blog post to Digg you need to get that same exact URL. There are plenty of other reasons as well. Here is how you can get current url in php.

Following function will do everything for you,

You can now get the current page URL using the line:

Sometimes it is needed to get the page name only. The following example shows how to do it:

Happy Coding .. 🙂

Display Unsupported Image Formats In Browser

Most of the browsers do not support most of the image file types rather than some common file types like JPG, PNG and GIF. But in most of the web projects user need to upload some most used file types like PSD, PDF ( Unsupported image formats ) etc. and they need to see what they have uploaded is perfect or not.

So here are some methods using PHP and ImageMagick module for PHP to disply a little more formats in browsers by converting them into JPG format.

Displaying the JPG file format image in browsers :

Displaying the PNG file format image in browsers :

Displaying the GIF file format image in browsers :

The functions used above imagecreatefromjpeg(), imagecreatefrompng(), imagecreatefromgif(), imagejpeg() for reading .jpg, .png and .gif files are library functions of PHP. So so need not to bother about how to make them work.

Displaying the PSD file format image in browsers :

Here the function used i.e. imagecreatefrompsd() is not predefined library function. So in order to make it work we need to include an extra class file which will read the PSD files and gice the browser a JPEG file as output. You can download the classPhpPsdReader.php here.

Displaying the TIFF file format image in browsers :

Displaying the PDF file format image in browsers :

You need to call he wrapper class of ImageMagick inorder to make it work of pdf and tiff files. Follow the installtion guide of ImageMagick Here.

Export MySQL To Excel Using PHP

Inserting data to your database and getting all the data when required is a much needed operation in websites maintenance or development. So making all these operations easy is really needy.

I am presenting you a PHP script to export mysql to excel sheet and download. Script is orginally written by Oliver Schwarz <oliver.schwarz@vaicon.de>

Following are the script to call the class file and produce the output. Just download the php file and class file and follow the following steps,

1 : Create Download Link

<a href=”export.php”>Export To Excel</a>

2 : Include the .class file to “php-excel.php”

// load library
require ‘php-excel.class.php’;

3 : Include db file for database connection

// load your db file for databse connection
require ‘php-db-connect.php’;

4 : Set your column names for excel file

5 : Create loop to fetch data and write to excel file

6 : Create Excel File & Export MySQL to Excel

Its so easy and handy to use. Download Export-To-Excel.zip