post

Easiest but powerful encryption in PHP

For a secured system, most of the data is encrypted in server end and sent to database. And after fetching the data from database, just decrypt before showing in front end.

There are lots of procedure to encrypt the data, lots of encryption algorithm out there. But, here we will use a simple encryption method though it’s powerful 🙂

We are going to use mcrypt library of php for this method. You can install the library following the instruction below: (based on Ubuntu) Read More

post

How to use custom post type archive as front page

We can easily set any page as front page from Settings > Reading. It can be any static page. But what if we want to set an archive or CPT archive page as front page?

There are many ways to do so. We can create a custom page template, and write our code to get the posts for the post type (will look like an archive). Then create a wordpress page using that page template and select that page as front page from Settings > Reading.

This is a little complex if you are not a developer. Or even if you use a custom theme. Let’s do it in another way.

What you need is to have a file inside your theme for archive – archive.php. For CPT the file name will be archive-{CPT}.php. We will use the same file to make our home page.

For example, if your CPT archive url is domain.com/my_cpts then we want to see exact same layout and same content of domain.com/my_cpts in domain.com/.

What I love to do in this case is to use a very simple snippet. Where I just need to set the CPT name, that’s it. Here is the code:

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

Note that, here CPT means Custom Post Type

Enjoy!

post

How to convert hexadecimal color value to RGB value in php

Most of the CMS provides a color picker in admin panel to user to choose a color. And almost all of the color pickers return the color code as hexadecimal mode, e.g. #4EE4D3. So, you can just get the value and set the color in css. For example:
[php]

body{
background:
}

[/php]

This is a very good practice. Problem occurs if you want to put a opacity option to the user. You can set background opacity with hexadecimal color value. You must use RGBA filter, so RGB color mode as well. Like:
[css]
body{
background: rgba(233, 122, 234, 0.7);
}
[/css]

So, we need to convert the hexadecimal value to its equivalent rgb value. Here is a very small function that will do the job:

So using this function you will get the rgb value, and with that you use the opacity :)

Hope you will enjoy! :)

post

How to write dynamic css in a php file in wordpress

It’s always standard to maintain a separate css file for styling, even for dynamic css that is generated by the theme options. Most of the theme developers include this dynamic styles into the header or footer using wp_head or wp_footer hook. How’s about having a totally separate css file for the dynamic styles? :) It’s pretty simple so let’s start!

In my example, I have used redux framework, so that I have a global variable. In my case, I assume my global variable is $lts. So, in functions.php you need to add like this:

[php]
// Adding custom script
add_action( ‘wp_enqueue_scripts’, ‘theme_custom_style_script’, 11 );
function theme_custom_style_script() {
wp_enqueue_style( ‘dynamic-css’, admin_url(‘admin-ajax.php’).’?action=dynamic_css’, ”, VERSION);
}

// Ajax handler for wordpress
add_action(‘wp_ajax_dynamic_css’, ‘dynamic_css’);
add_action(‘wp_ajax_nopriv_dynamic_css’, ‘dynamic_css’);
function dynamic_css() {
// Assuming the css file is in /wp-content/themes/THEME_NAME/assets/css/ directory
require( get_template_directory().’/assets/css/custom.css.php’ );
exit;
}
[/php]

Then in custom.css.php file add this:
[php]

body{
/* You can use your theme option variable here, just declare it as global variable first */
color: ;
}
[/php]

Gist code:

Hope it helps! :)

How to load a wordpress plugin at very last

You need to load your plugin at vert last? Well, sometimes we develop a simple plugin that is dependent to other plugins, something like add-on. So, in those cases, the addon plugins need to be loaded after the parent plugin. Here is a small snippet that need to put at the addon plugin to make sure that the plugin will be load at the last of all plugins.

Here you go:

Enjoy!

post

Limit one blog per user in a wordpress multisite

In a wordpress multisite, an user can create as many blog as he wants. If you Pro Sites you can control this. Pro Sites is a giant plugin that will help you to have control over the whole network.

But if you just want to limit one blog per user in a wordpress multisite and don’t want to use a huge plugin like Pro Sites, then this small snippet should help you.

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

Enjoy!

Easily display member cards with WPMU DEV Membership

Here’s a simple shortcode snippet to let you easily add a membership card for your site’s members when using WPMU DEV’s Membership plugin. This was requested in the WPMU DEV forums, where we love to help members out with simple and useful custom snippets. :)

WPMU DEV Forum topic: Display Membership Data Print/Mobile

The snippet adds a [mem_card] shortcode that will then show some member details.

The snippet can easily be added to your theme’s functions.php file, using the downloadable file available in that forum thread or using a plugin like Code Snippets.

How to enable php mail() in your ubuntu

In my last post I showed you how you can set up an ubuntu server for apache, php, mysql, phpmyadmin and wordpress multiste. Below is the link if you want to take a look:

How to setup a VPS (Ubuntu) for WordPress hosting – A to Z tutorial

Today, we will learn how we can enable email in that server, so that we can use php mail() to send mail from our system. Today I won’t go in that detail but just to set a simple mail server.

The simplest way to enable mail in your ubuntu is to run the following command:

[html]
sudo apt-get install sendmail
[/html]

It will install a copy of sendmail that listens locally. If you install that, then php can use the built in mail() function. There also won’t be a risk of becoming a remote relay if you install like this as well. If you plan on sending mail out to other internet domains, you will need to make sure the IP address of the machine sending mail has a reverse DNS entry (PTR record) set up as well. Most large ISP’s will reject your mail if your IP does not have a PTR record or one that points to a generic host (e.g. 1.2.3.3.domain.isp.com). It should reverse to something like mail.yourdomain.com

In other case, also I prefer to use postfix. It is also easy to install. To install run this command:

[html]
sudo apt-get install postfix
[/html]

During the installation a dialogue box will be appeared where you need to select “Internet Site” and the next box give your domain name as system domain.

Now to make it fully functional, we need to edit configuration file. Open the configuration file:

[html]
sudo nano /etc/postfix/main.cf
[/html]

Make the following changes:

[html]
myhostname = yourdomain.com
virtual_alias_maps = hash:/etc/postfix/virtual (you need to replace the line which has alias_maps)
mydestination = localdomain, localhost, localhost.localdomain, localhost, yourdomain.com
relayhost = (can be blank by default)
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
[/html]
Press ‘cntrl x’ to exit the file and then ‘Y’ to save the file.

Reload the configuration:

[html]
sudo /etc/init.d/postfix reload
[/html]

Also, you can add users to the alias file. Open up the the alias database:

[html]
sudo nano /etc/postfix/virtual
[/html]

Enter the names and emails of the user like:

[html]
system@domain.com user1
support@example.com user2
[/html]
Press ‘cntrl x’ to exit the file and then ‘Y’ to save the file.

Once you are finished, save, exit, and run the following command to enable:

[html]
postmap /etc/postfix/virtual
[/html]

Again restart the postfix:

[html]
sudo /etc/init.d/postfix reload
[/html]

Now we need to test. Create a file in your server root, called – mail.php, for example. Add the following code:

[php]
<?php
$headers = ‘From: yourmail@gmail.com’ . “rn” .
‘Reply-To: yourmail@gmail.com’ . “rn” .
‘X-Mailer: PHP/’ . phpversion();
if(mail(“targetMail@gmail.com”, “Test Email”, “Email is set up”, $headers)) echo “Correct”;
else echo “Wrong”;
[/php]

And run the file by yourdomain.com/mail.php

If it prints “Correct”, then email is setup and check the inbox :)

You also can try sending email from your terminal. Run the following command:

[html]
sendmail targetMail@domain.com
[/html]

Then write the email contents and press Ctrl + D to send email.

Now, play with emails in your server :)

post

How to hide a plugin for non-admin users in wordpress

Today I will show how you can hide a plugin from the users that are non-admin. If you use a multisite, you can control a lots of stuffs with Pro Sites plugin. But what if you don’t want to use Pro Sites?

This simple scripts will do the job for you.

[php]

function custom_plugins( $plugins ){

if ( !is_super_admin() ) {
// Add comma separated paths of plugins that you want to hide
$hide_plugins = array(‘pluginFolder/pluginFile.php’, ‘pluginFile.php’);
foreach ($hide_plugins as $hide_plugin) {
unset($plugins[$hide_plugin]);
}
}
return $plugins;
}
add_filter(‘all_plugins’, ‘custom_plugins’);
[/php]

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag <?php at the beginning of the code.

I have shown option for both – 1. if the plugin file is inside a folder 2. if the plugin file is in plugin root folder.

Just add more items in the array to hide :)