Hide or change name of Publish button for Custom Post Type

Lets assume you are creating a Custom Post Type for publishing events. You do not wish to show the publish button to your editors until all the event fields are properly filled and validated.

This can be achieved using below code

 

How to create wordpress options page

Creating WordPress options page in Dashboard for your own plugin is quite simple. Generally it is used to set global preferences for your plugin which you want to let the users customise as per their requirement.

We will create a settings screen by creating a class as that is the simplest way of creating your own options page for your custom plugin.

It can be done in 5 simple steps

  1. Declare Class for the settings Page
  2. Add the page in the settings menu
  3. Register your settings
  4. Create the backend options
  5. Finally generate the options page

Here is the entire code

Now we will discuss each function in detail

First of all we have named our class as someCustomPluginSettings

The constructor just calls the add_admin_menu function and initialises the settings registration

Below function creates the admin page.

Here we need to provide

  • Title of the settings page
  • Capability (this decides who will get access to this page). manage_options means editors and above can access this page.
  • Slug of the page

Below function registers the settings variables, description on the settings page, etc. The most important variable here is some_custom_plugin_settings. We will be using this variable to reference our stored data using the settings screen.

Below function creates the HTML objects to store our data. Please note that each field uses the same reference i.e.

some_custom_plugin_settings and stores the data in array i.e.

some_custom_plugin_settings[‘setting_1’], etc

Below function just renders the options page in a form

 

 

WordPress admin custom column sorting and filtering for custom post type

In WordPress Dashboard there are some default columns listed for any of your post types on the post listing page. We can sort our custom posts by Ascending or Descending order by just clicking these fields. However most of the times these fields are not enough.

E.g. if we create a custom post for books then we may need columns to sort or filter books by Year of publication, Author, Category, etc

So how do we create these extra columns for sorting and filtering

Assumptions

  1. Custom Post Type: book
  2. Year,  Author and Language information is stored in custom meta fields for each book

Se here are the steps

Step 1: Define the custom colums

Here we add two new columns to the admin panel of the custom post type book namely Publication Year and Author

Step 2: Fill Data to the columns

Here we are assigning data to the columns displayed in the custom post. Since the data is stored in the post meta we use get_post_meta function to get the data from the postmeta table.

Step 3: Define sortable colums

Here we are defining which columns are sortable. In our case we want all the columns to be sortable.

Step 4: Perform Sorting

Here we are using the request filterThis filter is applied to the query variables that are passed to the default main SQL query of the page. This filter adds additional parameters/filters to the query variable based on which the list of posts gets generated.

Until this step the additional columns are added to the custom post type listing page and they are sortable. They can be sorted by clicking the table heading as shown below

Additional columns

 

Step 5: Create Filters

This part of code can vary depending upon our configuration of custom fields and taxonomy. Main objective here is to create select boxes for the filters. Below approach shows how to create select boxes for data stored in custom meta fields. If the data is stored is categories then we may need to use wp_dropdown_categories function to list categories for the filter

Step 7: Perform Filtering

Here we are just altering the main query for the page by passing our own variables.

This will create filters as shown below

Custom Filter admin screen

How to create custom post type template files in wordpress

Any wordpress theme comes with a single post, page and archive template. None of these templates may be suitable for you if are creating your own custom post for event, products, etc.

In such cases we need to create our own templates

There are 2 ways to create custom post type template files in wordpress

Method 1: Create custom post type template files within your Child Theme folder

Suppose your custom post type is event. You would create below files in your child theme directory.

  • single-event.php
  • archive-event.php

Once the above files are created they will be automatically picked for your events single post and archive posts. So whatever your custom post type is, the template file names should be as named as below

  • single-{post-type}.php
  • archive-{post-type}.php

Refer WordPress Theme Handbook

Advantages:

It is very easy to create and modify your custom post type template files

Disadvantages:

  1. The template files are not within your plugin. So if you want to install the plugin on some different website you need to remember to get the template files from the theme directory as well.
  2. If later you do not want this plugin anymore then disabling the plugin is not sufficient. You need to remove the above template files from the themes directory.
  3. This method can get messy as later it becomes difficult to remember why certain templates were created (unless you properly document them).

 

Method 2: Create custom post type template files in your plugin folder

This is assuming you have created your custom post by creating a plugin for it. (Note: Ideally a custom post should be created using a plugin anyway due to various advantages of plugins).

In this method copy the single.php and archive.php files from your themes directory and paste them in your plugin directory. If you wish you can create a subfolder named as templates inside your plugin and paste this files in the templates subfolder.

Now add below code in your plugin file

Advantages

  1. All your code related to the custom post remains in a single directory. So it is much easier to maintain.
  2. You can move the plugin folder anywhere along with the templates
  3. Becomes easier to activate or deactivate a plugin

 

Plot multiple places on Google Maps – WordPress plugin

Plot events, photos, places, etc on Google Maps using a simple WordPress shortcode which is built using Google Maps API.

Everything below can be achieved easily with a simple plugin, XML file to provide the markers and a configurable shortcode

  • Plot Multiple Locations on a Map
  • Marker Clustering (markerclusterer)
  • Ability to click each marker to get more details i.e. through a Marker popup
  • Marker’s info popup details is customizable through shortcode and CSS
  • Ability to set initial zoom level

Step 1: Get an API Key for Google Maps API

First of all we need an API key for Google Maps. You can get an API key from Google Developer Console

Here are the steps to create an API Key

1. Visit Google Developer console Projects page and create a project as per your requirement

2. Go to API Manager and select the project created in above step

3. Enable Google Maps JavaScript API and Google Maps Embed API

4. Click the Credentials link

5. Click New Credentials and select API Key. On the options page select Browser Key

6. Add your domain name as shown in below screenshot

Screen Shot 2016-02-14 at 10.25.38 PM

7. Finally click the Create button which will generate the API Key

Step 2: Install the Google Maps plotter plugin

  1. Download the plugin files from WordPress plugins repository 
  2. Install and activate the plugin

Step 3: Generate XML file for the Google Map markers

Make sure you have the markers XML file ready.

XML markers file can also be dynamically created through php from your database or it could just be a static file in below format

Sample XML file for the markers

Read how to generate XML file dynamically – coming soon

Step 4: Display Google Map

Now just display the map on a page, post, custom post, widget, etc using below shortcode

Zoom => In above shortcode the initial zoom level is set to 2

html  => will display the marker popup text

Data attributes are separated with a bar (|)

Each data attribute has the data attribute matching to the XML file and the corresponding comma separated label

e.g. topic is the data attribute you wish to display while Topic is the label to it

Above html will display the popup as below after clicking the 1st Marker

Topic: Some Topic

Presenter: Mary

Date: 14-Feb-16

Telephone / Mobile:

Step 5: Stylize Google Maps Marker popup text

Now the last thing is to stylise the marker popup text

If you notice the source code for the marker. It is as below

So the label and the data associated with it can be easily styled using the IDs in your CSS

 

 

Replace wordpress search with Google Custom Search (CSE)

For better search results or due to integration with Adsense you many want to replace wordpress search with Google Custom Search (CSE)

Below are the steps to follow using a simple wordpress plugin. [Download Plugin]

1. Register you site on Google Custom Search by filling a simple form as shown below

Google Custom Search
2. After creating your CSE you will get a search engine ID as shown below. Make a note of this ID.

Screen Shot 2016-01-25 at 12.10.01 PM
3. Create a page to display the search results page and add [PW_ADD_GSEARCH_RESULTS] shortcode on this page.

4. Add the the URL of the search results page on the setting screen of the plugin.

Replace wordpress search with google custom search
Settings screen

5. Comment/Remove the code in searchform.php in your themes folder of probably header.php and add below code instead

This will create the search box.

 

 

 

 

How to change default RSS feed posts limit

By default RSS feed in WordPress shows 10 posts. This default count can be changed by adding below code in your functions.php file

In above code the limit is increased to show 20 posts

How to create extra widget/sidebar areas

Depending upon your theme you may have some predefined widget/sidebar areas like Left Sidebar, Right Sidebar.

If you want to create a new sidebar area let’s say for footer then you can add below code in your functions.php file

Replace THEME_NAME with the name of your theme

Finally add below code in the theme where the sidebar needs to appear

 

WordPress – remove emoji code introduced by 4.4

If you are on WordPress 4.4 or more you may have noticed the emoji related code in your source code.

If you do not need this code just add below lines in your theme’s functions.php

 

12