من به توسعه وردپرس علاقه زیادی دارم. چالشهای فنیش رو دوست دارم و اخیرا برای حل مسائل در کد زدن از رفرنسهای وردپرس برای پرامپنویسی استفاده میکنم.
بدیهی که باید دانش و درک اولیه برای مفاهیم از جمله فانکشها و نحوه عملکرد اونها وجود داشته باشه تا بشه خیلی دقیق پرامپت نوشت. یک نمونه تمرین میگذارم که شاید براتون جالب باشه. توی این تمرین قرار بر این بوده که در منو جنرال وردپرس یک بخشی اضافه بشه برای وارد کردن API گوگل.
توی پرامپتنویسی باید به این ۵ مورد توجع داشت:
Act like a senior php developer.
Well, the issue we want to address is related to the Settings API
It is in WordPress.
We have decided to add items to the settings section in the admin menu in the first part, which is general settings, at the bottom of the page. In the General section, the last option is related to Week Starts On. We want to add something after this. For this option, consider the following:
1. The title of this option is: Google API Settings.
2. This option includes two form fields.
3. The first field is called secret key, which the user enters its values.
3. The second field is the API key that the user enters.
4. User can enter values as letters and numbers or whatever.
5. The values entered by the user in these forms are stored in the database. (probably get_option() or add_option() should be used, I don't know exactly and I want you to choose the best way).
In the reviews I have done for this case, the WordPress reference is usually referred to, which provides the following to perform the operation. This Function Reference includes the following:
register_setting()
add_settings_section()
add_settings_field()
Execute this request according to your discretion and very cleanly.
خب خروجی چطور بوده؟ عالی و خیلی دقیق عمل کرده. خروجی:
// Register the settings function myplugin_register_settings() { register_setting('general', 'google_api_settings', 'myplugin_sanitize_callback'); // Add a new section to the General Settings page add_settings_section( 'google_api_settings_section', 'Google API Settings', 'myplugin_section_callback', 'general' ); // Add fields to the new section add_settings_field( 'google_api_secret_key', 'Secret Key', 'myplugin_secret_key_callback', 'general', 'google_api_settings_section' ); add_settings_field( 'google_api_key', 'API Key', 'myplugin_api_key_callback', 'general', 'google_api_settings_section' ); } // Sanitize and validate input function myplugin_sanitize_callback($input) { // Sanitization logic here if needed return $input; } // Section callback function myplugin_section_callback() { echo '<p>Enter your Google API settings below:</p>'; } // Secret Key field callback function myplugin_secret_key_callback() { $value = get_option('google_api_settings')['google_api_secret_key']; echo "<input type='text' name='google_api_settings[google_api_secret_key]' value='$value' />" } // API Key field callback function myplugin_api_key_callback() { $value = get_option('google_api_settings')['google_api_key']; echo "<input type='text' name='google_api_settings[google_api_key]' value='$value' />" } // Hook into the settings API add_action('admin_init', 'myplugin_register_settings');