ওয়ার্ডপ্রেস সেটিংস্ API এর সম্পূর্ণ নির্দেশনা, দ্বিতীয় পর্বঃ সেকশন, ফিল্ড এবং সেটিংস্
() translation by (you can also view the original English article)
যখন ডেভেলপাররা তাদের থীম ও প্লাগীন তৈরী করা শুরু করে, সেখানে তারা মেনু, অপশন এবং ভেলিডেশন ফাংশনের জন্য বিভিন্ন ধরনের পদ্ধতি ব্যাবহার করে। বিষয়টি এই যে, সত্যিই এই কাজটি সঠিকভাবে করতে ওয়ার্ডপ্রেসের মধ্যে কেবলমাত্র একটি উপায় আছেঃ সেটিংস API
এই সিরিজের উদ্দেশ্য কিভাবে ওয়ার্ডপ্রেস সেটিংস API এর উন্নত নির্দেশনা নেওয়া যায় সুতরাং তোমার একটি নির্ধারিতভাবে উল্লেখ থাকলো সঠিক পদ্ধিতে থীম এবং প্লাগীন তৈরী করা।
এই প্রথম লিখাটিতে আমারা দেখবো সেটিং API এবং কেন এটি গুরুত্বপূর্ণ । এখানে, আমারা API দিয়ে যাত্রা শুরু করবো এবং এটি যে সকল সুবিধা দিয়েছে তা কিভাবে নেয়া যায়/কাজে লাগানো যায় ।
আমারা ওয়ার্ডপ্রেস এর মূল ইউনিট - সেকশন, ফিল্ড এবং সেটিং সম্পকে দেখবো- এবং কিভাবে এটি নিজের ওয়ার্ডপ্রেস ড্যাশবোর্ডে যুক্ত করা যায়।
সেকশন, ফিল্ড এবং সেটিং
কোন কোড লেখার আগে, এটা গুরুত্বপূর্ণ যে ওয়ার্ডপ্রেস সেটিংস API এর প্রধান উপাদানগুলার সম্পর্কে বোঝা।
- ফিল্ড হয় একটি পৃথক অপশন যা মেনু পেজের সাথে যুক্ত। ফিল্ড পর্দার প্রকৃত উপাদানের সাথে মিল রাখে। এইটা, একটি ফিল্ড নিয়ন্ত্রিত হয় টেক্সট বক্স, রেডিয় বাটন, চেক্স বক্স ইত্যাদির মাধ্যমে। ফিল্ড ওয়ার্ডপ্রেস ড্যাশবোর্ডে সংরক্ষন করা মান প্রদর্শন করে।
- সেকশন বা বিভাগ হচ্ছে ফিল্ড বা ক্ষেত্রের একটি যৌক্তিক গোষ্ঠী। যখনই আপনি একাধিক ফিল্ড বা ক্ষেত্র নিয়ে কাজ করবেন, আপনি সম্ভবত একসঙ্গে সম্পর্কিত অপশনগুলোকে গোষ্ঠীবদ্ধ করে রাখতে চাইবেন - সেকশন এই গোষ্ঠীটির প্রতিনিধিত্ব করে। উপরন্তু, যদি আপনার কাজ একাধিক প্রশাসনিক পৃষ্ঠাগুলিতে অন্তর্ভুক্ত করে, প্রতিটি বিভাগ বা সেকশন প্রায়ই তাদের নিজস্ব মেনু পৃষ্ঠার অনুরূপ হয় (যদিও আপনি চাইলে বিদ্যমান বিভাগ বা সেকশনেও তাদেরকে যোগ করতে পারেন)।
- ফিল্ড বা ক্ষেত্র এবং সেকশন বা বিভাগ উভয়টি সংজ্ঞায়িত করার পর সেটিংস নিবন্ধিত করা হয়। মনে করুন সেটিংস হচ্ছে ফিল্ড এবং সেকশনের সংমিশ্রন, যা এটার অন্তর্গত।
এই পর্যায়ে, আপনি যদি এখনও কোনও প্রধান উপাদানের ব্যপারে স্পষ্ট ধারণা না পেয়ে থাকেন, তাহলে চিন্তিত হবেন না। আমরা প্রতিটি উপাদানের উদাহরণস্বরূপ সোর্স কোড সহ গভীরভাবে বর্ণনা করতে যাচ্ছি যা এই সবগুলোকে একত্রে যুক্ত করে।
আমাদের সেটিংসের জন্য একটি স্যান্ডবক্স
সেটিংস API দিয়ে প্রোগ্রামিং শুরু করার জন্য আসুন একটি মৌলিক থিম স্থাপন করি যা এই নিবন্ধ এবং সিরিজের বাকি অংশের জন্য ব্যবহার করা যেতে পারে। সব সোর্স কোডগুলো GitHub-এও পাওয়া যাবে।
আপনার স্থানীয় ওয়ার্ডপ্রেস ইনস্টলেশনে, "থিম" ডিরেক্টরিতে যান এবং একটি নতুন, ফাঁকা ডিরেক্টরি তৈরি করুন এবং এটাকে "WordPress-Settings-Sandbox" নামকরন করুন। নিম্নলিখিত তিনটি ফাইল যুক্ত করুন:
- style.css - থিমটির স্টাইলশীট। এটাতে থিমের জন্য সমস্ত মেটা তথ্য অন্তর্ভুক্ত আছে। ওয়ার্ডপ্রেসের জন্য অতীব প্রয়োজনীয় ফাইল
- Index.php - এটা হচ্ছে থিমের জন্য ডিফল্ট টেমপ্লেট। এটি প্রথমে খালি হতে পারে। এটাও ওয়ার্ডপ্রেসের জন্য দরকারী
- functions.php - এই হল সেই জায়গা, যেখানে আমরা আমাদের অধিকাংশ কাজ করবো। আমরা এই অংশটি এই টিউটোরিয়ালের মাধ্যমে পূর্ণ করবো।
নিম্নলিখিত কোড গুলা style.css ফাইলে যুক্ত করুনঃ
1 |
/*
|
2 |
Theme Name: WordPress Settings Sandbox
|
3 |
Theme URI: YOUR URI
|
4 |
Description: A simple theme used to showcase the WordPress Settings API.
|
5 |
Author: YOUR NAME
|
6 |
Author URI: YOUR WEBSITE
|
7 |
Version: 0.1
|
8 |
|
9 |
License:
|
10 |
|
11 |
Copyright 2012 YOUR NAME (YOUR EMAIL ADDRESS)
|
12 |
|
13 |
This program is free software; you can redistribute it and/or modify
|
14 |
it under the terms of the GNU General Public License, version 2, as
|
15 |
published by the Free Software Foundation.
|
16 |
|
17 |
This program is distributed in the hope that it will be useful,
|
18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
GNU General Public License for more details.
|
21 |
|
22 |
You should have received a copy of the GNU General Public License
|
23 |
along with this program; if not, write to the Free Software
|
24 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
25 |
|
26 |
*/
|
এরপর, নিম্নলিখিত কোডগুলা index.php তে যুক্ত করুনঃ
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
<head>
|
4 |
<title>The Complete Guide To The Settings API | Sandbox Theme</title> |
5 |
</head>
|
6 |
<body>
|
7 |
|
8 |
<div id="header"> |
9 |
<h1>Sandbox Header</h1> |
10 |
</div><!-- /#header --> |
11 |
|
12 |
<div id="content"> |
13 |
<p>This is theme content.</p> |
14 |
</div><!-- /#content --> |
15 |
|
16 |
<div id="footer"> |
17 |
<p>© <?php echo date('Y'); ?> All Rights Reserved.</p> |
18 |
</div><!-- /#footer --> |
19 |
|
20 |
</body>
|
21 |
</html>
|
লক্ষ্য করুন, উপরের মার্কআপটি অসাধারণ সহজ এবং আমি এটি থিম ডেভেলপমেন্টের ভিত্তি হিসাবে ব্যবহার করার জন্য সুপারিশ করছি না। এটি ধারাবাহিকভাবে নিবন্ধগুলির জন্য উপযোগী করে তৈরি এবং সহজেই এমন কিছু উপায় প্রদান করে যা আমরা সেটিংস API থেকে মান হিসেবে পাবো।
থিম পরিচালনার স্ক্রিনে, পেইজটি আরেকবার রিফ্রেশ করুন এবং আপনি দেখতে পাবেন নতুন স্যান্ডবক্স থিম আসবে। এগিয়ে যান এবং এটি সক্রিয় করুন।

এবার, আমরা শুরু করতে প্রস্তুত।
আমাদের প্রথম অপশন সেট
উপরোক্ত ইন্ডেক্স টেমপ্লেটটিতে লক্ষ্য করুন, আমরা তিনটি নির্দিষ্ট বিষয়বস্তুভিত্তিক অঞ্চলগুলিকে সংজ্ঞায়িত করেছি: হেডার, কনটেন্ট, এবং ফুটার। সেটিংস এপিআই ব্যবহার করে, তিনটি ক্ষেত্র ধারণকারী একটি "সাধারণ" বিভাগ তৈরি করা যাক, যার প্রতিটি বিষয়বস্তু আমাদের সংজ্ঞায়িত করা নির্দিষ্ট অঞ্চলভিত্তিক।
কোনও কোড লেখার আগে, কি করতে হবে তা সঠিকভাবে তালিকাভুক্ত করার জন্য এটা সর্বদা সহায়ক। এই ক্ষেত্রে, আমাদের নিম্নলিখিত কাজটি করতে হবে:
- একটি সেকশন সংজ্ঞায়িত করুন যা প্রতিটি ক্ষেত্রকে গ্রুপ হিসাবে ব্যবহার করবে।
- তিনটি ক্ষেত্র যোগ করুন - প্রতিটি বিষয়ভিত্তিক অঞ্চলের জন্য একটি করে - যেভাবে উপরের সেকশনে সংজ্ঞায়িত করা হয়েছে
- ওয়ার্ডপ্রেস এপিআই দিয়ে সেটিংস নিবন্ধন করুন।
কোডের জন্য বড় ধরণের ব্লক এড়াতে এবং আমাদের সব ঘাঁটি যাতে কভার করা হয় তা নিশ্চিত করার জন্য, আমরা উপরের প্রতিটি আইটেম বা বিষয়বস্তু পয়েন্ট বাই পয়েন্ট গ্রহণ করবো।
সেকশন তৈরি
অপশনসমূহের "সাধারণ" বিভাগটি খুঁজে পেতে, আমাদের সেটিংস API- এর add_settings_section ফাংশন ব্যবহার করতে হবে। ওয়ার্ডপ্রেস কোডেক্স অনুযায়ী, add_settings_section এর জন্য তিনটি যুক্তি প্রয়োজন:
- ID - এটা হচ্ছে বিশেষ বিভাগের জন্য অনন্য শনাক্তকারী। লক্ষ্য করুন, এই মানটি এই বিভাগের প্রতিটি ক্ষেত্র নিবন্ধনের জন্য ব্যবহার করা হবে। আপনি এটাকে যে নাম দিতে চান তাই দিতে পারেন, কিন্তু আমি আপনাকে এটা যাতে সহজে পড়া যায় এমন পরিচ্ছন্ন রাখতে বলবো।
- Title - যখন ব্যবহারকারীরা আপনার অপশন গুলো নিয়ে কাজ করবে, তখন এটা ওয়ার্ডপ্রেস ড্যাশবোর্ড পৃষ্ঠার উপরের দিকে প্রদর্শিত হবে।
- Callback – এটি একটি ফাংশন নাম যা আমরা এমনভাবে সংজ্ঞায়িত করব যেটা ফাংশনের জন্য পর্দায় পাঠ্য পাঠাবে। এটি বিভিন্ন কার্যকারিতার জন্য ব্যবহার করা যেতে পারে। সহজভাবে, এটি আপনার বিকল্প পৃষ্ঠার জন্য একটি নির্দেশনা বা বিবরণ প্রদান করতে ব্যবহার করা যেতে পারে।
- Page – এই মান বা ভ্যালুটি ওয়ার্ডপ্রেসকে বলে কোন পেইজে আপনার অপশনগুলো প্রদর্শিত হবে। ভবিষ্যতের কোন একটি রচনায়, আমাদের কাস্টম পেইজসমূহে অপশনসমূহ যোগ করতে এটা ব্যবহার করবো। এখন, আমরা শুধুমাত্র বিদ্যমান জেনারেল অপশন পেইজে এটি যোগ করব।
এর মাধ্যমে, চলুন এগিয়ে যাই এবং আমাদের সেকশন সংজ্ঞায়িত করি। নিন্মে লিখিত মন্তব্যসহ কোডটি দেখুন। আমরা এটি আমাদের functions.php ফাইল এ যোগ করছি।
এই সিরিজের কোড স্নিপেট সম্পর্কে একটি কথা মনে রাখবেন: কোডটি শুধুমাত্র কপি এবং পেস্ট করবেন না। প্রতিটি লাইন পড়ার জন্য সময় নিন, দেখুন কিভাবে যুক্তিগুলো আমাদের ব্যবহার করা প্রতিটি API ফাংশন এর সাথে জড়িত হয়, এবং আমরা যা করছি তা পূর্ণরূপে বুঝতে অনুরূপ মন্তব্যগুলো অনুসরণ করুন:
1 |
<?php
|
2 |
|
3 |
/* ------------------------------------------------------------------------ *
|
4 |
* Setting Registration
|
5 |
* ------------------------------------------------------------------------ */
|
6 |
|
7 |
/**
|
8 |
* Initializes the theme options page by registering the Sections,
|
9 |
* Fields, and Settings.
|
10 |
*
|
11 |
* This function is registered with the 'admin_init' hook.
|
12 |
*/
|
13 |
add_action('admin_init', 'sandbox_initialize_theme_options'); |
14 |
function sandbox_initialize_theme_options() { |
15 |
|
16 |
// First, we register a section. This is necessary since all future options must belong to one.
|
17 |
add_settings_section( |
18 |
'general_settings_section', // ID used to identify this section and with which to register options |
19 |
'Sandbox Options', // Title to be displayed on the administration page |
20 |
'sandbox_general_options_callback', // Callback used to render the description of the section |
21 |
'general' // Page on which to add this section of options |
22 |
);
|
23 |
} // end sandbox_initialize_theme_options |
24 |
|
25 |
/* ------------------------------------------------------------------------ *
|
26 |
* Section Callbacks
|
27 |
* ------------------------------------------------------------------------ */
|
28 |
|
29 |
/**
|
30 |
* This function provides a simple description for the General Options page.
|
31 |
*
|
32 |
* It is called from the 'sandbox_initialize_theme_options' function by being passed as a parameter
|
33 |
* in the add_settings_section function.
|
34 |
*/
|
35 |
function sandbox_general_options_callback() { |
36 |
echo '<p>Select which areas of content you wish to display.</p>'; |
37 |
} // end sandbox_general_options_callback |
38 |
|
39 |
?>
|
বোঝা গেলো? সাধারনভাবে বলতে গেলে, এটা খুব একটা দেখা যায় না কিন্তু এর দ্বারা আপনি সেটিংস মেনু পরিচালনা করতে এবং জেনারেলে ক্লিক করতে পারেন। পৃষ্ঠার নীচের দিকে স্ক্রোল করুন এবং আপনার অপশনগুলোর নতুন বিভাগটি দেখতে পাবেন।



আপনি সেটিংস মেনুর অধীনে যেকোনো পৃষ্ঠাতে এই বিভাগটি যুক্ত করতে পারেন। উপরের উদাহরণে, আমরা add_settings_section- এ শেষ প্যারামিটার হিসাবে "general" কে পাস করেছি, কিন্তু যদি আপনি এটি একটি ভিন্ন পৃষ্ঠাতে যোগ করতে চান, তাহলে আপনি একটি ভিন্ন পৃষ্ঠার শিরোনাম প্রদান করতে পারেন। এখানে প্রতিটি সেটিংস পৃষ্ঠা এবং তাদের সংশ্লিষ্ট কী বা চাবির জন্য একটি রেফারেন্স:
- General, "general"
- Writing, "writing"
- Reading, "reading"
- Discussion, "discussion"
- Media, "media"
- Privacy, "privacy"
- Permalinks, "permalink"
ফিল্ডসমূহ যুক্ত করা
এখন আমাদের একটি অধ্যায় বা সেকশন সংজ্ঞায়িত করা হয়েছে, এবার আমরা আপনাদেরকে কয়েকটি অপশনের সঙ্গে পরিচয় করিয়ে দিবো। মনে রাখবেন যে, আমাদের সূচক টেমপ্লেটটিতে আমরা তিনটি নির্দিষ্ট ধারক উপাদানের কথা উল্লেখ করেছি: হেডার, কনটেন্ট এবং ফুটার।
যদিও আমরা এই সিরিজের কোর্স জুড়ে বিভিন্ন অপশনের সাথে পরিচয় করিয়ে দিবো, একইসঙ্গে আজকে আমরা উপরোক্ত প্রতিটি উপাদানের দৃশ্যমানতা টগল করার একটি উপায় প্রবর্তন করবো।
সেটিংস বিভাগের সাথে আমরা যা করেছি তার অনুরূপ, আমি কোনও কোড লেখার আগে অবশ্যই আমাদের যা করতে হবে তা আগে তালিকাভুক্ত করতে চাই। যেহেতু আমরা প্রতিটি কন্টেন্ট এরিয়াই প্রতিস্থাপন করতে যাচ্ছি...
- আমাদের তিনটি বিকল্প প্রয়োজন - একটি কনটেন্টের প্রতিটি ক্ষেত্রের জন্য
- যেহেতু আমরা দৃশ্যমানতা টগল করেছি, আমরা একটি ইন্টারফেসের উপাদান হিসাবে চেকবক্স ব্যবহার করতে পারি
এই মুহুর্তে, আমরা প্রথম সেটিং ক্ষেত্রটি প্রবর্তন করার জন্য প্রস্তুত। এটি করার জন্য, আমরা add_settings_field ফাংশনটি ব্যবহার করবো। এই ফাংশনটি ছয়টি প্যারামিটার (চারটি প্রয়োজনীয়, দুটি ঐচ্ছিক) ব্যবহার করে। তারা নিম্নরূপ:
- ID - প্রকৃত ক্ষেত্রের আইডি। এটি থিমটি জুড়ে মান সংরক্ষণ এবং পুনরুদ্ধার করতে ব্যবহার করা হবে। আমি আপনাকে কোডের পঠনযোগ্যতা উন্নতির জন্য অর্থপূর্ণ কিছু একটা নামকরণ করার জন্য সুপারিশ করছি।
- Title – এই মান বা ভ্যালু প্রশাসন পৃষ্ঠার ক্ষেত্রের বিকল্প একটি শিরোনাম প্রয়োগ করে। এটা পরিষ্কার করা উচিত যে এটা নির্বিশেষ ব্যবহারকারীদের দ্বারা পঠিত হবে।
- Callback – এটা হচ্ছে ফাংশনের নাম যা প্রকৃত ইন্টারফেস উপাদান সরবরাহ করতে ব্যবহৃত হয় যেগুলোর সাথে ব্যবহারকারীরা ব্যবহার করবে।
- Page – আমাদের উল্লিখিত বিভাগের অনুরূপ, এই প্যারামিটারটি নির্দিষ্ট পৃষ্ঠাকে চিহ্নিত করে যেখানে এই অপশনটির থাকা উচিত। আপনি যদি শুধুমাত্র একটি একক অপশন প্রবর্তন করেন, তাহলে আপনি এটা সংজ্ঞায়িত করা একটি বিভাগের পরিবর্তে একটি বিদ্যমান পৃষ্ঠাতে যোগ করতে পারেন।
- Section – এটি add_settings_section ফাংশন ব্যবহার করে আপনার তৈরি করা বিভাগটি উল্লেখ করে। এই মানটি হল এমন আইডি যা আপনি আপনার বিভাগ তৈরি করার সময় উল্লেখ করেছেন। এটি একটি ঐচ্ছিক প্যারামিটার।
- Arguments – এটা হচ্ছে বিভিন্ন যুক্তির সমষ্টি যা কলব্যাক ফাংশনের মাধ্যমে পাস করা হয়। এটা দরকারী যদি এখানে অতিরিক্ত তথ্য থাকে যা আপনি আপনার অপশন এলিমেণ্ট পেশ করার সময় অন্তর্ভুক্ত করতে চান। এটি একটি ঐচ্ছিক পরামিতি।
যা বলছি, চলুন এগিয়ে যাই এবং আমাদের প্রথম সেটিং ফিল্ড বা ক্ষেত্রটি সংজ্ঞায়িত যাক। বিশেষ করে, আমরা হেডার বা শিরোলেখটির দৃশ্যমানতা টগল করার জন্য একটি অপশন প্রবর্তন করব।
প্রথমে, আমরা add_settings_section ফাংশন কলের ঠিক নিচে প্রারম্ভিক ফাংশনের ভিতর add_settings_field কল করি যেটা আমরা আমাদের টিউটোরিয়ালের প্রথম অংশে লিখেছিলাম। প্রতিটি লাইন এবং প্রতিটি অপশনের জন্য মন্তব্য পর্যালোচনা করুন:
1 |
// Next, we will introduce the fields for toggling the visibility of content elements.
|
2 |
add_settings_field( |
3 |
'show_header', // ID used to identify the field throughout the theme |
4 |
'Header', // The label to the left of the option interface element |
5 |
'sandbox_toggle_header_callback', // The name of the function responsible for rendering the option interface |
6 |
'general', // The page on which this option will be displayed |
7 |
'general_settings_section', // The name of the section to which this field belongs |
8 |
array( // The array of arguments to pass to the callback. In this case, just a description. |
9 |
'Activate this setting to display the header.'
|
10 |
)
|
11 |
);
|
পরবর্তীতে, আমরা উপরের ফাংশনে উল্লেখ করা callback সংজ্ঞায়িত করবো। এই কলব্যাকটি চেকবক্স এবং প্রশাসনের পৃষ্ঠার বিবরণ দিতে ব্যবহৃত হয়:
1 |
/**
|
2 |
* This function renders the interface elements for toggling the visibility of the header element.
|
3 |
*
|
4 |
* It accepts an array of arguments and expects the first element in the array to be the description
|
5 |
* to be displayed next to the checkbox.
|
6 |
*/
|
7 |
function sandbox_toggle_header_callback($args) { |
8 |
|
9 |
// Note the ID and the name attribute of the element should match that of the ID in the call to add_settings_field
|
10 |
$html = '<input type="checkbox" id="show_header" name="show_header" value="1" ' . checked(1, get_option('show_header'), false) . '/>'; |
11 |
|
12 |
// Here, we will take the first argument of the array and add it to a label next to the checkbox
|
13 |
$html .= '<label for="show_header"> ' . $args[0] . '</label>'; |
14 |
|
15 |
echo $html; |
16 |
|
17 |
} // end sandbox_toggle_header_callback |
যতক্ষণ পর্যন্ত চেকবক্স সংশ্লিষ্ট থাকে, মন্তব্যগুলোতে মনোযোগ দিন, কিন্তু এমন কিছু অ্যাট্রিবিউট সম্পর্কে খুব বেশি চিন্তা করবেন না যা আপনি চিনেন না (যেমন checked() ফাংশন)। এই সিরিজের পরে , আমরা প্রতিটি ইনপুট উপাদান এবং তাদের সংশ্লিষ্ট সহায়ক ফাংশনের মুখোশ উন্মচোন করবো।
এখন পর্যন্ত, আপনার functions.php ফাইলটি এরকম হওয়া উচিত:
1 |
<?php
|
2 |
|
3 |
/* ------------------------------------------------------------------------ *
|
4 |
* Setting Registration
|
5 |
* ------------------------------------------------------------------------ */
|
6 |
|
7 |
/**
|
8 |
* Initializes the theme options page by registering the Sections,
|
9 |
* Fields, and Settings.
|
10 |
*
|
11 |
* This function is registered with the 'admin_init' hook.
|
12 |
*/
|
13 |
add_action('admin_init', 'sandbox_initialize_theme_options'); |
14 |
function sandbox_initialize_theme_options() { |
15 |
|
16 |
// First, we register a section. This is necessary since all future options must belong to one.
|
17 |
add_settings_section( |
18 |
'general_settings_section', // ID used to identify this section and with which to register options |
19 |
'Sandbox Options', // Title to be displayed on the administration page |
20 |
'sandbox_general_options_callback', // Callback used to render the description of the section |
21 |
'general' // Page on which to add this section of options |
22 |
);
|
23 |
|
24 |
// Next, we will introduce the fields for toggling the visibility of content elements.
|
25 |
add_settings_field( |
26 |
'show_header', // ID used to identify the field throughout the theme |
27 |
'Header', // The label to the left of the option interface element |
28 |
'sandbox_toggle_header_callback', // The name of the function responsible for rendering the option interface |
29 |
'general', // The page on which this option will be displayed |
30 |
'general_settings_section', // The name of the section to which this field belongs |
31 |
array( // The array of arguments to pass to the callback. In this case, just a description. |
32 |
'Activate this setting to display the header.'
|
33 |
)
|
34 |
);
|
35 |
|
36 |
} // end sandbox_initialize_theme_options |
37 |
|
38 |
/* ------------------------------------------------------------------------ *
|
39 |
* Section Callbacks
|
40 |
* ------------------------------------------------------------------------ */
|
41 |
|
42 |
/**
|
43 |
* This function provides a simple description for the General Options page.
|
44 |
*
|
45 |
* It is called from the 'sandbox_initialize_theme_options' function by being passed as a parameter
|
46 |
* in the add_settings_section function.
|
47 |
*/
|
48 |
function sandbox_general_options_callback() { |
49 |
echo '<p>Select which areas of content you wish to display.</p>'; |
50 |
} // end sandbox_general_options_callback |
51 |
|
52 |
/* ------------------------------------------------------------------------ *
|
53 |
* Field Callbacks
|
54 |
* ------------------------------------------------------------------------ */
|
55 |
|
56 |
/**
|
57 |
* This function renders the interface elements for toggling the visibility of the header element.
|
58 |
*
|
59 |
* It accepts an array of arguments and expects the first element in the array to be the description
|
60 |
* to be displayed next to the checkbox.
|
61 |
*/
|
62 |
function sandbox_toggle_header_callback($args) { |
63 |
|
64 |
// Note the ID and the name attribute of the element match that of the ID in the call to add_settings_field
|
65 |
$html = '<input type="checkbox" id="show_header" name="show_header" value="1" ' . checked(1, get_option('show_header'), false) . '/>'; |
66 |
|
67 |
// Here, we will take the first argument of the array and add it to a label next to the checkbox
|
68 |
$html .= '<label for="show_header"> ' . $args[0] . '</label>'; |
69 |
|
70 |
echo $html; |
71 |
|
72 |
} // end sandbox_toggle_header_callback |
73 |
|
74 |
?>
|
এই পর্যায়ে এসে, General Settings পেইজটি রিফ্রেশ করুন। আপনি আপনার চেকবক্সটি "Header" লেবেল এবং পাশে একটি বর্ণনাসহ দেখতে পাবেন।



দুর্ভাগ্যবশত, এটি আসলে ডাটাবেইজে কোন মান সংরক্ষণ করে না।
আমাদের সেটিংস নিবন্ধন করা
আমাদের ফিল্ডের মানগুলো ডাটাবেইজে সংরক্ষণ করার জন্য, ওয়ার্ডপ্রেসে এগুলোকে রেজিস্টার বা নিবন্ধন করা প্রয়োজন। এটা করা খুবই সহজ - এজন্য আমাদেরকে register_setting ফাংশনের সুযোগ নিতে হবে।
এই ফাংশনটি তিনটি যুক্তি বা আর্গুমেন্ট গ্রহন করে (দুটি আবশ্যক, একটি ঐচ্ছিক):
- Option Group – এটি আসলে অপশনগুলোর গ্রুপের নাম। এটা হয় ওয়ার্ডপ্রেসে বিদ্যমান অপশন গ্রুপ হতে পারে অথবা একটি ID যা আমরা আমাদের নিজস্ব সেটিংস বিভাগ তৈরি করার সময় নির্দিষ্ট করেছি। এই যুক্তি বা আর্গুমেন্টটি আবশ্যক।
- Option Name – এটা হচ্ছে আমাদের নিবন্ধনকৃত ফিল্ডটির ID। আমাদের ক্ষেত্রে, আমরা শুধুমাত্র একটি ফিল্ড নিবন্ধন করছি, কিন্তু যদি আমরা একাধিক ফিল্ড নিবন্ধন করি, তাহলে আমাদের যেকোন ফিল্ডের জন্য এই ফাংশনটি কল করতে হবে। আমরা কিছুক্ষনের মধ্যে এ ব্যপারে আরও বিস্তারিত দেখবো। এই আর্গুমেন্ট বা যুক্তিটি আবশ্যক।
- Callback – এই যুক্তিটির এমন একটি ফাংশনের নাম প্রয়োজন যা আমরা ইতিপূর্বে ডাটাবেইজে তথ্য সংরক্ষনের সময় অগ্রাধিকার ভিত্তিতে কল করবো। এই যুক্তিটি এই নিবন্ধের সুযোগের বাইরে কিন্তু সিরিজ শেষ হওয়ার আগে আমরা এটি পরিদর্শন করব। এই যুক্তি বা আর্গুমেন্টটি ঐচ্ছিক।
এই পর্যায়ে, চলুন আমাদের এইমাত্র তৈরিকৃত সেটিংসগুলো রেজিস্টার করা যাক। নীচের কোডটি দেখুন। এই কোডের লাইনটি সরাসরি add_settings_field যেখানে কল করা হয়েছে তার নীচে যোগ করুন, এই প্রবন্ধের উপরিভাগে আমরা যেই প্রারম্ভিক ফাংশনটি সংজ্ঞায়িত করেছি। এটা যুক্তির বিবেচনায় এই নিবন্ধের সব স্নিপেট থেকে অনুসরণ করা সবচেয়ে সহজ:
1 |
// Finally, we register the fields with WordPress
|
2 |
register_setting( |
3 |
'general', |
4 |
'show_header'
|
5 |
);
|
এবার এটা পরীক্ষা করুন - চেকবক্সে টিক চিহ্ন দিন, 'Save Changes,' এ ক্লিক করুন, এবং লক্ষ্য করুন পেইজটি রিফ্রেশ করার পর অপশনগুলো পরিবর্তন হয়ে গেছে। এবার চেকবক্স থেকে টিক চিহ্ন তুলে দিন, সেভ করুন, এবং দেখুন সেভ হয়ে গেছে।



খুব সহজ, তাই না?
চুড়ান্ত দুটি অপশন যুক্ত করা
আমাদের এখন কন্টেন্ট এরিয়া ও ফুটার এরিয়ার দৃশ্যমানতা টগল করার জন্য বেশ কিছু অপশন প্রয়োজন। আমরা যেভাবে হেডারটির টগলিং অপশন সেটআপ করেছি, এটা ঠিক সেরকমই।
প্রথমে, চলুন কন্টেন্ট এরিয়ায় প্রদর্শনের জন্য ফিল্ড সংজ্ঞায়িত করা যাক। এটা add_settings_field এর প্রথম কল হিসেবে যোগ হবে:
1 |
add_settings_field( |
2 |
'show_content', |
3 |
'Content', |
4 |
'sandbox_toggle_content_callback', |
5 |
'general', |
6 |
'general_settings_section', |
7 |
array( |
8 |
'Activate this setting to display the content.'
|
9 |
)
|
10 |
);
|
এবং চলুন কলব্যাক ফাংশন সেটআপ করা যাক:
1 |
function sandbox_toggle_content_callback($args) { |
2 |
|
3 |
$html = '<input type="checkbox" id="show_content" name="show_content" value="1" ' . checked(1, get_option('show_content'), false) . '/>'; |
4 |
$html .= '<label for="show_content"> ' . $args[0] . '</label>'; |
5 |
|
6 |
echo $html; |
7 |
|
8 |
} // end sandbox_toggle_content_callback |
এরপর, চলুন ফুটার এরিয়ায় প্রদর্শনের জন্য ফিল্ডটি সংজ্ঞায়িত করি:
1 |
add_settings_field( |
2 |
'show_footer', |
3 |
'Footer', |
4 |
'sandbox_toggle_footer_callback', |
5 |
'general', |
6 |
'general_settings_section', |
7 |
array( |
8 |
'Activate this setting to display the footer.'
|
9 |
)
|
10 |
);
|
এবং এই ফিল্ডের জন্যও কলব্যাক সেটআপ করুন:
1 |
function sandbox_toggle_footer_callback($args) { |
2 |
|
3 |
$html = '<input type="checkbox" id="show_footer" name="show_footer" value="1" ' . checked(1, get_option('show_footer'), false) . '/>'; |
4 |
$html .= '<label for="show_footer"> ' . $args[0] . '</label>'; |
5 |
|
6 |
echo $html; |
7 |
|
8 |
} // end sandbox_toggle_footer_callback |
অবশেষে, চলুন ওয়ার্ডপ্রেসের সাথে এই দুটি নতুন ফিল্ড নিবন্ধন করা যাক। এই দুটি ফাংশন কল প্রবন্ধের শুরুতে সংজ্ঞায়িত প্রারম্ভিক ফাংশনের শেষে যুক্ত হবে।
1 |
register_setting( |
2 |
'general', |
3 |
'show_content'
|
4 |
);
|
5 |
|
6 |
register_setting( |
7 |
'general', |
8 |
'show_footer'
|
9 |
);
|
functions.php সর্বশেষ সংস্করণটি দেখতে এমন হবে:
1 |
<?php
|
2 |
|
3 |
/* ------------------------------------------------------------------------ *
|
4 |
* Setting Registration
|
5 |
* ------------------------------------------------------------------------ */
|
6 |
|
7 |
/**
|
8 |
* Initializes the theme options page by registering the Sections,
|
9 |
* Fields, and Settings.
|
10 |
*
|
11 |
* This function is registered with the 'admin_init' hook.
|
12 |
*/
|
13 |
add_action('admin_init', 'sandbox_initialize_theme_options'); |
14 |
function sandbox_initialize_theme_options() { |
15 |
|
16 |
// First, we register a section. This is necessary since all future options must belong to one.
|
17 |
add_settings_section( |
18 |
'general_settings_section', // ID used to identify this section and with which to register options |
19 |
'Sandbox Options', // Title to be displayed on the administration page |
20 |
'sandbox_general_options_callback', // Callback used to render the description of the section |
21 |
'general' // Page on which to add this section of options |
22 |
);
|
23 |
|
24 |
// Next, we will introduce the fields for toggling the visibility of content elements.
|
25 |
add_settings_field( |
26 |
'show_header', // ID used to identify the field throughout the theme |
27 |
'Header', // The label to the left of the option interface element |
28 |
'sandbox_toggle_header_callback', // The name of the function responsible for rendering the option interface |
29 |
'general', // The page on which this option will be displayed |
30 |
'general_settings_section', // The name of the section to which this field belongs |
31 |
array( // The array of arguments to pass to the callback. In this case, just a description. |
32 |
'Activate this setting to display the header.'
|
33 |
)
|
34 |
);
|
35 |
|
36 |
add_settings_field( |
37 |
'show_content', |
38 |
'Content', |
39 |
'sandbox_toggle_content_callback', |
40 |
'general', |
41 |
'general_settings_section', |
42 |
array( |
43 |
'Activate this setting to display the content.'
|
44 |
)
|
45 |
);
|
46 |
|
47 |
add_settings_field( |
48 |
'show_footer', |
49 |
'Footer', |
50 |
'sandbox_toggle_footer_callback', |
51 |
'general', |
52 |
'general_settings_section', |
53 |
array( |
54 |
'Activate this setting to display the footer.'
|
55 |
)
|
56 |
);
|
57 |
|
58 |
// Finally, we register the fields with WordPress
|
59 |
register_setting( |
60 |
'general', |
61 |
'show_header'
|
62 |
);
|
63 |
|
64 |
register_setting( |
65 |
'general', |
66 |
'show_content'
|
67 |
);
|
68 |
|
69 |
register_setting( |
70 |
'general', |
71 |
'show_footer'
|
72 |
);
|
73 |
|
74 |
} // end sandbox_initialize_theme_options |
75 |
|
76 |
/* ------------------------------------------------------------------------ *
|
77 |
* Section Callbacks
|
78 |
* ------------------------------------------------------------------------ */
|
79 |
|
80 |
/**
|
81 |
* This function provides a simple description for the General Options page.
|
82 |
*
|
83 |
* It is called from the 'sandbox_initialize_theme_options' function by being passed as a parameter
|
84 |
* in the add_settings_section function.
|
85 |
*/
|
86 |
function sandbox_general_options_callback() { |
87 |
echo '<p>Select which areas of content you wish to display.</p>'; |
88 |
} // end sandbox_general_options_callback |
89 |
|
90 |
/* ------------------------------------------------------------------------ *
|
91 |
* Field Callbacks
|
92 |
* ------------------------------------------------------------------------ */
|
93 |
|
94 |
/**
|
95 |
* This function renders the interface elements for toggling the visibility of the header element.
|
96 |
*
|
97 |
* It accepts an array of arguments and expects the first element in the array to be the description
|
98 |
* to be displayed next to the checkbox.
|
99 |
*/
|
100 |
function sandbox_toggle_header_callback($args) { |
101 |
|
102 |
// Note the ID and the name attribute of the element match that of the ID in the call to add_settings_field
|
103 |
$html = '<input type="checkbox" id="show_header" name="show_header" value="1" ' . checked(1, get_option('show_header'), false) . '/>'; |
104 |
|
105 |
// Here, we will take the first argument of the array and add it to a label next to the checkbox
|
106 |
$html .= '<label for="show_header"> ' . $args[0] . '</label>'; |
107 |
|
108 |
echo $html; |
109 |
|
110 |
} // end sandbox_toggle_header_callback |
111 |
|
112 |
function sandbox_toggle_content_callback($args) { |
113 |
|
114 |
$html = '<input type="checkbox" id="show_content" name="show_content" value="1" ' . checked(1, get_option('show_content'), false) . '/>'; |
115 |
$html .= '<label for="show_content"> ' . $args[0] . '</label>'; |
116 |
|
117 |
echo $html; |
118 |
|
119 |
} // end sandbox_toggle_content_callback |
120 |
|
121 |
function sandbox_toggle_footer_callback($args) { |
122 |
|
123 |
$html = '<input type="checkbox" id="show_footer" name="show_footer" value="1" ' . checked(1, get_option('show_footer'), false) . '/>'; |
124 |
$html .= '<label for="show_footer"> ' . $args[0] . '</label>'; |
125 |
|
126 |
echo $html; |
127 |
|
128 |
} // end sandbox_toggle_footer_callback |
129 |
|
130 |
?>
|
এবার General Settings পেইজটি রিফ্রেশ করুন এবং লক্ষ্য করুন আপনার ইতিমধ্যেই তিনটি সম্পূর্ণ ফাংশনাল চেকবক্স তৈরি হয়েছে।



API বুঝতে পারা
অপশন থেকে লাভ কি যদি আমরা এগুলোকে আমাদের থিমে অথবা প্লাগিনে ব্যবহার করতে না পারি? এজন্য আমাদের ফিল্ডের মানগুলো পড়তে সমর্থ হওয়ার পাশাপাশি নতুন অপশনগুলো ঠিকভাবে ম্যানেজ করাও প্রয়োজন।
এটা করতে আমাদেরকে get_option ফাংশন ব্যবহার করতে হবে। এই ফাংশনটি দুটি আর্গুমেন্ট গ্রহন করে (একটি আবশ্যক, অন্যটি ঐচ্ছিক):
- Option ID – এই আর্গুমেন্টটি হচ্ছে নির্ধারিত ফিল্ডের ID যার মান আপনি উদ্ধার করতে যাচ্ছেন। এই আর্গুমেন্টটি আবশ্যক।
- Default Option – এটা হচ্ছে নির্ধারিত মান যা ফাংশনটি ফেরত নিয়ে আসবে, যদি ফাংশনটি একটি শূন্য মান নিয়ে আসে (যেমন কোনও ক্ষেত্রে যদি অপশনটি ডাটাবেসে না পাওয়া যায়)। এই যুক্তিটি ঐচ্ছিক।
প্রথমত, চলুন হেডারের দৃশ্যমানতা টগল করতে চেষ্টা করি। এই নিবন্ধের প্রথম দিকে আমরা যেই ইনডেক্স টেম্পলেটটি তৈরি করেছি, সেটাতেই হেডারের ID সহ এলিমেণ্টটি শনাক্ত করুন। এটা নীচের মত দেখাবে:
1 |
<div id="header"> |
2 |
<h1>Sandbox Header</h1> |
3 |
</div><!-- /#header --> |
পরবর্তীতে, শর্ত সাপেক্ষে get_option ফাংশনটি কল করা যাক। যদি এই শর্ত সাপেক্ষে সত্যায়ন করা হয় (অর্থাৎ, General Settings পৃষ্ঠায় অপশনটি চেক করা হয়েছে), তাহলে উপাদানটি প্রদর্শন করা হবে; অন্যথায়, প্রদর্শন করা হবে না।
1 |
<?php if(get_option('show_header')) { ?> |
2 |
<div id="header"> |
3 |
<h1>Sandbox Header</h1> |
4 |
</div><!-- /#header --> |
5 |
<?php } // end if ?> |
এরপরে, General Settings পৃষ্ঠায় ভরসা রেখে, হেডার উপাদানটি লুকানোর জন্য অপশনটি পরীক্ষা করুন, এবং আপনার হোমপেজ রিফ্রেশ করুন। হেডার এলিমেণ্টটি আর দেখা যাচ্ছে না।
এই পর্যায়ে এসে, কনটেন্ট এবং ফুটার এলিমেন্টের জন্য প্রক্রিয়াটি পুনরাবৃত্তি করা একটি সহজ কাজ। আমরা কন্টেন্ট এবং ফুটার এলিমেন্টের শর্তাবলী এক সঙ্গে করবো যা get_option কল এর ফলাফল মূল্যায়ন করবে।
এক নজরে দেখে নিন:
1 |
<?php if(get_option('show_content')) { ?> |
2 |
<div id="content"> |
3 |
<p>This is theme content.</p> |
4 |
</div><!-- /#content --> |
5 |
<?php } // end if ?> |
6 |
|
7 |
<?php if(get_option('show_footer')) { ?> |
8 |
<div id="footer"> |
9 |
<p>© <?php echo date('Y'); ?> All Rights Reserved.</p> |
10 |
</div><!-- /#footer --> |
11 |
<?php } // end if ?> |
General Settings পেইজে আরেকবার যান, প্রতিটি চেকবক্স টগল করুন, এবং থিম পেইজটি রিফ্রেশ করুন। আপনার উপাদানগুলির একে অপরের থেকে স্বাধীনভাবে টগল হওয়া উচিত।
পরবর্তীতে, মেনু পৃষ্ঠাগুলি
এখন পর্যন্ত, এটুকুই! আমরা ওয়ার্ডপ্রেসে নতুন বিভাগ, ক্ষেত্র এবং সেটিংস প্রবর্তনের জন্য প্রয়োজনীয় সব ফাংশনগুলো এক নজরে দেখবো। অবশ্যই, এখানে আরো অনেক কিছু আছে।
পরের প্রবন্ধে, আমরা কীভাবে ওয়ার্ডপ্রেস মেনুতে কাস্টম মেনু আইটেমগুলি যোগ করতে পারি এবং কিভাবে আমরা ওয়ার্ডপ্রেস ড্যাশবোর্ডে আমাদের নিজস্ব পেজগুলি প্রবর্তন করতে পারি তা দেখবো।
সম্পর্কিত রিসোর্সসমূহ
আমরা এই নিবন্ধটি অনেক উপাদান বর্ণনা করেছি। এখানে এই নিবন্ধটি জুড়ে ব্যবহৃত সবকিছুর জন্য একটি রেফারেন্স প্রদান করা হলো।