How To Add Custom Shortcode in WordPress Tutorial in Hindi,2024

| | 24 Minutes Read

आज हम बात करेंगे WordPress Custom Shortcodes Kaise Banaye के बारे में और सीखेंगे की कैसे आप खुद से WordPress में custom shortcodes create करके अपने बड़े-बड़े कामो को बहुत ही जल्दी और आसान तरीके से कर सकते है WordPress Tutorial in Hindi.

Shortcodes को WordPress 2.5 में introduced किया गया था और उसमे बाद से बहुत सी plugins में इसका use शुरू हो गया लेकिन फिर भी बहुत ही कम bloggers shortcodes का सही तरीके से use करना जानते है और सिर्फ programming knowledge बाले bloggers ही ये बात जानते है की वो खुद के WordPress shortcodes भी बना सकते हैं.

लेकिन ऐसा कोई जरुरी नही की WordPress shortcodes को use करने के लिए आपको programming knowledge होनी चाइये. अगर आप भी WordPress के इस powerful features यानी shortcodes को अपने blogs में use करना चाहते हो तो इस post को बहुत आराम से और step by step आखिर तक read करियेगा.

WordPress Custom Shortcodes Kya Hai

ऐसा कभी न कभी हर bloggers के साथ होता है की उन्हें अपनी सभी या कुछ pages या posts में कुछ specialized content जैसे की ads, signup form, piece of text या कुछ other common elements को बार-बार manually add करना पड़ता है.

जब भी हम किसी same piece of code या text को बार-बार type या copy and paste करते है तो वो काम बहुत ज्यादा boring और time wasting हो जाता है, इस problem का सबसे आसान solution है WordPress custom shortcodes.

Shortcodes क्या है ये तो आप इसके name से भी समझ सकते हो, की किसी बढ़ी-बढ़ी lines of code की जगह एक छोटा सा (short) code लिखना यानी किसी बड़े-बड़े code के shortcut को shortcodes कहा जाता है.

Shortcodes कुछ इस तरह [shortcode] का tag होता है जिसे आप अपनी posts या pages में कहीं भी add कर सकते है और फिर जब भी आप या visitors उस post या page को अपने browser पर देखेंगे तो उन्हें उस shortcodes वाली जगह पर वो content नजर आयेगा जिसका वो shortcodes था.

अगर आप अपने browser पर right click या Ctrl+U करके उस post या page का view-source यानी back-end code देखेंगे तो आप shortcodes वाली जगह पर full lines of code नजर आएगा जिसके लिए आपने shortcodes बनाया था.

How To Add Custom Shortcode in WordPress

जैसा की आप जानते ही हैं की WordPress CMS को PHP programming language से बनाया गया है और इसलिए WordPress में use होने वाली सभी चीजो को जैसे themes, plugins इत्यादि को PHP की help से code करके बनाया जाता है.

इसलिए तरह WordPress custom shortcodes create करने के लिए हम PHP की ही use करेंगें और अगर आपके PHP programming language नही भी आती है तो कोई चिंता करने की बात नही है क्योंकि मैं आपको हर steps को details से explain करूँगा.

WordPress custom shortcodes बनाने के लिए आपको simple 3 steps को follow करना होता है. नीचें दिए गये steps को हम आगे details से समझेंगे.

1. सबसे पहले आपको अपने theme की functions.php में एक callback function create करना होता है जिसमे वो सभी content (code) define किया जाता है जो आप अपने shortcode वाली जगह पर show कराना चाहते हो.

2. Function create करने के बाद आपको उस function को WordPress shortcodes API के add_shortcode function की help से as a shortcode register करना होता है.

3. Last step में आप जहाँ-जहाँ shortcode use करना चाहते हो वहां [yourshortcode] tag को type करना होता है.

Warning Note: जैसा की आपने ऊपर पढ़ा की की आपको अपनी theme की functions.php file में कुछ code करना होगा इसलिए मैं आपको suggest करूँगा की पहले आप original functions.php file के backup ले लीजियेगा क्योंकि अगर आपने कोई छोट्टी सी mistake कर दी तो आपकी site बंद हो सकती है.

इसके अलावा ये कर सकते हैं की आप functions.php में changes WordPress dashboard से ना करके cPanel के file manager में जाकर करें जिससे की कोई भी mistake होने पर आप उस code को remove कर सकें.

WordPress Tutorial in Hindi

अब बहुत से readers अगर इस warning note को पढ़ कर शायद डर गयें हो की कहीं उनकी site बंद न हो जाये तो मैं आपको बता दूँ की अगर आप इस post को अच्छे से follow करोगे तो कुछ नही होगा और कभी भी कुछ नया करने से डरना नही चाइये. जब तक हम गलती नही करेंगे तब तक हम कुछ नया नही सीखेंगे.

Ultimate Solution: अगर आप बिलकुल भी risk नही लेना चाहते हो तो आप WordPress से related किसी नही new experiment के लिए WordPress Testing Environment को use कर सकते हो. अधिक जानकारी के लिए आप दिए गये link पर click करके post को पढ़ सकते हो.

Step 1: Create WordPress Custom Shortcodes Function in Functions.php

सबसे पहले आप अपने WordPress dashboard में appearance >> editor के अंदर या cPanel के file manager में  public_html >> wp-content >> themes >> yourtheme में functions.php को edit करिए.

अब functions.php file में WordPress custom shortcodes बनाने सबसे पहले आपको एक function create करना होगा.

PHP में function create करने के लिए सबसे function ही लिखा जाता है और फिर उस function का एक unique name with parentheses bracket ( )के साथ लिखा जाता है और फिर उनके साथ curly brackets { } की एक body बनाईं जाती है जिसके बीच में हम अपना code करते है.

function your_function_name( )
{
     // your code here
}

नीचें code snippet में मैंने एक function create किया है जिसको मैंने gyanians_message name दिया है और फिर उस function में मैंने एक message को return कराया है, यानी जहाँ ये function call होगा वहां पर ये message return हो जायेगा.

function gyanians_message( )
{
     return "Hello <b>Gyanians</b>, तो कैसे हो आप?";
}

Step 2: Register WordPress Custom Shortcodes Function in Functions.php

Function create करने बाद आपको उस function को WordPress shortcodes API के add_shortcode function की help से as a shortcode register करना होगा.

Register करने के लिए पहले हम अपनी मर्जी से अपने shortcode को कोई unique name दे देते है और फिर उसके बाद step 1 में created function name को लिखा जाता है.

add_shortcode('shortcode_name','function_name');

नीचें code snippet में मैंने step 1 में created function gyanians_message को WordPress में as a shortcode register किया है और shortcode को मैंने msg name दिया है.

add_shortcode('msg','gyanians_message');

Step 3: Add WordPress Custom Shortcodes Function in Posts and Pages

Last 2 steps में हमने function को create करके use WordPress में as custom shortcode register भी कर दिया और अब मैंने दोनों steps के code को एक साथ लिख दिया है जिसे आप नीचें code snippet में देख सकते हो.

function gyanians_message( )
{
return "Hello <b>Gyanians</b>, तो कैसे हो आप?";
}
add_shortcode('msg','gyanians_message');

Functions.php में अपना custom shortcodes function create करने के बाद आप अपनी functions.php file को save (update) कर लीजियेगा.

अब जहाँ भी आपको WordPress custom shortcodes को use करना करना है वहां आपको सिर्फ ये [yourshortcode] tag add करना होगा जैसे की मेरे case में मैं [msg] tag type करूँगा.

 [msg]
Custom Shortcode Kaise Create Kare

Hello Gyanians, मैं आशा करता हूँ आपने ऊपर बताये गये simple shortcode को WordPress में add करके देख लिया होगा. अब हम WordPress shortcodes की इस post को थोडा advanced level पर ले चलते है. अब हम सीखेंगे की कैसे आप shortcodes with attributes को WordPress में add और use कर सकते हो?

Shortcodes with Attributes WordPress में कैसे Add करते है?

Shortcodes में attributes add करने करने के लिए आपको सिर्फ इतना करना होता है की posts या pages में shortcode के square brackets [] में shortcode name के साथ attributes यानी property और उसकी value को भी type करना होता है.

[shortcode attributes="value"]

Example के तौर पर मान लीजये मुझे अपनी किसी post में lorempixel.com site की help से कोई random pictures add करनी है अलग-अलग size (width x height) में.

इसके लिए मैं अपने WordPress में custom shortcode add कर रहा हूँ. जिसमें मैं attributes की help से ये बता सकूं की मुझे किस size की random image अपने blog में add करनी है.

function random_picture($atts) 
{
	extract(shortcode_atts(array(
		'width'=>400,
		'height'=>250,
	),$atts));
	return '<img src="http://lorempixel.com/'.$width.'/'.$height.'"/>';
}
add_shortcode('ranpic','random_picture');

हमने WordPress custom shortcodes के function में default attributes भी set कर दिए यानी अगर हम shortcode को blog में add करते वक्त अगर attributes pass नही करेंगे तो default attributes work करेंगे. आप नीचें दिए गये दोनों तरीको से अपनी blog posts में shortcode add कर सकते हो.

[ranpic]
or
[ranpic width="700" height="400"]
How To Add Shortcode in Widget

अगर WordPress Custom shortcodes आपके blog के widgets area show नही हो रहें है तो उसके लिए आपको simple अपनी theme की functions.php file में 1 line code add करना होगा और फिर उसके बाद आप text widgets में shortcode tag [yourshortcode] add कर सकते हो.

add_filter('widget_text', 'do_shortcode');

ऐसे ही अगर आप अपनी WordPress theme की template files में WordPress custom shortcodes add करना चाहते हो तो उसके लिए आपको उस template file में उस जगह simple 1 line code add करना जहाँ आप shortcode content show करना चाहते हो.

do_shortcode("[yourshortcode]");
Shortcodes Tag/Content Kaise Hide Kare

कुछ plugins और themes आपको किसी तरह की styles या features add करने के लिए shortcodes provide करती है जिन्हें आप manually अपने posts या pages में add कर देते हो लेकिन shortcodes use करने में problem ये है की जब आप वो plugins या themes delete कर देते हो.

तो उनके shortcodes आपकी posts और pages में से remove नही होते है और वो posts और pages में show होने लगते है जो आपके blogs readers को बहुत अजीब लगते है इसलिए आपको उन्हें manually हटाने पड़ता है लेकिन बहुत सारी posts और pages से shortcodes हटाने एक time wasting और difficult काम है

इसके अलावा अगर आपने भी WordPress custom shortcodes को अपनी posts या pages में add कर रखा है और आप कभी भी उसके content को और tag को हमेशा के लिए या कुछ time के लिए hide करना चाहते हो तो उसके लिए भी आपको अपनी theme की functions.php file में 1 line code add करना होता है.

add_shortcode('yourshortcode','__return_false');
Adsense Ke Liye Shortcode Kaise Banaye

अगर आप अपनी blog में कहीं भी adsense ads लगाना चाहते हो तो उसके लिए भी आप WordPress shortcodes बना सकते हो. मैंने नीचें code दिया है जिसे आप अपने adsense code के साथ change करके अपने blog में use कर सकते हो.

आप जहाँ पर भी adsense ads लगाना चाहते हो आपको बस उस जगह ये shortcode tag [adsense] add करना होगा.

function showads() {
    return '<script type="text/javascript"><!--
    google_ad_client = "pub-5643220125174754";
    google_ad_slot = "4668915978";
    google_ad_width = 468;
    google_ad_height = 60;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
';
}
add_shortcode('adsense', 'showads');

My Every Posts Last Paragraph using WordPress Custom Shortcodes

Gyanians पर हर posts के last में एक same paragraph होता है जिसमे post का title भी होता है इसे मैं custom shortcode की हेल्प से ही posts में add करता हूँ. इसका code example भी मैंने नीचें दिया है जिसे आप अपने blog पर भी use कर सकते हो.

function gyanians_msg()
{
$post_url = get_permalink($post->ID);
$post_title = get_the_title($post->ID);
$share = '<a href="'.$post_url.'">'.$post_title.'</a>';
$msg="Hello <b>Gyanians</b>, आशा करता हूँ की आपको ये ". $share ." post पसंद आई होगी. अगर आपको इस post से related कोई सवाल या सुझाव है तो नीचे comment करें और इस post को अपने दोस्तों के साथ जरुर share करें.";
return $msg;
}
add_shortcode('mymsg','gyanians_msg');

आशा करता हूँ की आपको ये WordPress Custom Shortcodes Kaise Banaye का WordPress Tutorial in Hindi post पसंद आई होगी. अगर आपको इस post से related कोई सवाल या सुझाव है तो नीचे comment करें और इस post को अपने दोस्तों के साथ जरुर share करें.

Author:

Hello!! दोस्तों मेरा नाम Aryan है. मैं gyanians.com का Writer हूँ. मुझे हिंदी में Educational Blogs लिखना पसंद है. मैं इन Blogs की मदद से आप तक सभी तरह के Wordpress से जुड़ी जानकारी एवं Technical Course Exams की जानकारी पहुंचाना चाहता हूँ.मेरा आपसे निवेदन है की आप इसी तरह मेरा सहयोग देते रहें और ज़्यादा से ज़्यादा लोगों के साथ मेरे लिखे Content को शेयर करें.मैं आप सभी के लिए Latest जानकारियाँ उपलब्ध करवाता रहूँगा.

Questions Answered: (10)

Leave a Reply to Dharmendra Yadav Cancel reply

Your email address will not be published. Required fields are marked *