How to add an user into a BuddyPress group automatically when he joins in a WPMU membership

This is an useful snippet that will add your newly registered (via WPMU DEV Membership 2) member to corresponding BuddyPress group.

https://gist.github.com/bappi-d-great/28764839a18115a66a1d

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.

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 http://domain.com/my_cpts then we want to see exact same layout and same content of http://domain.com/my_cpts in http://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:

https://gist.github.com/bappi-d-great/f06ca7dbe37d860337fe

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!

Defer parsing of javascript to improve performance in wordpress

Nothing for introduction, hehe! A very small snippet which will help you to defer parsing all javascript files except jquery in wordpress. Here is the code:

https://gist.github.com/bappi-d-great/2a67ff307db672d4566b

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.

Why you shouldn’t use one click installer to install wordpress

Yes, you will fall at the mid-ocean, trust me!

Well, most of the bloggers just maintain a simple blog so usually they don’t need to worry. They just need to keep a regular backup. But if you are planning to run a business site in wordpress and if that site is your everything, never ever use one click installer to install wordpress! You’d be better off installing it manually which may take you a few minutes longer to install.

Here you can get an idea about manually installing wordpress:
http://codex.wordpress.org/Installing_WordPress
http://www.siteground.com/tutorials/wordpress/wordpress-installation.htm

If you want to install manually, download wordpress from here. Then you can upload this through ftp. You can use several ftp client such as – filezilla, coreftp, fireftp (a firefox add-on) etc. You can know about uses of filezilla here.

Why you shouldn’t?

  • Big WordPress Security Risks
  • Exporting and importing content with WordPress’s built in export tools
  • They use old scripts. Updated versions of WordPress or any script are not immediately released on those services. They install an outdated version of WordPress. It’s not a good start with outdated one.
  • They bypasses almost all settings used for installing
  • Sometimes it has issues while you want to upgrade.
  • Suppose first created database is wpdb1 and the next one is wpdb2 and so on. Malicious hackers know this is how they’re created and it gives them more ammo.
  • Created a file named fantversion.php (or something similar), which is common for all auto installers. This is a security risk if crackers know how to break into it.
  • The auto created database name and the database username is same in most cases.

So what if you have already installed?

  • First of all, keep a backup of whole site and the database.
  • Download latest wordpress from WP rep. (see above for link)
  • Delete wp_admin and wp_includes folder via ftp.
  • Extract the zip and upload only wp_admin and wp_includes folder via ftp.
  • Change (alter) the database name, db username and password.
  • Put the changed db name, db username and db password in wp-config.php.
  • Get new auth key, salt etc from https://api.wordpress.org/secret-key/1.1/salt
  • Copy and replace the keys in wp-config.php
  • Uninstall the plugins that come with default one-click installation

It should work.

WordPress is a 15min install when completely secured and optimized through htaccess. So it’s your decision what will be your route. I just showed you some examples, if you do a little research over google you will get lots of ideas about it.

Hope you will enjoy! :)

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:

https://gist.github.com/bappi-d-great/d778d16e8220b5309582

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

Hope you will enjoy! :)

Get current page url in php

In an application, sometimes we need to get the current page URL in php. I mean, the URL shown in the browser. Here is how you can do it.

First, we will declare and use the function as the following:

https://gist.github.com/bappi-d-great/bc4037a4896f0dd233e6

Hope you will like it :)

Reorder or rearrange buddypress menu items

Don’t like the order of BuddyPress menu items? Change it!

Use the following code and organize in your way – how you want to reorder the BuddyPress menu items.

https://gist.github.com/bappi-d-great/591be83f017a963659e1

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.

:)

Get All posts by tag in a network – wordpress multisite

If you want to show some selected posts as popular posts across the network, then this snippet is for you :) You can fetch posts by any tag that is available in your wordpress multisite network and show those to anywhere, maybe in the main site or in any subsite.

If you want to show posts from some selected subsites, you just need to pass those blog IDs as an array parameter or if you send an empty parameter it will fetch posts from all subsites in the network.

Here is the code:

https://gist.github.com/bappi-d-great/bb9ee958a4d297dd218d

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. And then use the function in anywhere :)

Show posts based on user preference in wordpress

This is an interesting snippet that will help you to set a preference for the users, so that they will choose their favorite categories and they will see posts from these categories only.

https://gist.github.com/bappi-d-great/88302389c26a60075787

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! :)

Remove joined group notification from sitewide activity

This is very little code snippet that will help you to hide some activity notification from the sitewide activity page. When someone joins in a group, we see a notification in sitewide activity page says, “Someone joined the group ABC”.

Screen Shot 2014-09-26 at 7.43.47 PM

This not that bad, but if your site is attacked by spam users, your activity feed will be flooded with spam notification. So why don’t just hide it? :) Here is the code snippet:

https://gist.github.com/bappi-d-great/da834c303f29273abdb3

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.

You can do the same for other notification type. If you don’t know the type, in the above code just use:
[php]
print_r($activities);
[/php]

You will get all the types that are coming in your activity field.

Enjoy! :)