Show Different Number Of Posts Per Category In WordPress

WPRecipes has posted a way to set different number of posts for a WordPress category. It works nicely, but only for one category. And it uses custom query, which is not very familiar to newbies. In this post, we’ll do the same thing more simpler.

Assume that we want to show 5 posts per page for category “WordPress” and 10 posts per page for category “song”, just open the functions.php file in your theme folder and insert these line into it:

1.  add_action('pre_get_posts', 'diff_post_count_per_cat');
2.   
3.  function diff_post_count_per_cat() {
4.      if (is_admin()) return;
5.   
6.      $cat = get_query_var('category_name');
7.      switch ($cat) {
8.          case 'wordpress':
9.              set_query_var('posts_per_page', 5);
10.            break;
11.        case 'wordpress/song':
12.            set_query_var('posts_per_page', 2);
13.            break;   
14.    }
15.}

Change “wordpress” and “song” into real category names of your blog. Note that in the code above, we use category slug, not original name. If the category is a child category, don’t forget to insert full path (i.e. “wordpress/song” in the example below).


Change WordPress Default Email From Name And Address

By default, WordPress uses the from name “WordPress” and address “wordpress@yourdomain.com” when it sends notifications to users. It isn’t convenient and you might want to change it into your Blog name or something like that. This tutorial will show you how to do this just with some simple code.

Open the functions.php file of your theme and paste the following code into it:
  1. add_filter('wp_mail_from', 'new_mail_from');
  2. add_filter('wp_mail_from_name', 'new_mail_from_name');
  3.  
  4. function new_mail_from($old) {
  5. return 'admin@yourdomain.com';
  6. }
  7. function new_mail_from_name($old) {
  8. return 'Your Blog Name';
  9. }

Displaying A Login Form In Sidebar

Displaying A Login Form In Sidebar In WordPress

In WordPress 2.x and ealier, to display a login form in sidebar, we must use some raw PHP codes. But since version 3.0, WordPress provides new function, wp_login_form(), to make it done easily. In this article, we’ll see how the function wp_login_form() works and how to implement it in WordPress sidebar.

The function
wp_login_form() takes many parameters:
  wp_login_form(array(
     'echo' => true,
     'redirect' => site_url($_SERVER['REQUEST_URI']),     
      'form_id' => 'loginform',
    'label_username' => __('Username'),
      'label_password' => __('Password'),
     'label_remember' => __('Remember Me'),
     'label_log_in' => __('Log In'),
      'id_username' => 'user_login',
    'id_password' => 'user_pass',
    'id_remember' => 'rememberme',
    'id_submit' => 'wp-submit',
    'remember' => true,
    'value_username' => ,
    'value_remember' => false
));
The most important are:
  • echo: display (true) or not (false) login form. If not, it returns HTML string that you can put anywhere. Default is true.
  • redirect: the URL of web page to redirect user to after login. The default value is site_url( $_SERVER['REQUEST_URI'] ), that means current URL.
  • form_id: ID of login form. Default is loginform. You might want to know this for styling the login form.
  • remember: (true/false) Whether to remember the values. Default is true.
To see all parameters and their meanings, you can go to the Codex.
Now, let’s see an example of wp_login_form’s usage. We’ll check if user is logged in or not. If user logged in, we’ll display a greeting and a logout link. If not, we’ll display a login form. So, open your sidebar.php file and paste the following code into it:

  global $user_login;
  if (is_user_logged_in()) {
      echo 'Hello, ', $user_login, '. <a href="', wp_logout_url(), '" title="Logout">Logout</a>';
  } else {
      wp_login_form();
  }

That’s really simple and clean, isn’t it?
Adding a login form in sidebar is a good way to improve user experience in your WordPress blog, especially when you want your users do everything in the front-end only.


Plugin for Facebook widget in wordpress

1. Copy this code and  save it in plugins folder activate the plugin and enjoy.

<?php
/*

 * Plugin Name:Facebook Like widget

author: Arunendra Pratap Rai

*/

class apr_facebook_widget extends WP_Widget {

    /** constructor */

    function apr_facebook_widget() {

        parent::WP_Widget(false, $name = __('(apr) Facebook Like Box', 'ocmx'), $widget_options = __('Display a Facebook Like Box.','ocmx'));

    }

    /** @see WP_Widget::widget */

    function widget($args, $instance) {

        extract( $args );

        if(isset($instance["title"]))

$title = esc_attr($instance["title"]);

if(isset($instance["facebookpage"]))

$facebookpage = esc_attr($instance["facebookpage"]);

if(isset($instance["height"]))

$height = esc_attr($instance["height"]);

if(isset($instance["faces"]))

$faces = esc_attr($instance["faces"]);

if(isset($instance["colorscheme"]))

$colorscheme = esc_attr($instance["colorscheme"]);

if(isset($instance["stream"]))

$stream = esc_attr($instance["stream"]);

if(isset($instance["border"]))

$border = esc_attr($instance["border"]);      

     

echo $before_widget; ?>

<?php echo $before_title; ?>

            <?php echo $instance['title']; ?>

            <?php echo $after_title; ?>

            

         

        <?php echo $after_widget;

    }

    /** @see WP_Widget::update */

    function update($new_instance, $old_instance) {

        return $new_instance;

    }

    /** @see WP_Widget::form */

    function form($instance) {

        if(isset($instance["title"]))

$title = esc_attr($instance["title"]);

if(isset($instance["facebookpage"]))

$facebookpage = esc_attr($instance["facebookpage"]);

if(isset($instance["height"]))

$height = esc_attr($instance["height"]);

if(isset($instance["faces"]))

$faces = esc_attr($instance["faces"]);

if(isset($instance["colorscheme"]))

$colorscheme = esc_attr($instance["colorscheme"]);

if(isset($instance["stream"]))

$stream = esc_attr($instance["stream"]);

if(isset($instance["border"]))

$border = esc_attr($instance["border"]);

        ?>

            

<?php } } // class FooWidget //This sample widget can then be registered in the widgets_init hook: // register FooWidget widget add_action('widgets_init', create_function('', 'return register_widget("apr_facebook_widget");')); ?>

Redirect Users to Custom Pages by Role in wordpress

WordPress is being used more and more as a web application framework. With that use case comes a bunch of extra circumstances that WordPress doesn't cover. Do you really want your application users to see the WordPress admin? In my web application development experience, the answer to that question is usually "no." Today I'm going to teach you how to redirect a user based on their role to a custom page in WordPress. Getting Set Up Let's start this by building a plugin. You want this in a plugin because it's likely you'll change your theme design and still want the redirect functionality. Any functionality that you want to live past the current theme design should be in a plugin. Create a new plugin folder in your wp-content/plugins directory called 'cm-redirect-by-role' and add a file called cm-redirect-by-role.php. To that file we're going to add the basic WordPress plugin header seen below.
<?php
/*
Plugin Name: Redirect Users by Role
Plugin URI:
Description: Redirects users based on their role
Version: 1.0
Author: Arunendra Pratap Rai
*/
Now that you have a plugin started let's take a look at how user login works. User Login Flow The default spot that a user can log in to your WordPress site is via http://yoursite.com/wp-login.php. When you log in to a site from that location the site sends you to the WordPress admin dashboard. That means that the WordPress admin is starting up and you need to use an admin action to catch the user. I always hook the admin_init action since it runs late enough that you have access to user data but not so late that the user will see anything on the dashboard. Using the admin_init action means that even if they are already logged in and try to access the WordPress admin they will still get redirected. Now let's take a look at the code we are going to use. For our example we'll assume that we want to redirect all subscribers but this will work with any standard or custom role in WordPress.
function cm_redirect_users_by_role() {
    $current_user   = wp_get_current_user();
    $role_name      = $current_user->roles[0];
    if ( 'subscriber' === $role_name ) {
        wp_redirect( 'http://yoursite.com/dashboard' );
    } // if
} // cm_redirect_users_by_role
add_action( 'admin_init', 'cm_redirect_users_by_role' );
We start this process by getting our current user object with wp_get_current_user(). Out of that we get our role name and assign it to the $role_name variable. Then we check if $role_name matches with the role we want to redirect. If it does we use wp_redirect to send the user to our location of choice. While this will work we still have one more piece to add. Making It AJAX safe When making AJAX calls in WordPress you should always call the WordPress AJAX routing file which is inside the WordPress admin. If we leave our code as it is any AJAX call made by our matching roles will fail since it will meet our conditional and be redirected. To fix that we need to check if we are currently doing an AJAX call and if so skip the role check. function cm_redirect_users_by_role() {
    if ( ! defined( 'DOING_AJAX' ) ) {

        $current_user   = wp_get_current_user();
        $role_name      = $current_user->roles[0];

        if ( 'subscriber' === $role_name ) {
            wp_redirect( 'http://yoursite.com/dashboard' );
        } // if $role_name

    } // if DOING_AJAX

} // cm_redirect_users_by_role
add_action( 'admin_init', 'cm_redirect_users_by_role' );
Now we have our redirect function wrapped in a check for the DOING_AJAX constant. If that is defined, we are running an AJAX call and we want to skip the redirect code.