Rename Publish button in WordPress admin

There are scenarios where you want the WordPress Admin to behave differently, specially for the custom post types. Imagine you have custom post type called ‘contacts’ and you want to change the button from PUBLISH to SAVE. So how to achieve this without changing the core files?

Add the following code to your functions.php:

Note: Make sure you make a copy of your function.php before placing the code. Just incase if you break something you can always bring it back.

Happy Coding ūüôā

WordPress Nested meta_query Using Multiple Relation OR & AND

WP_Meta_Query is a class defined in wp-includes/meta.php that generates the necessary SQL for meta-related queries. It greatly improved the possibility to query posts by custom fields. In the WP core, it’s used in the WP_Query and WP_User_Query classes.

meta_query is used by WP_Query while we have a situation like we have to search for something that is not native by wordpress and was added by custom code known as custom field.

So, when we have a simple search that contains only one meta key to search through the thing is quite easy like below,

After that we got a condition that we have to search through different meta keys having one single value, there comes the role of “meta_query”. For that we have a code as follows,

Finally we got a situation where we have multiple meta_key with more than one conditions. Suppose I am searching a lawyer with his Area of Law and location, and also location is divided in to three parts i.e. city, state and zip. Here I have to select one of the three meta keys like “city”, “state” & “zip” and also I have to compare it with “Area of Law”. Here is the situation where native functionality of WordPress fails to integrate the functionality. So by googling so much I found the following solution that uses both relation keys i.e. OR & AND in single meta_query. Here is the example,

While some say it works and some denies, in my case this query failed completely. In my case I used an array of keys for “key” and compared it with a sting value inserted for the “value”. But whatever I did, totally violets the rule for meta_query as type of key should be a string while I am putting an array. It only gave me a warning that invalid argument for “trim()” as I was providing an array instead of string. Otherwise it created the exact result that was expected. Here is the query,

More exact solution is much appreciated.

Make Checkboxes Act Like Radio Buttons Using jQuery

This tutorial shows you how to make Check boxes act like radio buttons, this really need when the designer designed a quiz page with check boxes instead of radio buttons and the client wants only in that way. Sad isn’t it?

But don’t worry here is the solution for that,
jQuery Part:

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… ūüôā

Simple Image Scroll Using Javascript Marquee

Simple image scroll using javascript marquee, only with the help of jQuery and marquee.js. Only need to add the javascripts and the scrolling div. This script will only output a div with a number of images scrolling inside it. You can add more functionality to it later as per your need.

Then you need to add the css for image container and the images,

That’s it. You have integrated your scroll. You can download the attached zip file which contains the html and js files here.

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.

Scroll To Specific Section Using jQuery

We can scroll to specific section using jQuery method .scrollTop(). .scrollTop() expects a parameter, that is , the pixel value where the scroll bar should scroll to.

is able to scroll the window ( if there is enough content in it ). You can also get desired value with offset() and position().

This will scroll the contact element to view. The non-jQuery alternate method is .scrollIntoView(). You can call that method on any DOM element like:

true indicates that the element is positioned at the top whereas false would place it on the bottom of the view. The nice thing with the jQuery method is, you can even use it with functions like .animate(). So you might smooth scroll something.

you can use a very lightweight approach by defining your custom scroll jquery function.

Scroll to a page using coordinates,

scrolling with window.scroll,

only to sum up, use the window.location.hash to jump to element with ID,

Happy Coding ūüôā

A Guide To WordPress Custom Post Types: Create, Display & Meta Boxes

Custom Post Types are a new set of administrative options appearing along with the default post types such as Posts, Pages, Attachments etc. A Custom Post Type can store any type of information. It has a dedicated editor, media uploader and uses the existing WordPress table structure for ease in data management. The main advantage of creating custom post types using the WordPress API is that it equips itself well with existing themes and templates. Custom Post Types are also SEO friendly because of their nifty permalinks.

Why Use Custom Post Types?

Custom Post Types help us to keep different types of posts in different buckets. It separates our regular posts from others. Simple enough!

What Actually Are Custom Post Types?

Suppose you want your blog to have a separate section for Portfolio. By using Custom Post Types you can create a new type of item like Posts and Pages, which will contain a different set of data. It will have a new administration menu, dedicated editing pages, custom taxonomies and many more utilities required for full fledged publishing.

Let’s Create Custom Post Type “Portfolio” For Your New Portfolio Page

Register Custom Function :-

Add the below function to your theme’s function.php. The following line of code is used to execute the custom function named¬†site_portfolio¬†during the initialization phase every time a page is generated.

add_action( ‘init’, ‘site_portfolio’ );

Implement The Custom Function :-

The register_post_type function does most of the work for us. As soon as it is called it prepares the WordPress environment for a new custom post type including the different sections in the admin. This function takes two arguments: the first one is an unique name of the custom post type and the second one an array demonstrating the properties of the new custom post type. Here it’s another array containing the different labels, which indicates the text strings to be displayed in the different sections of the custom post type e.g. name displays the custom post type name in the dashboard, edit and view are displayed in Edit and View buttons respectively. I think the rest are pretty self explanatory.

In the next arguments:

  • 'public' => true¬†determines the visibility of the custom post type both in the admin panel and front end.
  • 'menu_position' => 15¬†determines the menu position of the custom post type.
  • 'supports' => array( 'title', 'editor', 'comments', 'thumbnail' )¬†determines the features of the custom post type which is to be displayed.
  • 'taxonomies' => array( '' )¬†creates custom taxonomies. Here it‚Äôs not defined.
  • 'menu_icon' => get_bloginfo( 'template_url').'/images/portfolio.png'¬†¬†displays the admin menu icon.
  • 'has_archive' => true¬†enables archiving of the custom post type.

Icon for Custom Post Type :-

Save a 16√ó16 pixel icon image in your current plugin folder. This is required for the custom post type icon in the dashboard.

Creating Meta Box Fields for Custom Post Types

The meta box mechanism uses the help of the built in WordPress meta box system and helps to add fields required specifically for the custom post types, without requiring the default custom fields in the editor.

Registering the Custom Function :-

Open your theme’s function.php and add the following function.¬†This registers a function to be called when the WordPress admin interface is visited.

add_action( ‘admin_init’, ‘my_admin_portfolio’ );

Implement The Custom Function For Meta Box :-

Here add_meta_box is the function used to add meta boxes to custom post types. Explanation of the given attributes:

  • portfolio_meta_box¬†is the required HTML¬†id¬†attribute
  • Portfolio Details¬†is the text visible in the heading of the meta box section
  • display_portfolio_meta_box¬†is the callback which renders the contents of the meta box
  • portfolio¬†is the name of the custom post type where the meta box will be displayed
  • normal¬†defines the part of the page where the edit screen section should be shown
  • high¬†defines the priority within the context where the boxes should show

Implementation of the display_portfolio_meta_box Function :-

This code renders the contents of the meta box. Here we have used an object variable that contains the information of each of the portfolio displayed in the editor. Using this object we have retrieved the¬†post ID¬†and used that to query the database to get the associated ‘Description’, ‘Type’ and ‘URL’ which in turn render the fields on the screen. When a new entry is added then the¬†get_post_meta¬†returns an empty string which results in displaying empty fields in the meta box.

Registering A Save Post Function :-

add_action( ‘save_post’, ‘add_portfolio_detail_fields’, 10, 2 );

This function is called when posts get saved in the database.

Implementation Of The add_portfolio_detail_fields Function :-

This function is executed when posts are saved or deleted from the admin panel. Here after checking for the type of received post data, if it is a Custom Post Type then it checks again to see if the meta box elements have been assigned values and then finally stores the values in those fields.

Creating a Custom Template Dedicated to Custom Post Types

  • archive-{post_type}.php
  • single-{post_type}.php
archive-{post_type}.php
If your custom post type were ‘product’, WordPress would look for¬†archive-product.php¬†to display the archive of posts.
single-{post_type}.php
If your custom post type were ‘product’, WordPress would look for¬†single-product.php¬†to display the single or permalink of the post.

If these files are not available in your Theme’s directory WordPress will look for¬†archive.php¬†and¬†single.php, respectively. If even these files are not present it will default to¬†index.php.

Create PHP file named¬†archive-portfolio.php¬†and put the following code in it. It will display all the post having post type “portfolio”.

Now for displaying the detail of particular post create single-portfolio.php

Now create a page in admin named as “Portfolio” (same name as post_type) and save it. Now access your page by your domain name/portfolio.

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