Post Tags

WordPress

Pretty posts

We know that WordPress is currently most popular blogging CMS. With WordPress, you can do many things. There are a lot of plugins that's help you to create your site amazing. Actually, plugins can be called the soul of WordPress.

Use the plugin on your site help to improve your site but remember the uses of additional plugin can slow your site. Moreover, malicious code hidden into many free/Nulled plugins. That's make your site harmful by hacker. Now you say, "I saw many people using this plugin, but nothing happen".

Hmm, you are right. But i know many people whose sites installed the plugin, after a few month there site has been hacked. So, are you stop using the plugin? Definitely not. But which work can be done easily by entering a few line of code, there don't need use any plugins. I used these method to my WordPress theme projects. That's what's i am going into this series.

FTP/cPanel File Manager is the best way to use these code. Because when editing with WordPress (Appearance > Editor > functions.php ) if you do anything wrong then you can't access your site. Paste these code at the end of your theme function.php file before ;?>
If you have an idea about PHP then you can put code whatever you like.

Automatically notify your members on new posts:
//Sends new post list automatically to all users email

function newPostNotify($post_ID) {
if( wp_is_post_revision($post_ID) ) return;
global $wpdb;
$get_post_info = get_post($post_ID);
if ( $get_post_info->post_status == 'publish' && $_POST['original_post_status'] != 'publish' ) {
// Get all of the emails from the database
$wp_user_email = $wpdb->get_results("SELECT DISTINCT user_email FROM $wpdb->users");
// Send emails to each of registered users
foreach ( $wp_user_email as $email ) {
// Email subject
$subject = 'New Posts from Codiblog.Com ';
// Messages:new post url:+ URL
$message = 'View this new post now:' . get_permalink($post_ID);
// Send email
wp_mail($email->user_email, $subject, $message);
}
}
}

add_action('publish_post', 'newPostNotify'); 

Remove unnecessary menu from site toolbar:
##Removing Unusal menu##

function hide_yoastseo() {
remove_action('admin_bar_menu', 'wpseo_admin_bar_menu',95);
remove_menu_page('wpseo_dashboard');
}
add_action( 'admin_init', 'hide_yoastseo');
#Remove Contact Form 7 Links from dashboard menu items if not admin #
if (!(current_user_can('administrator'))) {
function remove_wpcf7() {
remove_menu_page( 'wpcf7' );
}

add_action('admin_menu', 'remove_wpcf7'); 

Remove help entry from admin panel:
add_filter( 'contextual_help', 'mytheme_remove_help_tabs', 999, 3 );
function mytheme_remove_help_tabs($old_help, $screen_id, $screen){
$screen->remove_help_tabs();
return $old_help;
}

Support shortcode and PHP to widget & menu :
//Adding Shortcodes support
add_filter('wp_nav_menu_items', 'do_shortcode');
add_filter('widget_text', 'do_shortcode');
 
//Adding PHP Support
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}

Edit login page message :
// custom login message
function custom_login_message() {
$message = "<div style='background: #F8FFC7;padding: 8px;text-align: center;color: #9E9E9E;border: 1px solid #E7E7BD;'>
If you already have an account in <b>Codiblog.com</b>, then Log In below. Otherwise Click Register and create an account to start writing posts.
</div>";
return $message;
}
add_filter('login_message', 'custom_login_message');
N.B: The code $ message = ".." Here you can customize the message (HTML Supported)

Auto set first image in post as featured image:
function auto_featured_image() {
global $post;
 
if (!has_post_thumbnail($post->ID)) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
 
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
}
// Use it temporary to generate all featured images
add_action('the_post', 'auto_featured_image');
// Used for new posts
add_action('save_post', 'auto_featured_image');
add_action('draft_to_publish', 'auto_featured_image');
add_action('new_to_publish', 'auto_featured_image');
add_action('pending_to_publish', 'auto_featured_image');
add_action('future_to_publish', 'auto_featured_image');

Only show post & comment to backend:
## Only Users post in Backend ##
function posts_for_current_author($query) {
global $user_level;
 
if($query->is_admin && $user_level < 5) {
global $user_ID;
$query->set('author',  $user_ID);
unset($user_ID);
}
unset($user_level);
 
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
 
## Only Users own post comment##
function wps_get_comment_list_by_user($clauses) {
if (is_admin()) {
global $user_ID, $wpdb;
$clauses['join'] = ", ".$wpdb->base_prefix."posts";
$clauses['where'] .= " AND ".$wpdb->base_prefix."posts.post_author = ".$user_ID." AND ".$wpdb->base_prefix."comments.comment_post_ID = ".$wpdb->base_prefix."posts.ID";
};
return $clauses;
};
if(!current_user_can('edit_others_posts')) {
add_filter('comments_clauses', 'wps_get_comment_list_by_user');
}

Remove unnecessary box from admin panel:
function remove_dashboard_widgets() {
global $wp_meta_boxes;
 
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
 
}
Now this. If you face any problem, don't forget to comment.