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

Difference between wp_print_scripts, wp_enqueue_scripts and wp_register_scripts

I had lot of confusion earlier while enqueuing or dequeuing a script in wordpress. I was not sure which function to use and would just keep on using various functions on a trail and error basis. Hope below points would help to clarify atleast a few things with regards to when we should use a particular function wp_register_scripts This just registers the script but does not actually queue it for execution wp_enqueue_scripts This is used to enqueue a script which is required to be executed on our template. A script can be enqueued without registering it i.e. without using wp_register_scripts. wp_print_scripts This is an action which prints/outputs/shows up the scripts on our template for execution When to use wp_register_script You may be wondering why would I use wp_register_scripts and why not just directly use wp_enqueue_scripts The answer to that is if you are developing a custom plugin and wish to execute a script only on certain pages then it helps to just register the script and then programatically enqueue it on the required pages.  ...
Read More

How to add custom checkout fields to WooCommerce order

Follow below steps to add custom checkout fields to WooCommerce. Scenario: We want to create a new address field named as territory. It will be a drop down field and will be used to decide the shipping cost. Create the new field/s on the checkout page [crayon-5996a81e85efb635621931/] To create a similar field in shipping address, $fields array would need to be duplicated mentioning ['shipping'] instead of ['billing'] Now let us understand the parameters used in the above code Hook: Creating a new field uses woocommerce_checkout_fields hook type: Since this an drop down field the type of the field is select. If you want a mobile number field it could be a text field required: specify whether the field is compulsory or not. In our case we are calculating the shipping cost based on this field so it is compulsory. class:  form-row-wide: The new field will occupy the entire div width for the billing section form-row-first: The new field will appear in the first half area of the row (like the first name field) form-row-last:...
Read More

WordPress plugin – Custom Admin columns for sorting and filtering

This plugin adds the capability to add custom columns to any post type to provide sorting and filtering of posts using those columns. Download the plugin Download the plugin from the wordpress plugin repository Installation To install the plugin follow below steps Upload the plugin files in wp-content/plugins folder (same as any other wordpress plugin) Activate the plugin Configuration Open the settings page "Custom Admin Columns for Sorting and Filtering" As shown in the below screenshot add the extra fields and labels in comma separated format to the required post type Custom columns will then appear on the respective post listing  ...
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-5996a81e86382906453292/] 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-5996a81e8638c117724091/] 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-5996a81e86390418951485/] 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-5996a81e867a8458429194/] 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-5996a81e867b0914517287/] 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

How to correctly get visitor/client IP address

For security reasons and to prevent spam it is always recommended to log visitor/client IP address in your database atleast for important transactions like posting a comment, registration, etc. However it is important that we store the real IP of the visitor. Visitor/Client is not using proxy In almost all cases i.e. when the visitor/client is not behind the proxy we can get the real IP address of the visitor/client using [crayon-5996a81e86b4c754311604/] Visitor/Client is behind a proxy server In some cases the visitor/client could be behind a proxy server. In that case we can get the real IP using [crayon-5996a81e86b54342620962/] However using some tools one can easily pretend to be behind a proxy server. In that case we cannot get the real IP using above method Also we cannot be sure if the visitor/client is using a proxy or not. So it is best to store both the values in different fields in your database....
Read More

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: 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...
Read More
12