Empty trash hook in wordpress

In some cases we may want to delete data related to certain post stored in some custom table (i.e. data which is currently not in wordpress). If the data related to a post is not in wordpress then it won't get cleaned once the post is deleted from the wordpress and you may end up garbage and unmanageable data in your custom table. e.g. we have a custom post to register attendees to our events. There is also a need to log if the user had attended previous events too and to maintain their preferences for each year/event. Since it is not possible to store such yearwise/eventwise data in wordpress through custom fields, we may end up creating our own custom table. The requirement is when the user gets deleted from the system, the data stored about the user in the custom table should also get deleted Wordpress provides a hook to clean such data stored in your custom table. [crayon-59c1038dd4261275558824/]    ...
Read More
How to create wordpress custom filter hook

How to create wordpress custom filter hook

This is just an example explaining how to create a wordpress custom filter hook Let's say you have a page or a plugin displaying posts/custom posts based on a certain criteria Now you wish to provide a hook to the end user to be able to modify how the posts get displayed. Here is a sample code to display posts using a standard wordpress loop [crayon-59c1038ddd88b653511387/] Note that we have displayed the  posts after getting the contents in a variable named $wpi_snippet_view. This helps us to define a hook to be able to modify this variable. Below code shows how to define our custom filter hook [crayon-59c1038ddd895264751584/] The only difference is the above code is line number 19 where we have defined the custom hook as wpi_snippet_view_hook Now that the custom hook is defined, all we need to do is to call the hook in our functions.php and modify the variable $wpi_snippet_view to show the required output. [crayon-59c1038ddd89a825745328/] Using the above hook we have changed the thumbnail size from thumbnail to...
Read More
Server load monitoring tools for your wordpress site

Server load monitoring tools for your wordpress site

Here are some basic tools which will allow you to monitor server load to keep your wordpress site optimised. Uptime (shell command) [crayon-59c1038dddd95010047195/] Above command is an example of the uptime command. It says the server is up since 364 days, 2 users are logged in and the rest of the numbers are showing the server average load. The three numbers show the load averages for the last minute, 5 minutes and 15 minutes. If you have 4 CPUs and the load is 2 then your server is using half the CPU capacity. If you have 2 CPUs and the load is 2 then your server CPU is running at full capacity. A load above the number of CPUs means that the system is overloaded which reduces performance. top (shell command) [crayon-59c1038dddda2427332245/] top command shows information like tasks, memory, cpu and swap. Here is a sample output of the top command. PHP sys_getloadavg function [crayon-59c1038dddda8918869271/] sys_getloadavg function returns an array. In above code, $load[0] would be the server load value. Based on the above code we...
Read More
How to prevent WordPress CSRF attack

How to prevent WordPress CSRF attack

Wordpress CSRF attack happens the same way as it happens on other sites. Wordpress provides some inbuilt tools to protect against CSRF. We will see how to make use of these tools while creating our own wordpress plugins. What is CSRF ? CSRF meansCross-Site Request Forgery (CSRF). It is a type of attack that occurs when a malicious web site, email, blog, instant message, or program causes a user's web browser to perform an unwanted action on a trusted site for which the user is currently authenticated. How does it happen ? For e.g. if you have a form on your website and you haven't protected it for CSRF attacks then a hacker can create a similar form elsewhere and trick one of your users to submit the form. This means the hacker can fill any values in the form. The damage depends on the functioning of the form. How to prevent CSRF In short, to prevent CSRF attack all we need to do is to check if...
Read More
Show custom field validation errors in WordPress Admin

Show custom field validation errors in WordPress Admin

If you are creating your own custom post type in wordpress and you use some custom fields to store data related to each post. For e.g. if you create a custom post for events then you would store data like event start date, end date, address, etc within custom fields. Unless all the all required custom fields are filled you do not want to publish the event and so you would want to warn the event editor about it. Wordpress admin_notices hook allows to achieve this very easily [crayon-59c1038dde3b0104875543/] admin_notices is the hook available to display the messages add_settings_error - Registers the setting error to be displayed to the user settings_errors - This function simply displays all the errors line by line...
Read More
Setup wordpress cron jobs and debug methods

Setup wordpress cron jobs and debug methods

Why would you need to setup Wordpress cron jobs ? Cron jobs in Wordpress can be set for following reasons You have a custom post type for events and you wish to archive all your old events at regular intervals Archive logs e.g. if you have some plugin which tracks user activity then you may want to archive the logs table at regular intervals If you have integrated wordpress with some external application e.g. a mailing server then you need to synchronise your mailing lists with the external server Clearing caches for certain pages How to setup wordpress cron jobs wp_schedule_event is the function used to set up wordpress cron jobs Here's a sample code to set up a cron job [crayon-59c1038dde81d885092457/] The above code will execute the function wpi_some_cron_job on daily basis. Some of the other options are hourly, twicedaily Now the question is where do we add the above code. The answer to this depends whether you want to set this up as part of your own...
Read More
How to tackle WordPress slow queries

How to tackle WordPress slow queries

Here are some wordpress slow queries i.e. queries which take more than 0.05s. It really depends on your wordpress site i.e. how big is the database, plugins and your site configuration. However if you are facing performance issues related to the Dashboard then it is more likely to be due to the slow wordpress dashboard queries. Query Monitor is good plugin to check/analyse your slow queries. Some Wordpress Slow queries Below query auto populates the custom fields drop down box. [crayon-59c1038ddeb29681028106/] For large tables this query can take lot of time like 2secs or so. If you do not need custom fields it is very easy to turn them off using below function. [crayon-59c1038ddeb31283169914/] For more information read this interesting post on CSS Tricks Below query runs on every Dashboard page so it is important that your wp_options table is optimised. [crayon-59c1038ddeb35859454576/] Depending upon the plugins you have installed, the wp_options table size can grow rapidly. Some plugins use this table to store _transient options. These _transient options are objects stored in cache....
Read More

How to apply Sendgrid categories to WordPress emails

What are Sendgrid Email Categories Creating categories for different kinds of emails sent through sendgrid is quite beneficial. It allows you to track emails based on each category i.e. it allows you to tag your emails by topics. e.g. it would be nice to know how many user registration emails actually were delivered, how many people requested password resets, etc For instance emails sent through wordpress can be categorised as User Registration Password Reset Newsletter, etc How to add Sendgrid categories As per the sendgrid documentation You can add categories to the X-SMTPAPI header of the emails you send via SendGrid. This will allow you to track emails based on your own categorization system. In case of wordpress emails here are the steps to follow Install the Wordpress Sengrid plugin: Since the version 1.6.9 this plugin allows to add categories in the email headers. add the category headers to the wp_mail function as shown below How to add category headers to the wp_mail function [crayon-59c1038ddf014836309102/] The above headers can be added to your custom plugins where you define your own wp_mail...
Read More

WordPress Custom Login page

Creating a Wordpress custom login page has 2 benefits Login page can be created based on your own theme The URL for the login page would be different to the Wordpress login URL. This is a good security practice provided you block the Wordpress default login page or redirect it to your custom login page. This does not mean that you need to create your own methods to store and retreive cookies by creating your own login function. Wordpress provides easy to use functions using which you can create own own plugin to create a wordpress custom login page. So here is the sample login form Wordpress custom login page - form [crayon-59c1038ddf25e228531848/] Here the form action submits to a page which does the authentication part. If you have created your own plugin the page would most likely be in your plugin folder. Here is the page which does the authentication and sets the cookie Wordpress custom login page - authentication [crayon-59c1038ddf266725910978/] wp_authenticate authenticates the user. If ok wp_set_auth_cookie sets the cookie for...
Read More

Adding a plugin textdomain / translation into wordpress

A plugin textdomain is required if you need to translate your own plugin in different langauges i.e. to internationalize the plugin. Here are the required steps Step 1: Decide the plugin textdomain name e.g. my_plugin_textdomain Step 2: Initialise the languages directory for the plugin textdomain Add below code to your plugin [crayon-59c1038ddf4ee861483115/] Create languages folder within your plugins directory. Step 3: Create PO file for the languages If you are creating a language translation for German then you would need to create a po file with below name my_plugin_textdomain-de_DE.po Download a sample PO file Open the file in a suitable text editor and add the necessary translations in the file Step 4: Create MO file Once all the translations are added to the PO file open the file in Poeditor and just save the file. Poeditor will automatically create corresponding mo file. Upload both the files on your plugin languages folder   For the translation to show up for the corresponding words or phrases __("sample text") is to be used within the plugin code....
Read More
12