WordPress REST API v2 Examples

Here are a few examples on how to use Wordpress REST API v2 First of all Download and Install the plugin just like any other wordpress plugin Get list of posts [crayon-59c1002bc6937462122813/] Get list of pages [crayon-59c1002bc6943484856866/] Wordpress REST API for Custom Posts Prerequisite: REST API support needs to be added to custom posts while registering the custom post. Below parameters add the necessary support. For detailed instruction please refer Adding REST API support to Custom Post Types Suppose we have a Custom Post Type news, below parameters would add the necessary REST support to the Custom Post Type [crayon-59c1002bc6948254647052/] show_in_rest This allows the Custom post type to be accessed through the REST API rest_base => Optional Parameter This allows to change the REST API route. For e.g. if the custom post type is news. We can define a custom route to access the books post using rest_base parameter e.g. news_api rest_controller_class This is only required to be changed if you are using a custom namespace i.e. other than wp/v2 Once the necessary support is added as shown above the Custom...
Read More

Create a WordPress staging site through shell script

Creating a wordpress staging environment requires 4 things. Here we are assuming that the staging environment is on the same server. Requirements for creating a wordpress staging environement Clone Database - Each Time Copy the code - Only the wp-content folder Edit wp-config to point to the Staging Database (single site only) plus the Staging Domain (multisite) - Once Only Update the wp-options table with the Staging Domain (single site), Update the wp_blogs, wp_site, wp_options, wp_1_options, etc tables with the Staging Domain (multisite) - Each Time So how to to achive creating a wordpress staging environement with a single script? Here are the steps. Step 1: Create staging.sh and define variables Create a file named staging.sh inside a directory named staging (preferably outside your public_html folder) Define all the variables related to your production and staging database connection as shown below [crayon-59c1002bc75a0737220400/] Step 2: MySQL Dump of the Production database Add below code to staging.sh file to get MySQL Dump of the current production database [crayon-59c1002bc75aa990768614/] Step 3: Export the Database to the Staging database Again...
Read More

How to selectively dequeue a script on individual post

Lot of plugins. Not sure what to do?? All of a sudden you may realise that you already have quite a lot of plugins on your website and probably some of those plugins are slowing your site down. That's the point of time you may decide to review your plugins but at that point it is very difficult to imagine the impact of deactivating a certain plugin. Here is a simple trick we can use to review our plugins and make sure atleast our important pages do not break. Idea The idea is to try and dequeue or remove a script on a page by page basis or post by post basic. Using this approach we are not deactivating the plugin on the entire site but we are just deactivating or removing it on just a single post or page. This way we can study the impact of removing the plugin on our most important pages/posts just so that we are sure it won't break our site and...
Read More

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 Declare Class for the settings Page Add the page in the settings menu Register your settings Create the backend options Finally generate the options page Here is the entire code [crayon-59c1002bc7df5646283335/] 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 [crayon-59c1002bc7e00249243042/] 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 [crayon-59c1002bc7e05338306028/] Below function registers the settings variables, description on the settings...
Read More

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 Custom Post Type: book 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 [crayon-59c1002bc8517048905684/] 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 [crayon-59c1002bc8522152018189/] 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...
Read More
12