Skip to content

Instantly share code, notes, and snippets.

View Tsunamijaan's full-sized avatar

Tariqul Islam Tsunamijaan

  • Coder Team IT
  • Rajshahi, Bangladesh
View GitHub Profile
@Tsunamijaan
Tsunamijaan / Logged In Users Current Info
Created January 5, 2019 08:57
Logged In Users Current Info
<?php global $current_user;
get_currentuserinfo();
echo 'Username: ' . $current_user->user_login . "\n";
echo 'User email: ' . $current_user->user_email . "\n";
echo 'User first name: ' . $current_user->user_firstname . "\n";
echo 'User last name: ' . $current_user->user_lastname . "\n";
echo 'User display name: ' . $current_user->display_name . "\n";
echo 'User ID: ' . $current_user->ID . "\n";
?>
@Tsunamijaan
Tsunamijaan / Remove width & height from images in posts
Created January 5, 2019 08:56
Remove width & height from images in posts
add_filter( 'post_thumbnail_html', 'remove_wps_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_wps_width_attribute', 10 );
function remove_wps_width_attribute( $html ) {
$html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
return $html;
}
@Tsunamijaan
Tsunamijaan / Automatically add the Google +1 button
Created January 5, 2019 08:55
Automatically add the Google +1 button
add_filter('the_content', 'google_plusone');
function google_plusone($content) {
$content = $content.'<div class="plusone"><g:plusone size="tall" href="'.get_permalink().'"></g:plusone></div>';
return $content;
}
add_action ('wp_enqueue_scripts','google_plusone_script');
function google_plusone_script() {
wp_enqueue_script('google-plusone', 'https://apis.google.com/js/plusone.js', array(), null);
}
@Tsunamijaan
Tsunamijaan / How to loop posts from a specific category in homepage
Created January 5, 2019 08:55
How to loop posts from a specific category in homepage
add_action( 'pre_get_posts', function ( $q ) {
if( $q->is_home() && $q->is_main_query() ) {
$q->set( 'cat', CATEGORY_ID );
$q->set( 'posts_per_page', 3 );
}
});
@Tsunamijaan
Tsunamijaan / Automatically Disable comments on posts over one month
Created January 5, 2019 08:53
Automatically Disable comments on posts over one month
function close_comments( $posts ) {
if ( !is_single() ) { return $posts; }
if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
$posts[0]->comment_status = 'closed';
$posts[0]->ping_status = 'closed';
}
return $posts;
}
add_filter( 'the_posts', 'close_comments' );
@Tsunamijaan
Tsunamijaan / Member only content
Created January 5, 2019 08:52
Member only content
add_shortcode( 'member', 'member_check_shortcode' );
function member_check_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
Uses:===============
@Tsunamijaan
Tsunamijaan / Require minimum word count to published post
Created January 5, 2019 08:51
Require minimum word count to published post
function minWord($content){
global $post;
$num = 100; //set this to the minimum number of words
$content = $post->post_content;
if (str_word_count($content) < $num)
wp_die( __('Error: your post is below the minimum word count.') );
}
add_action('publish_post', 'minWord');
@Tsunamijaan
Tsunamijaan / Browser detection body classes
Created January 5, 2019 08:51
Browser detection body classes
add_filter('body_class','browser_body_class');
function browser_body_class($classes) {
global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;
if($is_lynx) $classes[] = 'lynx';
elseif($is_gecko) $classes[] = 'gecko';
elseif($is_opera) $classes[] = 'opera';
elseif($is_NS4) $classes[] = 'ns4';
elseif($is_safari) $classes[] = 'safari';
elseif($is_chrome) $classes[] = 'chrome';
elseif($is_IE) $classes[] = 'ie';
@Tsunamijaan
Tsunamijaan / Filter search results by post type
Created January 5, 2019 08:50
Filter search results by post type
function search_posts_filter( $query ){
if ($query->is_search){
$query->set('post_type',array('post','custom_post_type1', 'custom_post_type2'));
}
return $query;
}
add_filter('pre_get_posts','search_posts_filter');
@Tsunamijaan
Tsunamijaan / Set maximum post title length
Created January 5, 2019 08:49
Set maximum post title length
function JebamaxWord($title){
global $post;
$title = $post->post_title;
if (str_word_count($title) >= 10 ) //set this to the maximum number of words
wp_die( __('Error: your post title is over the maximum word count.') );
}
add_action('publish_post', 'JebamaxWord');