WordPress Ke Liye 30 Useful Code Snippets [Full Customize & Control]

Hello friends, अगर आप WordPress user हो तो आप जानते ही होंगे कि WordPress में आपको full control दिया जाता है. जिससे आप ब्लॉग में ब्लॉग में बहुत से चीजों को access कर सकते हो. आज हम आपको WordPress के कुछ Useful Snippets के बारे में बताने वाले हैं. जिससे आप अपने ब्लॉग को advanced level तक control और customize कर सकते हो.
WirdPress ke kiye 30 important usefuo code snippets in hindi

हम सभी लोग जानते हैं कि WordPress में Plugins को नए features को extend करने के लिए use किया जाता है. इनकी help से आप ब्लॉग को customize कर सकते हो और ब्लॉग में नई function को add कर सकते हो. यदि आपको coding की knowledge नही है तो आप plugin के माध्यम से उसे कर सकते हो.

WordPress team ने इसमे plugin की feature इसलिए add किये हैं ताकि जो लोग coding नही जानते हैं वो इसके द्वारा आसानी से ब्लॉग को manage और customize कर पाए. बड़े बड़े developers अपने ब्लॉग में सिर्फ कुछ ही plugins का use करते हैं बाकी वो manually करने की कोशिश करते हैं.

में अपने readers को हमेशा suggest करता हूँ कि ज्यादा plugins का use करने से बचें. क्योंकि इसके कई नुकसान भी हैं. अगर आप उसी काम को manually करते हो तो आप दूसरों से अच्छा और अलग कर सकते हो. लेकिन यदि आप ब्लॉग में manually customize या control करना चाहते हो तो आपको coding की जानकारी होनी चाहिए. अगर आप coding नही जानते हो तो YouTube में video देखकर या Book पढ़कर भी सीख सकते हो।

In my case, मुझे अपने ब्लॉग में plugin use करने से डर लगता है. क्योंकी hackers किसी site को hack करने के लिए अनेक तरीके अपनाते हैं. यदि आप unknown plugin को use करते हैं तो hacking का खतरा ज्यादा होता है. क्योंकि कुछ hacker plugin में malicious script add कर देते हैं और जब कोई उसे ब्लॉग में install करता है तो hacker आसानी से उस ब्लॉग को hack कर लेते हैं. इसलिए हमेशा कोशिश करें कि आप जिस plugin का use करते हो तो उसके बारे research कर details प्राप्त कर लीजिए।

आप अपने ब्लॉग में बहुत से काम को without plugin भी कर सकते हो. Manually, अगर आप कुछ भी करेंगे तो इससे कोई drawbacks की चिंता नही होगी. इसलिए हम आपको कुछ wordpress snippet code बताएंगे. जिससे आप अपने ब्लॉग में extra features enable कर सकते हो.

20 Useful Code Snippets for WordPress.

Note: ये सभी snippet codes कई सारे sites से लिया गया है. These snippet codes are copied from various sites on the Internet.

1. Allow Contributors to Upload Images:

By Default, WordPress contributors को image upload करने के लिए permission नही देता है. अगर आप किसी user के role में contributor set किये हुए हैं तो वो आपके ब्लॉग में image upload कर नही पाएंगे. WordPress Author और Editor role वाले users को ही image add करने के लिए allow करता है।

Now, हम आपको कुछ lines के code बताने वाले हैं, जिन्हें ब्लॉग में add करने के बाद आपके contributors भी image upload कर पाएंगे. इसके लिए आप नीचे दिए code को अपने ब्लॉग theme की functions.php में add कीजिए।

if ( current_user_can('contributor') && !current_user_can('upload_files') )
     add_action('admin_init', 'allow_contributor_uploads');      
     function allow_contributor_uploads() {
          $contributor = get_role('contributor');
          $contributor->add_cap('upload_files');
     }

2. Protect Your Site from Malicious Requests:

आप अपने WordPress site को कई सारे तरीके से malicious attack से बचा सकते हैं. आप अपने ब्लॉग में free plugin के उपयोग से firewall protection enable कर सकते हो. अगर आप अपने ब्लॉग को malicious requests से manually protect करना चाहते हो तो इसके लिए आप नीचे दिए code को अपने theme की functions.php में add कर दीजिए।

global $user_ID; if($user_ID) {
    if(!current_user_can('administrator')) {
        if (strlen($_SERVER['REQUEST_URI']) > 255 ||
            stripos($_SERVER['REQUEST_URI'], "eval(") ||
            stripos($_SERVER['REQUEST_URI'], "CONCAT") ||
            stripos($_SERVER['REQUEST_URI'], "UNION+SELECT") ||
            stripos($_SERVER['REQUEST_URI'], "base64")) {
                @header("HTTP/1.1 414 Request-URI Too Long");
                @header("Status: 414 Request-URI Too Long");
                @header("Connection: Close");
                @exit;
        }
    }
}

3. Disable the Admin bar:

WordPress में logged in userd को admin bar show होते हैं, जिसमे कुछ important link added होते हैं. जैसे कि add new post, new page के link भी उसमे added होते हैं. कुछ लोग इसे disabled रखना चाहते हैं तो उसके लिए आपको अपने blog theme भी functions.php में निम्न code add कर देना है।

// Remove the admin bar from the front end
add_filter( 'show_admin_bar', '__return_false' ); 

4. Show Post Thumbnails in RSS Feed:

हमारे ब्लॉग में post title के साथ साथ thumbnail show होते हैं तो उसका look बहुत अच्छा होता है. By default, आपके WordPress ब्लॉग की RSS Feed में thumbnail show नही होता है. बहुत से लोग चाहते हैं कि RSS feed में भी thumbnail show हो तो इसके लिए हम आपको नीचे एक code बता रहे हैं, इन्हें आप theme की functions.php file में add कर दीजिए।

// Put post thumbnails into rss feed
function wpfme_feed_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '' . $content;
}
return $content;
}
add_filter('the_excerpt_rss', 'wpfme_feed_post_thumbnail');
add_filter('the_content_feed', 'wpfme_feed_post_thumbnail'); 

5. Change Author URL Structure:

By default, WordPress में किसी author profile का default URL इस तरह होता है. yoursite.com/author/name आप इस URL में author के जगह कोई दूसरा structure add कर सकते हो. जैसे कि yoursite.com/publisher/name

इसके लिए आपको एक simple code snippet को आप के ब्लॉग theme की functions.php में add करना होगा. याद रहे नीचे code में हम URL structure में publisher ले रहे हैं. आप इसे change भी कर सकते हो।

add_action('init', 'cng_author_base');
function cng_author_base() {
    global $wp_rewrite;
    $author_slug = 'publisher'; // change slug name
    $wp_rewrite->author_base = $author_slug;
}

6. Disable or Limit Post Revisions:

बहुत से लोग इसके बारे में पहले से जानते होंगे। जो लोग पहली बार इसके बारे में सुने हैं, उन्हें शायद थोड़ा अजीब लग सकता है. लेकिन यह सच है कि जब हम wordpress ब्लॉग में किसी post में कोई changes करते हैं तो पहले वाली post revision के रूप में save हो जाती है. यह हमारे ब्लॉग की database में add होकर उसकी size को ज्यादा कर देते हैं.

यदि आप अपने ब्लॉग posts को regular update करेंगे तो उसमें revisions बहुत ज्यादा create होंगे. जिससे आपके ब्लॉग की database का size ज्यादा हो जाएगा और आपके ब्लॉग की loading speed slow हो जाएगी. कभी कभी यह features हमारे लिए बहुत उपयोगी भी साबित हो जाती है. लेकिन ज्यादा तर cases में जब हम post update करते हैं तो old revision की जरूरत ही नही होती है.

इसीलिए आप अपने ब्लॉग में post revision create होने की limit set कर सकते हो या इसे disable भी कर सकते हो. अगर आप post revision को disable करना चाहते हो तो इसके लिए आपको अपने wordpress की directory में जाना है और wp-config.php को open करके उसमे इस code को add करना है।

define('WP_POST_REVISIONS', false); 

यदि आप post revision create होने की limit set करना चाहते हो तो इसके लिए wp-config.php में नीचे दिए code को add करना है. हम यहाँ maximum revisions create होने की value 3 ले रहे हैं. आप इसे अपने हिसाब से set कर सकते हो।

define('WP_POST_REVISIONS', 3); 

7. Set Auto Save Interval:

जिस तरह आप wordpress में post update करते हो तो old post revision में convert होकर database में store हो जाता है. उसी तरह जब आप wordpress में post लिखते हो तो वहाँ हर 1 मिनट में post save हो जाता है।

See also  WordPress Database Se Orphan/Unused Tables Ko Delete Kaise Kare

इससे आपका post हर minute में save होकर draft में आ जाता है. इससे भी इससे भी revisions create होते हैं. और आपके ब्लॉग की database size बढ़ जाती है. जिसके कारण आपका site slow load होता हैं.

अगर आप post saving की interval time ज्यादा करना चाहते हो तो इसके लिए आप नीचे दिए code को अपने ब्लॉग की wp-config.php में add कीजिए.

define('AUTOSAVE_INTERVAL', 300); 

8. Disable Automatic Updates:

जब हम wordpress site में automatic updates को enable करके रखते हैं तो इससे जब wordpress core, theme या plugin update होते हैं तो हमारे site में automatically update हो जाते हैं।

By default, जब wordpress core, plugin या theme का new version update होता है तो उसका notification हमारे ब्लॉग की dashboard में show होता है सुर हमें उसे update करना पड़ता है।

बड़े बड़े wordpress experts का मानना है कि automatic updates को enable रखना ठीक नही है. इससे बहुत सारे नुकसान हो सकते हैं, इसलिए wordpress users को इसे disable करके रखना चाहिए. इसके लिए आप नीचे दिए code को wp-config.php में add कर दीजिए.

define('WP_AUTO_UPDATE_CORE', false); 

9. Remove WordPress Version Number:

आप जानते ही होंगे कि wordpress पूरे world मर सबसे best CMS मानी जाती है और इसी लिए यह hackers के निशाने पर सबसे अधिक है. By default, हमारे ब्लॉग की source code check करने पर version number show होता है। यदि hackers को हमारे ब्लॉग की wordpress version के बारे में पता चलता है तो उसे hack करने में आसानी होती है।

इसलिए यदि आप hackers से सुरक्षित रहना चाहते हो तो अपने ब्लॉग से wordpress version number को remove कर दीजिए. इसके लिए बस आपको अपने ब्लॉग theme के functions.php में ये code add कर देना है।

remove_action('wp_head', 'wp_generator'); 

10. Optimizing the Dashboard with Customized Message:

जब आप अपने wordpress ब्लॉग में login करने के बाद dashboard में जाते होंगे तो वहाँ Howdy, Username लिखा हुआ आता है. आप इसे customize करके Howdy की जसघ कोई दूसरी चीज लिख सकते हो. जैसे कि आप Howdy की जगह Welcome को replace कर सकते हो. जिसके बाद Welcome, Username लिखा हुआ आएगा.

इसके लिए आपको एक simple php code को अपने ब्लॉग की functions.php में add करना होगा. नीचे code में आप Welcome की जगह कोई दूसरी चीज भी लिख सकते हो।

function replace_howdy($wp_admin_bar) {
$my_account = $wp_admin_bar->get_node('my-account');
$newtitle = str_replace('Howdy', 'Welcome', $my_account -> title);
$wp_admin_bar -> add_node(array(
'id' => 'my-account',
'title' => $newtitle,
));
}

add_filter('admin_bar_menu', 'replace_howdy', 25);

11. Limit Number of Results in Search Page:

कोई भी जब हमारे ब्लॉग में search करता है तो ज्यादा time तक scroll नही करना चाहता है. क्योंकि वो जिसके बारे में search कर रहे हैं वो top में ही show होगा. नीचे उससे related results ही show होते हैं. इसलिए search result page में result की limitation setup कर देनी चाहिए.

इसके लिए आप नीचे दिए code को अपने ब्लॉग की functions.php में add कर देना है. इसमे आप 20 के जगह जितने results दिखाना चाहते हो वो set कर दीजिए.

function limit_posts_per_search_page() { 
if ( is_search() ) 
set_query_var('posts_per_archive_page', 20); 
} 

add_filter('pre_get_posts', 'limit_posts_per_search_page'); 

12. Controlling URL spam with a simple Function:

Spamming कभी भी खत्म नही होने वाला process है. Spamming सिर्फ आपके site की performance को down नही करती है बल्कि आपके site की search ranking को भी decrease कर देती है. इसलिए हमेशा spamming से बचे रहने की कोशिश करना चाहिए।

अगर आपके पास ब्लॉग है तो उसमें भी spam comment आते रहते होंगे. उसमे आपने देखा होगा कि URL बहुत बड़ा होता है. यदि आप spamming से बचना चाहते हो तो comment URL का maximum length set कर देना होगा. इसके लिए आप नीचे दिए code को अपने theme की functions.php में add कर दीजिए. यहाँ हमने URL की maximum length 50 रखी है, आप उसे change भी कर सकते हो।

function rkv_url_spamcheck ($approved, $commentdata ) {
return(strlen ( $commentdata['comment_author_url']> 50 )? 'spam' : $approved;
}

add_filter( 'pre_comment_approved', 'rkv_url_spamcheck', 99, 2);

13. Setting minimum Content length:

हमने ऊपर में ही आपको spamming के नुकसान के बारे में बता दिया है. फिलहाल हम आपको एक और अच्छा तरीका बता रहे हैं, जिससे आप अपने ब्लॉग में spam comments को कम कर सकते हो. इसके लिए हम comment की minimum length set कर देंगे।

अक्सर, spam comment की length बहुत कम होती है. क्योंकि उन्हें सिर्फ एक link चाहिए बाकी comment में कुछ लिखे न लिखे उससे कोई मतलब नही होता है. इसलिए आप अपने ब्लॉग में comment में minimum length set कर सकते हो. इसके लिए आप minimum length में 20 set करते हो तो जब कोई comment में 20 character से कम character use करेंगे तो comment submit नही होगा।

इसके लिए आप अपने ब्लॉग की functions.php में नीचे दिए code को add कर दीजिए. इसमे हमने minimum comment length 20 character set की है, आप 20 के जगह दूसरा value put कर सकते हो।

add_filter('preprocess_comment', 'minimal_comment_length');
function minimal_comment_length($commentdata) {
$minimalCommentLength = 20; //change it according to your requirement

if ( strlen(trim($commentdata['comment_content'])) < $minimalCommentLength) {
wp_die('All comments must be at least' . $minimalCommentLength . 'characters long')

}
return $commentdata;
}

14. Redirecting user registration to a specific page:

अगर आप अपने ब्लॉग में users को account बनाने के लिए allow करते हैं तो यह आपके लिए helpful होगा. बहुत से लोग ब्लॉग में guest post accept करने के लिए users को account बनाने के लिए allow करते हैं. जब आपके ब्लॉग में कोई new account create करता है तो create होने के बाद user को homepage पर redirect कर दिया जाता है.

आप new registered users को किसी specific page में redirect कर सकते हो. जैसे कि आप उन्हें guidelines या privacy and policy की page में redirect कर सकते हो. इसके लिए आप नीचे दिए code को अपने theme की functions.php में add कर दीजिए. ध्यान रहे कि इसमे हमने /finished/ add किया है, जिससे user example.com/finished/ page में redirect हो जाएगा। आप अपने हिसाब से भी set कर सकते हो।

function wps_registration_redirect(){
return home_url('/finished');
}

add_filter('registration_redirect', 'wps_registration_redirect'); 

15. Emptying WordPress Trash For Boost Performance:

जब आप ब्लॉग में post, page या कोई media file को delete करते हो तो वो trash folder में आकर जमा हो जाता है. अगर आपके ब्लॉग की trash folder में बहुत ज्यादा posts या media store हो जाएंगे तो यह आपके ब्लॉग की performance को down कर सकती है।

See also  Google AMP Use Karne Ke Pros And Cons (Fayde Aur Nuksan)

By default, WordPress में trash folder की data हर 30 days में automatically delete हो जाती है. इतने दिन में आपके ब्लॉग की बहुत से data इस folder में आ जाएंगे, इसलिए आप इसकी time को change कर सकते हो. इसके लिए आपको अपने wordpress ब्लॉग की wp-config.php file में नीचे दिए code को add कर देना होगा. इसमे हमने 10 days value दिया है, आप इसे change सकते हो।

define('EMPTY_TRASH_DAYS', 10 ); // 10 days 

17. Remove the URL field from WordPress comment form:

यह छोटा सा function code आपके ब्लॉग में comment form से URL field को remove कर देगा. इससे आप spam comment से safe हो जाएंगे. इसके लिए आप नीचे दिए गए code को अपने theme की functions.php में add कर दीजिए।

function remove_comment_fields($fields) {
    unset($fields['url']);
    return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields'); 

18. Display comments in admin to authors own posts only:

अगर आपके ब्लॉग में multi author काम करते हैं तो यह आपके लिए बहुत काम की हो सकती है. अक्सर, जब हमारे ब्लॉग में multi author होते हैं तो all post के comments all authors को show होती है, जिससे अलग अलग author अलग अलग post के comments का reply दे देते हैं.

अगर देखा जाए तो ऐसा ठीक नही है. क्योंकि कोई भी आदमी के comment करने के मकसद author से संतुष्ट होना होता है. ऐसे में जब कोई दूसरा author उसका reply देते हैं तो commentator को ठीक नही लगता है. इसलिए अलग अलग authors के admin page पर आप सिर्फ उसके post के comments दिखा सकते हो. इसके लिए बस आपको नीचे दिए code को अपने theme functions.php में add करना होगा।

function wps_get_comment_list_by_user($clauses) {
        if (is_admin()) {
                global $user_ID, $wpdb;
                $clauses['join'] = ", wp_posts";
                $clauses['where'] .= " AND wp_posts.post_author = ".$user_ID." AND wp_comments.comment_post_ID = wp_posts.ID";
        };
        return $clauses;
};
if(!current_user_can('edit_others_posts')) {
add_filter('comments_clauses', 'wps_get_comment_list_by_user');
}

19. Temporary Maintenance:

अल्सर, जब हम अपने ब्लॉग को customize करते हैं या ब्लॉग में कोई changing करते हैं तो site में maintenance mode को enable कर देते हैं. इससे visitors को पता चलता है कि site में कुछ changes हो रहे हैं. यदि हम live ब्लॉग या website में changes करेंगे तो हमारे visitors को परेशानी हो सकती है।

इसके लिए बहुत से plugins हैं लेकिन आप बिना plugin का use किये भी अपने ब्लॉग में temporary maintenance mode enable कर सकते हो. इसके लिए आपको अपने ब्लॉग theme की functions.php में नीचे दिए code को add करना है.

// Temp Maintenance - with http response 503 (Service Temporarily Unavailable)
// This will only block users who are NOT an administrator from viewing the website.
function wp_maintenance_mode(){
    if(!current_user_can('edit_themes') || !is_user_logged_in()){
        wp_die('Maintenance, please come back soon.', 'Maintenance - please come back soon.', array('response' => '503'));
    }
}
add_action('get_header', 'wp_maintenance_mode');

20. Set First Uploaded as Featured Image:

जब हम post लिखते हैं तो उसमें featured image को set करना जरूरी होता है. यदि आप features image को choose नही करते हो या करना भूल जाते हो तो loop में thumbnail display होने में problem हो सकती है।

इसलिए यदि आप कई बार post में featured image select नही करते हो तो नीचे दिए function का use करके आप by default, first uploaded image को featured automatically choose कर सकते हो. इसके लिए आप theme functions.php में नीचे दिए code का use कर सकते हो।

add_filter('the_content', 'set_featured_image_from_attachment');
function set_featured_image_from_attachment($content) {
	global $post;
	if (has_post_thumbnail()) {
		// display the featured image
		$content = the_post_thumbnail() . $content;
	} else {
		// get & set the featured image
		$attachments = get_children(array(
			'post_parent' => $post->ID, 
			'post_status' => 'inherit', 
			'post_type' => 'attachment', 
			'post_mime_type' => 'image', 
			'order' => 'ASC', 
			'orderby' => 'menu_order'
		));
		if ($attachments) {
			foreach ($attachments as $attachment) {
				set_post_thumbnail($post->ID, $attachment->ID);
				break;
			}
			// display the featured image
			$content = the_post_thumbnail() . $content;
		}
	}
	return $content;
}

21. Allow PHP in WordPress text widgets:

आप सभी जानते ही होंगे कि wordpress widget में PHP code use करना allow नही किया गया है. इसलिए अगर आपको कभी widget में php code use करने की जरूरत परी तो आप plugins का use कर सकते हो.

Alternatively, आप बिना plugin के भी widgets में php code use कर सकते हो. इसके लिए आपको अपने ब्लॉग theme की functions.php में ये code add कर देना है।

function php_text($text) {
 if (strpos($text, '<' . '?') !== false) {
 ob_start();
 eval('?' . '>' . $text);
 $text = ob_get_contents();
 ob_end_clean();
 }
 return $text;
}
add_filter('widget_text', 'php_text', 99); 

22. Move WordPress Admin Bar to the Bottom:

जब आप ब्लॉग में logged in होते हो तो ऊपर में admin bar show होता है. यह सभी pages में show होता है. आप इस admin बार को plugin की help से move कर सकते हो लेकिन आप इसे बिना plugin के भी bottom में move कर सकते हो.

इसके लिए आपको अपने theme की functions.php में ये code add कर देना है।

    <style type="text/css">
    body { 
    margin-top: -28px;
    padding-bottom: 28px;
    }
    body.admin-bar #wphead {
       padding-top: 0;
    }
    body.admin-bar #footer {
       padding-bottom: 28px;
    }
    #wpadminbar {
        top: auto !important;
        bottom: 0;
    }
    #wpadminbar .quicklinks .menupop ul {
        bottom: 28px;
    }
    </style>';
}
// on backend area
add_action( 'admin_head', 'fb_move_admin_bar' );
// on frontend area
add_action( 'wp_head', 'fb_move_admin_bar' ); 

23. Delete unused post revisions:

हमने आपको पहले ही revisions के बारे में बता दिया है कि जब हम ब्लॉग post कोई changes करते हैं या उसे update करते हैं तो revisions create होती है. आप ये भी जानते हो कि इससे database में size बढ़ जाती है और site performance down हो जाती है।

इसलिए अगर तो अपने ब्लॉग की database से unused revisions को delete करना है तो सबसे पहले cPanel में login करके PhpMySQL में जाएँ और अपने ब्लॉग की database select करें. अब उसके बाद आपको SQL टैब में जाना है और वहाँ नीचे दिए query add करके Go पर click करना होगा।

DELETE a,b,c FROM wp_posts a
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)
WHERE a.post_type = 'revision' 

24. Delete all Spam Comments:

WordPress ब्लॉग में spam comments एक बहुत बड़ी problem होती है. यह spam comments की समश्या सबसे अधिक wordpress में ही है. अगर आपका ब्लॉग wordpress में है तो हर दिन 50+ spam comments तो आते ही होंगे।

See also  WordPress Blog Me Broken Link Ko Fix Kaise Kaise

In my case, मेरे ब्लॉग में daily 300+ spam comments आते हैं. में इसके लिए Akismet plugin use करता हूँ. जब हमारे ब्लॉग में spam comments अधिक हो जाते हैं तो performance पर bad effect पड़ता है. अगर आपके ब्लॉग में बहुत सारे spam comments हैं तो उसे आप आसानी से delete कर सकते हो।

इसके लिए cPanel में login करके PhpMySQL में जाएँ और अपने ब्लॉग की database select करें. अब उसके बाद आपको SQL टैब में जाना है और वहाँ नीचे दिए query add करके Go पर click करना होगा।

DELETE FROM wp_comments WHERE comment_approved = 'spam';

25. Required a Post Featured Image:

अक्सर, मेरे साथ ऐसा होता है कि जब में ब्लॉग में post publish करता हूँ यो उसमे featured image choose करना भूल जाता हूँ. अगर आपके साथ भी कभी कभी ऐसा होता जी तो में आपको जो code बताने वाला हूँ, उसका use करके featured image को post में required कर सकते हो. I mean, जब आप आप post में featured image को select नही करेंगे तो publish नही होता है. जब आप post में featured image set करना भूल जाएंगे और publish button पर click करेंगे तो warning massage show होगा।

इसके लिए आप simply नीचे दिए code theme की functions.php में add कर दीजिए।

add_action('save_post', 'wpds_check_thumbnail');
add_action('admin_notices', 'wpds_thumbnail_error');
function wpds_check_thumbnail($post_id) {
    // change to any custom post type
    if(get_post_type($post_id) != 'post')
        return;
    if ( !has_post_thumbnail( $post_id ) ) {
        // set a transient to show the users an admin message
        set_transient( "has_post_thumbnail", "no" );
        // unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'wpds_check_thumbnail');
        // update the post set it to draft
        wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
        add_action('save_post', 'wpds_check_thumbnail');
    } else {
        delete_transient( "has_post_thumbnail" );
    }
}
function wpds_thumbnail_error()
{
    // check if the transient is set, and display the error message
    if ( get_transient( "has_post_thumbnail" ) == "no" ) {
        echo "<div id='message' class='error'><p><strong>You must select Featured Image. Your Post is saved but it can not be published.</strong></p></div>";
        delete_transient( "has_post_thumbnail" );
    }
} 

26. Disable Auto Link in User Comments:

By default, जब आपके ब्लॉग में कोई comment करता है तो उसमें http:// से start होने वाली कोई भी text link में convert हो जाता है. इससे आपको बहुत परेशानी हो सकती है. अगर आप comment से auto link को disable करना चाहते हो तो नीचे दिए code को functions.php में add कर दीजिए।

remove_filter('comment_text', 'make_clickable', 9); 

27. Set a Maximum Word Count on Post Titles:

आप सभी जानते होंगे कि Google में post title की सिर्फ 68 characters ही show होते हैं. यदि हम title में 68 से ज्यादा character use करेंगे तो search engine में पूरा show नही होगा. जब हम post का title लिखते हैं तो characters को ध्यान में नही रखते हैं और Google SERPs में अधूरा title show होता है।

इसलिए हम जो code बता रहे है, उससे आप post title की maximum length set कर सकते हो. उसके बाद जब आप post में limit से ज्यादा character use करेंगे तो warning show होगा. इसके लिए आप इस code को theme की functions.php में add कर सकते हो। ध्यान रहे यहाँ हमने maximum title length 68 character set किया है, आप अपने according value put कर सकते हो।

function maxWord($title){   global $post; $title = $post->post_title;   if (str_word_count($title) >= 68 ) //set this to the maximum number of words wp_die( __('Error: your post title is over the maximum word count.') );  }  add_action('publish_post', 'maxWord'); 

28. Disable HTML in Comments

कई सारे spammers जब हमारे ब्लॉग में comment submit करते हैं तो उसमें html के द्वारा link insert कर देते हैं ताकि वो हमारे ब्लॉग से backlink प्राप्त कर पाए. By default, कोई भी comment में html का use कर सकता है. आप इसे disable करके spam comment से बच सकते हो.

इसके लिए आप नीचे दिए code को functions.php में add कर दीजिए.

// This will occur when the comment is posted function plc_comment_post( $incoming_comment ) {  // convert everything in a comment to display literally $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);  // the one exception is single quotes, which cannot be #039; because WordPress marks it as spam $incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] ); return( $incoming_comment ); } // This will occur before a comment is displayed function plc_comment_display( $comment_to_display ) { // Put the single quotes back in $comment_to_display = str_replace( ''', "'", $comment_to_display );   return $comment_to_display;  }  add_filter( 'preprocess_comment', 'plc_comment_post', '', 1);  add_filter( 'comment_text', 'plc_comment_display', '', 1);  add_filter( 'comment_text_rss', 'plc_comment_display', '', 1);  add_filter( 'comment_excerpt', 'plc_comment_display', '', 1); 

29. Hide Login Errors in WordPress:

जब हम wordpress site में login करते हैं तो वहाँ wrong password या username enter करने पर error show होता है. जिससे आसानी से password या username guess कर लेते हैं. इसलिए यदि आप अपने site को ज्यादा secure बनाना चाहते हो तो login error को hide कर दीजिए. इसके लिए नीचे दिए गए code को theme की functions.php में add कर दीजिए।

function no_wordpress_errors(){
  return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );

30. Disable Login by Email in WordPress:

By default, WordPress users को email के द्वारा login करने के लिए allow करता है. कोई भी आपके email को आसानी से पता कर सकता है इसलिए यदि आप ब्लॉग को ज्यादा secure बनाना चाहते हो तो इसके लिए login by email को disable कर दीजिए.

इसके लिए आप नीचे दिए code को ब्लॉग theme की functions.php में add कर सकते हो।

remove_filter( 'authenticate', 'wp_authenticate_email_password', 20 ); 

At the Last,
इन सभी snippets की मदद से आप अस्सनी ब्लॉग में extra functions enable कर सकते हो. ज्यादा तर लोग इनके लिए plugin का use करते हैं. जबकि वो आसानी से बिना plugin का भी कर सकते हो. अगर आप अधिक plugin का use करेंगे तो धीरे धीरे आपको समझ मे आ जायेगा कि इसके सिर्फ फायदे नही बल्कि नुकसान भी हैं।

So guys, I hope की आपको यह post अच्छा लगा होगा और यह आपके लिए helpful हुआ होगा. इससे सम्बंधित कोई भी confusion हो तो comment जरूर करें. अगर आपको सच मे यह post पढ़कर अच्छा लगा हो तो इसे अपने मित्रों के साथ social media में share करें।

Like the post?

Also read more related articles on BloggingHindi.com Sharing Is Caring.. ♥️

Sharing Is Caring...

4 thoughts on “WordPress Ke Liye 30 Useful Code Snippets [Full Customize & Control]”

  1. bhai meri blog post ka url search ingne mai show nahi hota

    uski jagah category show hoti hai kaise post ka url show karu.

    Reply
  2. Maine is post ko 2 ghanta padha lekin abhi bhi kuch chize hai jo mai samjh nhi paa rha hun.

    jaise wp-config.php hame kaha aur kaise milega. maine isse follow kiya hai Cpanel>> File manager>> Public HTML>> wp-config.php

    kya ye sahi hai.? agr nhi to aap process bataye

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

×