Category Archives: WordPress

What actually are Hooks in WordPress?

So, you have heard a lot about Hook, haven’t you? There are hundreds of articles out there about hooks. What are hooks, how do they work, bla bla bla..

What actually are Hooks in WordPress?

Do you REALLY understand what actually are hooks? If your answer is ‘Yes’, well, you can ignore this post.
But, if your answer is ‘No’, then let me tell you about Hooks in a way that perhaps nobody uses.

Hooks are “places” to alter or add something in your WordPress!

Places?! Nobody defines Hooks as places, right?

Well. You have noticed that I said “alter or add something”. Meaning, Hooks are used to alter/modify something that is already there or to add something new. These 2 are the main goals of using Hooks. So, hooks are of 2 types-

  • Action Hook – To add something new
  • Filter Hook – To alter or modify something

Action Hook

As I mentioned before, Hooks are “places”. So, Action Hooks are places that allows you to add something at the location that it was declared! Let me give you an example.

Say, we are creating a theme. At the footer area of the theme, we have a navigation menu added.
Now, you are thinking, what if someone wants to add a copyright text just below the menu?

This is where Action Hook comes into the picture. We can allow anybody to add their own text (or an image, or anything else) by defining an Action Hook here.

So, we’ll write our code like this-

If you see carefully, just after where the code for navigation menu ends, we have defined a “place”. Anybody now can add his own text, image or anything else at this place from an external file. Which could be a plugin, or a child theme.

Wondering how to add custom text at that place?
Just write a function and “hook” that function at the predefined place.

You can even send an email, perform a database query etc, whatever you need, at that “place”!

Filter Hook

Unlike Action Hooks, Filter Hooks allow you to alter/modify an existing data. For example, in the theme we were talking about, we want to show today’s date in the header area. So, we have written our code like this-

It will print Today’s date is 2018-03-07.

Now, what if we want to allow others to modify this date, as we have kept it hardcoded?
Yes, you got it right. We’ll make our code hook-able!

It will still print the same, Today’s date is 2018-03-07.

This date is now hook-able from external sources. If another developer wants to alter this and show current dynamic date, he needs to write code like this-

How to remove items from +New admin menu?

This tutorial is actually taken from an answer I gave on StackOverflow to a question by sailingthoms

Sailingthoms’s question

I would like to limit the +New admin menu to only show the single sub menu Event (“Veranstaltung”). Basically the users are allowed to create other items as well but not from that +New menu.

My answer

To hide everything (menu and submenu)-

To hide specific menu/submenu item(s)-

So, the basic rule is-

Add a new menu-

Update an existing menu-

How to get menu ID-
The easiest way is to inspect element with Firebug and take the ID. See this screenshot-

Navigate to your desired menu item and get the string next to wp-admin-bar-

Generate activation or deactivation link of WordPress plugin

You often need an action link to activate or deactivate a plugin. How do you do that?
I have written a simple function that can help you achieve this.

How to use?
This function requires 2 parameters; first one is the plugin file name, and second one is action type.
Plugin file name is the base file resides in wp-content/plugins directory. If you have a plugin called My Super Plugin and the base file is wp-content/plugins/my-super-plugin/my-super-plugin.php, then the file name will be my-super-plugin/my-super-plugin.php

And second parameter is the action type, either activate or deactivate.

So, if you want to activate My Super Plugin, you have to write-

And to deactivate-

Edit Next Post – my 16th plugin published on the WP repository

As I’m a man of Open Source, I always try to give back to the community. I already had 15 plugins published in WordPress repository. And today, the 16th one just added in the list. Of course free and Open Source.

Edit Next Post, the plugin you’ll always need
Imagine when you have a lot of posts in your WordPress site and you are going to edit some of them. You’ll have to go to posts’ archive page repeatedly in wp-admin area and choose one to edit.

Isn’t is disgusting? What about an easy way to switch between posts in the edit screen?

Yes, that’s what I’m talking about. Just install Edit Next Post and you are done.

Edit Next Post

It’ll show a meta box at top-right of the edit screen that allows to switch between posts.  So-

  • You can switch between posts with a single click.
  • It works for posts, pages or custom post types.
  • You can search posts as well.

Liked it? Please give it a 5 star rating.

Add a ‘Scroll to Top’ button to your WordPress site

A ‘Scroll to Top’ button can enhance usability of your website. There are several plugins available out there that can help you achieve this. You just need to install the plugin and activate it.

Few days back, I’ve written a plugin that adds this feature to a WP site. Unlike other similar plugins, this one contains 80 of built-in icons that you can choose from.

Your own icon!
And of course, you can upload your own icon.

Position and other settings
But what about the icon position?
Yes, this plugin will allow you to choose the position of your icon. You can set the icon at left or right, padding from sides as well as time to scroll to top etc.

You can download it from the official repository of WordPress.

And here is a sample output-

Change WordPress password when ‘reset password’ is not working

Imagine a situation when you forgot the password of your admin account of your WordPress site, and ‘reset password’ is not functioning. Or even, you cannot remember the email you used for it. You cannot access the wp-admin area. Now what?

Well, there’s a solution. You can change the password from phpMyAdmin without knowing previous password.

So, what is phpMyAdmin and where do I find this?
Okay, phpMyAdmin is a database administration tool that comes with most of the hosting services. You can find this from your cPanel.

Well, now what?
Open phpMyAdmin and choose the database that your WordPress uses from the list at the left. Click on it.
Choose the database
If you do not know the database name, open wp-config.php from root directory of the WordPress installation, maybe with an FTP client like FileZilla or from your cPanel’s file manager. Then search for DB_NAME constant. It’ll look like this-

Okay. Now after clicking the database name in phpMyAdmin, it’ll expand database tables in the right side. Click on wp_users.
Click wp_users
This name may vary with any prefix instead of wp. It may be foo_users or bar_users or anything else.

Next, find your username that you want to change password of and click edit.
Find username and click edit
It’ll open an editor page.

See the screenshot carefully.
Set new password
1. Choose MD5 from Functions column and user_pass row.
2. Type your new password in Value column and user_pass row.
3. Click Go.

Bah! You are done with this struggle. Now go login with your new password and have fun.

Feel free to ask me, if you have any questions. I appreciate if your share this post.

How to stop WordPress from generating multiple image sizes

When you upload an image using the media uploader of your WordPress site, it creates multiple sizes of that image. By default, WordPress creates 4 additional sizes, but your theme and/or plugin(s) may create more sizes of that image. And it’s really a headache when you are considering your storage.

So, how to stop it?

There are 2 ways to stop WordPress from generating multiple image sizes-

1. With Coding: Set a filter to intermediate_image_sizes_advanced. It returns an array that contains the sizes. Make it return an empty array, and phew! You’re done.
Paste this into the functions.php file of your theme.

It’ll stop WP to create additional image sizes and then you’ll have only one copy of the image.

2. Without Coding: Install this plugin- Stop Generating Image Sizes and you are done.
Stop Generating Image Sizes
This plugin also allows you to choose which of the sizes you want to be prevented from creating.