post

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:

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

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!

post

How to redirect user to his primary blog on login in a multisite

Some web admins don’t like to to redirect the user to network site dashboard upon login. So, they want to redirect the user to the primary blog’s dashboard. Well, it’s fair enough.

So, if an user 5 subsites, and when he will login, he will be redirected to his primary blog’s dashboard. He will still be able to access all of his sites from My Sites link at the top left of admin bar. He is also able to change his primary blog.

So the following script will do the job:
[php]
function go_to_primary_blog($redirect_to, $request, $user){
$user_info = get_userdata($user->ID);
return get_blogaddress_by_id($user_info->primary_blog) . ‘wp-admin’;
}
add_filter(“login_redirect”, “go_to_primary_blog”, 10, 3);
[/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.

Hope it helps!

post

WordPress Multisite: How to show popular post acrosee the network (without any plugin)

WordPress Multisite is a very popular platform now a days for owning of a series of blog site. Well, it can be any e-commerce market. You can easily develop a etsy ptsy style market with MarketPress. Also you can charge your store owner for using your platform as well themes and plugins using Pro Sites. It’s fun, huh?

Sometimes we need to show popular posts across our site. That can be easily done. But what if you want to grab popular posts across the network in multisite? Well, that’s not also a rocket science. We will see today how to get popular posts based on comment count. It’s also possible to get based on page views, that’s in some other day :)

First of all let’s see how to get popular posts in a single wordpress installation. 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.

[php]
function get_popular_posts() {
global $wpdb;
$posts = $wpdb->get_results(“SELECT * FROM {$wpdb->prefix}posts WHERE post_type=’post’ AND post_status=’publish’ AND comment_count != 0 ORDER BY comment_count DESC LIMIT 10″); /*We don’t want to include those posts that has no comments yet, so comment_count != 0 */
$html = ‘

‘;
return $html;
}
[/php]

Then you can call the function anywhere you like – header, footer, sidebar etc.
[php]
echo get_popular_posts();
[/php]

Now, if we want to get popular posts across the network, we need to use the above function with a little tricks :) Let’s take a look:

[php]
function get_popular_posts() {
global $wpdb;

$blogs = $wpdb->get_results( “SELECT blog_id FROM {$wpdb->blogs} WHERE blog_id != {$wpdb->blogid} AND site_id = ‘{$wpdb->siteid}’ AND spam = ‘0’ AND deleted = ‘0’ AND archived = ‘0’ order by blog_id”, ARRAY_A);
array_unshift($blogs, 1); /*Including parent blog in the array*/
$html = ‘

    ‘;
    foreach($blogs as $blog) {
    switch_to_blog( $blog[ ‘blog_id’ ] );
    $posts = $wpdb->get_results(“SELECT * FROM {$wpdb->prefix}posts WHERE post_type=’post’ AND post_status=’publish’ AND comment_count != 0 ORDER BY comment_count DESC LIMIT 10″);
    foreach($posts as $post) {
    $html .= ‘

  • ID).’”>’.$post->post_title.’
  • ‘;
    }
    }
    $html .= ‘

‘;
return $html;
}
[/php]

Then you can call the function anywhere you like – header, footer, sidebar etc.
[php]
echo get_popular_posts();
[/php]

Easy pissy, nah? ;)

post

WordPress Multisite: How to define a common upload folder for all blogs or subsites

In WordPress multisite, media uploaded from the subsite users are stored to different subdirectories using blog id into uploads folder. But, if you need to save all media files in one folder rather than uploads folder with lots of child folders, then you are in right place.

Use the following code into your theme functions.php file if you think your theme won’t be changed. Otherwise, mu-plugins is the best option.

[php]
0) {
$upload[‘path’] = ABSPATH . ‘wp-content/images’;
switch_to_blog(1);
$upload[‘url’] = site_url() . ‘/’ . ‘wp-content/images’;
restore_current_blog();
$upload[‘subdir’] = “”;
$upload[‘basedir’] = $upload[‘path’];
$upload[‘baseurl’] = $upload[‘url’];
}
return $upload;
}

function make_filename_hash($filename) {
global $user_ID;
return $user_ID . time() . $filename; /*Using time() function for uniqueness*/
}
add_filter(‘sanitize_file_name’, ‘make_filename_hash’, 10);
[/php]

Enjoy!