Pages

Thursday, November 29, 2012

Logout and Redirect to a New URL

Since Wordpress 2.7, a new template tag has been introduced using which one could redirect a logged-in user to another URL. Why would one need such a function? Well, people want logged out users to still hang-around their site to check something that might interest them.

This is how you can use this template tag:

Step 1: Add this code in your functions.php


add_filter('allowed_redirect_hosts','logout_redirect');
function logout_redirect($link)
{
    $link[] = 'example.com';
    return $link;
}
Step 2: Add this code anywhere on your site, for example in a sidebar.php or header.php or footer.php:

<a href="<?php echo wp_logout_url( 'http://example.com' ); ?>" title="Logout">Logout</a>
That's it! You are done. Wait, I just forgot to caution you that the Step 2 has PHP in it. So it won't work if you are simply adding it to a HTML text widget in sidebar or if you are adding it to a page or post. You should necessarily add this to a php file, ideally sidebar.php would do.

In case you insist that you want to use it in a sidebar widget, then better install a plugin which allows PHP in sidebar widgets. 

Monday, November 26, 2012

How to count exact number of users when BuddyPress does it wrong

There have been instances where BuddyPress function does not count active members. It is possible because they use different tables for counting. If you want to count exact members of your wordpress site then you can by-pass buddypress function. I have written a few lines of code which you can use in your functions.php file.

///count users in wp_users table and display at various places
function my_bp_core_get_active_member_count(){
 global $wpdb;
$user_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->users;");
return $user_count;
}
add_filter( 'bp_core_get_active_member_count', 'my_bp_core_get_active_member_count');

What this code does is, whenever bp_core_get_active_member_count() function is triggered it routes to my custom function mentioned above and returns the users count which will be displayed across the entire site. Got it? No? then write to me!

Monday, November 19, 2012

Automatically assign a different theme randomly for every new site in Wordpress Multisite

If you are the Multisite Super Admin of your Wordpress installation, then you must have felt the need to automatically assign a different theme for every new site that gets registered.

Currently what is happening in Wordpress Multi Sites is the same default theme that you initially assign will be used by all sites - and to me that's quite boring, especially when the admins of those sites do not change the default sites. It can also be an equally boring experience if you have disabled the Themes option for your sites and thus every user gets same theme.

Here is a simple trick from your wordpress kid:

  •  Step 1: Open wp-config.php file 
    • I'm sure you would know how to open this file. For those who don't please go to control panel of your hosting service provider where you can find the wordpress installation files. You wil find wp-config.php right in the root directory of your site.
  • Step 2: Just before the line which says "*/ That's all, stop editing! Happy blogging. */" paste this following code:
    • $theme1 =  'surface'; //theme1 folder name
       $theme2 = 'colorway'; //theme2 folder name
       $theme3 = 'amdhas'; //theme3 folder name
       $theme4 = 'swedish-grays'; //theme4 folder name
      
       $array = array($theme1, $theme2, $theme3, $theme4);
       $random_theme = $array[rand(0, count($array) - 1)];
      
       define('WP_DEFAULT_THEME', $random_theme);
  • Note that your theme folder names should be included in the variables $theme1, $theme2 etc. They are not theme names, but they are the folder names containing your themes. You can get these folder names in the wp-content/themes folder
  • Step 3: Sit back, take rest because you're done
    • The above code does the trick of assigning a random theme for every new user. 
If you face any difficulties, you could simple comment here and I will try to respond. That's two cents from Wordpress Kid. Thanks for reading. 

Monday, November 5, 2012

How to unlock Wordpress Admin when Theme My Login locks you up

The other day, I got myself into trouble when I forgot my admin password (silly me!) Well, I am a wordpress kid so I deserve the right to commit such silly mistakes. I knew that it got locked because of the Theme-My-Login plugin. But I didnt know how to unlock it.

To unlock my admin login, I kept moving from pillar to post in search of a trick. Some said there was no solution and some dared to give a trick (like change the plugin name). Somehow none worked for me.

I thought I should do something myself and did these following steps and it worked. Here are the steps you may want to use in case you got locked up by Theme My Login plugin.


  • Step 1: Try to find your way straight to your database itself.
    • If you are using cpanel, then the click on PhpMyAdmin where you actually find your wordpress installed database and its related tables. 
  • Step 2: Try to find wp_usermeta table and open the table
    • Opening wp_usermeta table is quite easy in PhpMyAdmin. All you need to do is click on your database on the left panel and a list of tables will be presented. From this you can easily click on wp_usermeta table.
  • Step 3: Try to find the row having meta_key as "theme_my_login_security"
    • You can find this in the same table under meta_key column. If there are too many rows and you are finding it difficult to find this meta_key, then you can write a SQL query in the SQL Query option on the top menu. The query could be like this:
      • SELECT * 
        FROM `wp_usermeta` 
        WHERE `user_id` =1
        AND `meta_key` = 'theme_my_login_security'
        LIMIT 0 , 30
  • Step 4: Delete that row having theme_my_login_security as meta_key
    • Deleting is a fairly easy task on PhpMyAdmin. Simply click on Delete link corresponding to the row having theme_my_login_security as meta_key. 
  • Step 5: Take a deep breathe and Login from a new browser.
    • Success, isn't it? If not then send me all your brickbats along with the specific detail of your problem. This kid of wordpress can try to help you in such a case. 
Hope this trick is useful for you.