Ncrypt : A 2-way encryption library for PHP

I have released Ncrypt, a 2-way encryption library for PHP. Using Ncrypt, you can encrypt a string and decrypt an encrypted string.

Steps

1. Include the class

1.a. Option 1 (using Composer)

  • Add the following to your composer.json file-
  • Run composer install command.
  • Include your autoloader file (if not already), like this-

1.b. Option 2

  • Copy class.ncrypt.php file from ncrypt/src/ directory to your project.
  • Include the class in your project file, like this-

2. Instantiate the class

Create an object of the class.

3. Configure (optional)

Optionally set secret key, secret IV and cipher

4.1. How to encrypt a plain text/string

Pass your string to encrypt() method-

4.2. How to decrypt an encrypted string

Pass the already encrypted string to decrypt() method-

Requirement (minimum)

  • PHP 5.3.0
  • php_openssl library needs to be enabled. See here

Link: https://github.com/mukto90/ncrypt

Disclaimer: This library is NOT guaranteed to be secure enough to use in your serious projects.

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-

How to handle error messages in PHP

Error messages are nightmares to a programmer. Default error messages are annoying and looking ugly. And kind of security threat as well. You probably wish to show a custom message when an error occurred. And tried and failed?

I had written this small code snippet for one of my projects and now wondering if this can help others. This function will triggered when a shutdown event happens.

Place this code at the top of your project file. Or maybe create a separate file and include where required.

This will help you handle messages for fatal error, undefined function error and required file not found error. And a general message for other errors.

So, when an undefined function is called, for example

It’ll show your defined message instead of showing-

You can change messages in line 27, 31, 35 and 39 with your own texts too.

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-
preview

A simple two-way function to encrypt or decrypt a string

Update: I have released Ncrypt, a library (OOP based) that facilitates the same (two-way encryption and decryption). See here

This function will provide you a two-way system to encrypt a string or decrypt an encrypted string.

So, how to encrypt a string?

Just call this function and pass your string. Additionally set your second parameter to 'e' (optional). To encrypt Hello World!, write-

It’ll generate a encrypted string RTlOMytOZStXdjdHbDZtamNDWFpGdz09

And, how to decrypt?
Pass your encrypted string to the function and set second parameter to 'd'.