Wordpresscore

Explor Wordpress

How to Ban Users Accounts in WordPress

Are you looking for a way to allow the admin to band WordPress user accounts? While there’s probably a plugin for this, we have created a quick code snippet that you can use to ban users accounts in WordPress.

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

// display checkbox to admin
add_action( 'edit_user_profile', 'ban_user_profile_fields' );
function ban_user_profile_fields( $user ) {
 global $current_user;
 if ( current_user_can( 'edit_user' ) && $user->ID != $current_user->ID ){
  $status = get_the_author_meta( 'ban_user', $user->ID  );
  ?>
  <h3><?php _e("Account Status", "blank"); ?></h3>
  <table class="form-table">
 
  <tr>
       <th>Ban User</th>
       <td><label for="ban_user"><input type="checkbox" name="ban_user" id="ban_user" value="1" <?php if($status == 1){ echo ' checked'; } ?> /></label>
       <span class="description"><?php _e("Check this option to ban this users account."); ?></span>
       </td>
  </tr>
 
  </table>
  <?php
 }
}
 
// Save profile update
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ){
      if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
      update_usermeta( $user_id, 'ban_user', $_POST['ban_user'] );
}
 
// Check if user is banned
add_filter( 'wp_authenticate_user', 'login_ban_status', 1 );
function login_ban_status($user) {
 
        if ( is_wp_error( $user ) ) { return $user; }
        $status = get_user_meta( $user->ID, 'ban_user', 'true' );
 
        if($status == 1){
          return new WP_Error( 'banned', __('<strong>ERROR</strong>: This user account has been banned.', 'banned') );
        }
 
        return $user;
}

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
100 %
Angry
Angry
0 %
Surprise
Surprise
0 %

WordPress Show a Post Date and Modified Date

Sometimes six months after you write a post you decide to update it with some new information. This simple snippet will add a modified date to your posts in the event that you make a change sometime after the initial publication date.

Posted on &lt;?php the_time('F jS, Y') ?&gt;
&lt;?php
          $u_time = get_the_time('U');
          $u_modified_time = get_the_modified_time('U');

      if ($u_modified_time != $u_time) {
                echo &quot;and last modified on &quot;;
                the_modified_time('F jS, Y');
                echo &quot;. &quot;;
          }
?&gt;
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

WordPress Remove the Private/Protected From Your Post Titles

In case you ever publish posts that are private, you’ll notice that the title is prefaced by an unsightly reminder. You can add the code below to your functions file and everything will look good again.

function the_title_trim($title) {

$title = attribute_escape($title);

$findthese = array(
'#Protected:#',
'#Private:#'
);

$replacewith = array(
'', // What to replace &quot;Protected:&quot; with
'' // What to replace &quot;Private:&quot; with
);

$title = preg_replace($findthese, $replacewith, $title);
return $title;
}
add_filter('the_title', 'the_title_trim');
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

WordPress Using the Current Year in Your Posts

Do you ever wish you could insert the current year in some of your posts via a simple shortcode?
Add the snippet below to your functions.php file and you’ll be partying like it’s [year].

function year_shortcode() {
$year = date('Y');
return $year;
}
add_shortcode('year', 'year_shortcode');
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

WordPress Redirect New Registered Users to a Specific Page

If you require a new user to register on your WordPress site, you might want to redirect them to a specific page upon successful completion. Maybe you want to provide them with some important information or specific download.

function wps_registration_redirect(){
return home_url( '/finished/' );
}
add_filter( 'registration_redirect', 'wps_registration_redirect' );
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

WordPress get Tiny URL

Everyone likes tiny, little URLS. Use this snippet to get them:

 
function get_tiny_url( $url ) 
{ 
$tiny_url = file_get_contents( "http://tinyurl.com/api-create.php?url=".$url ); 
 
 
return $tiny_url;
} 
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

WordPress add Paypal Donate Button

Essential for non-profits and charities.

 
<?php 
function donate_shortcode( $atts, $content = null) { 
 
global $post;extract(shortcode_atts(array( 
'account' => 'your-paypal-email-address',
'for' => $post->post_title,
'onHover' => '',
), $atts));</p> 
<p&gt;if(empty($content)) $content='Make A Donation'; 
 
return '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;<span id="IL_AD3" class="IL_AD">business</span>='.$account.'&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;item_name=Donation for '.$for.'" title="'.$onHover.'">'.$content.'</a>'; 
} 
 
add_shortcode('donate', 'donate_shortcode'); 
?> 
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

WordPress Show Tag Cloud

Does what it says – show your tag cloud.

 
<?php wp_tag_cloud(array( 
'smallest' => 10,      // size of least used tag 
'largest' => 18,       // size of most used tag 
'unit' => 'px',        // unit for sizing 
'orderby' => 'name',   // alphabetical
 
'order' => 'ASC',      // starting at A 
'exclude' => 6         // ID of tag to exclude from list 
)); ?> 
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

WordPress Gravatar for Comments

Easily include your gravatar in your comments

 
<?
$email = $comment->comment_author_email;
$default = "http://DOMAIN.COM/gravatar.jpg"; // enter a link to your default avatar
$size = 80; // size in px, this covers width and height
$grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=" . md5($email) . "&amp;default=" . urlencode($default) . "&amp;size=" . $size;
?>
<img src="<?=$grav_url ?>" height="<?=$size ?>" width="<?=$size ?>" alt="gravatar" class="gravatar" title="<?php comment_author();?>"/>
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

WordPress Show List of Recently Created Blogs

Show off all of your latest blogs. Insert into your functions:

 
<?php 
/*Fetch an array of $number_blogs most recently created blogs
**@number_blogs :The number of most recently created blogs you want to show. 
*/ 
function get_recent_blogs($number_blogs=5) 
{ 
 
global $wpdb; 
$blog_table=$wpdb->blogs; 
/*fetch blog_id,domain,path from wp_blogs table ,where the blog is not spam,deleted or archived order by the date and time of registration */ 
$query="select blog_id,domain,path from $blog_table where public='1' and archived='0' and spam='0' and deleted='0' order by registered desc limit 0,$number_blogs"; 
$recent_blogs=$wpdb->get_results($wpdb->prepare($query)); 
 
return $recent_blogs; 
} 
?>

Use the following to get its output:

 
<ul class="recent-blogs"> 
<?php $recent_blogs=get_recent_blogs(5); 
<span style="white-space: pre;"> </span&gt;foreach($recent_blogs as $recent_blog): 
<span style="white-space: pre;"> </span>$blog_url=""; 
<span style="white-space: pre;"> </span&gt;if( defined( "VHOST" ) &amp;&amp; constant( "VHOST" ) == 'yes' ) 
<span style="white-space: pre;"> </span>$blog_url="http://".$recent_blog->domain.$recent_blog->path; 
 
<span style="white-space: pre;"> </span&gt;else
 
<span style="white-space: pre;"> </span>$blog_url="http://".$recent_blog->domain.$recent_blog->path; 
 
<span style="white-space: pre;"> </span>$blog_name=get_blog_option($recent_blog->blog_id,"blogname"); 
<span style="white-space: pre;"> </span>?> 
<li> 
<h3><a href="<?php echo $blog_url;?>"> </a></h3> 
<span><?php echo $blog_name?></span> 
</li> 
<?php endforeach;?> 
</ul>
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Page 2 of 11

Powered by WordPress & Theme by Anders Norén