1. Home
  2. WordPress
  3. Plugin Development
  4. Basic
  5. প্লাগিন সেটিংস পেজ

প্লাগিন সেটিংস পেজ

প্লাগিন কাস্টোমাইজেশনের জন্য আপনি এক্সটেনশন অপশন ব্যবহার করতে পারেন যা একটি ইউজার ইন্টারফেস প্রদান করে যাতে ওই অপশনগুলো কাস্টমাইজ করা যেতে পারে। নিচের উদাহরণে আমি একটি প্লাগিন কাস্টোমাইজেশন ডেমোনস্ট্রেশন দিচ্ছি যেখানে আমরা একটি ইনপুট ফিল্ড এবং একটি বাটন যোগ করবো যা প্লাগিন সেটিংস পেজে প্রদর্শিত হবে।

Folder Structure:

প্লাগিন ফোল্ডার স্ট্রাকচারে নিম্নলিখিত ফাইলগুলো থাকবে:

scroll-to-top-vanilla-js/
├── scroll-to-top-vanilla-js.php
├── scroll-to-top.js
└── scroll-to-top.css

Step-by-Step Tutorial:

1. Plugin Activation and Deactivation Hooks:

First, let’s add the activation and deactivation hooks to enqueue and dequeue scripts and styles:

<?php
/*
Plugin Name: Scroll to Top VanillaJS
Plugin URI: http://example.com/scroll-to-top-vanilla-js
Description: একটি বেসিক ওয়ার্ডপ্রেস প্লাগিন যা স্ক্রল টু টপ ফাংশনালিটি যোগ করে।
Version: 1.0
Author: আপনার নাম
Author URI: http://example.com
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: scroll-to-top-vanilla-js
Domain Path: /languages
*/

// Enqueue scripts and styles on plugin activation
register_activation_hook(__FILE__, 'scroll_to_top_activate');

// Remove scripts and styles on plugin deactivation
register_deactivation_hook(__FILE__, 'scroll_to_top_deactivate');

// Activation callback function
function scroll_to_top_activate() {
    // Enqueue scripts and styles
    wp_enqueue_script('scroll-to-top-js', plugins_url('scroll-to-top.js', __FILE__), array(), '1.0', true);
    wp_enqueue_style('scroll-to-top-css', plugins_url('scroll-to-top.css', __FILE__));
}

// Deactivation callback function
function scroll_to_top_deactivate() {
    // Remove scripts and styles
    wp_dequeue_script('scroll-to-top-js');
    wp_dequeue_style('scroll-to-top-css');
}

2. Plugin Settings Page:

Next, we’ll create a settings page to allow customization of the background image and background color:

// Register plugin options page
add_action('admin_menu', 'scroll_to_top_add_admin_menu');
function scroll_to_top_add_admin_menu() {
    // Add a submenu page under Settings menu
    add_options_page(
        'Scroll to Top Settings',     // Page title
        'Scroll to Top Settings',     // Menu title
        'manage_options',             // Capability required to access menu
        'scroll_to_top_settings',     // Menu slug (unique identifier)
        'scroll_to_top_render_settings_page' // Callback function to render the settings page
    );
}

// Render settings page callback function
function scroll_to_top_render_settings_page() {
    ?>
    <div class="wrap">
        <h2>Scroll to Top Settings</h2>
        <form method="post" action="options.php">
            <?php
            // Add nonce for security
            settings_fields('scroll_to_top_settings');
            // Output settings sections
            do_settings_sections('scroll_to_top_settings');
            // Add submit button
            submit_button('Save Settings');
            ?>
        </form>
    </div>
    <?php
}

// Register and define the settings
add_action('admin_init', 'scroll_to_top_admin_init');
function scroll_to_top_admin_init() {
    // Register a setting and its sanitization callback
    register_setting(
        'scroll_to_top_settings',        // Settings group name
        'scroll_to_top_button_image',    // Option name for button image URL
        'esc_url_raw'                    // Sanitization callback function (esc_url_raw for URL sanitization)
    );

    // Add a section and fields to the settings page
    add_settings_section(
        'scroll_to_top_main_section',   // Section ID
        'Main Settings',                // Section title
        'scroll_to_top_main_section_cb', // Callback function to render section content
        'scroll_to_top_settings'        // Page slug where section is added
    );

    // Add button image URL field
    add_settings_field(
        'scroll_to_top_button_image_field', // Field ID
        'Button Image URL',                 // Field title
        'scroll_to_top_button_image_field_cb', // Callback function to render field input
        'scroll_to_top_settings',                // Page slug where field is added
        'scroll_to_top_main_section'             // Section ID where field is added
    );
}

// Callback function to render section content
function scroll_to_top_main_section_cb() {
    echo '<p>Customize the button image for your Scroll to Top button.</p>';
}

// Callback function to render button image field input
function scroll_to_top_button_image_field_cb() {
    $image_url = get_option('scroll_to_top_button_image');
    echo '<input type="text" id="scroll_to_top_button_image" name="scroll_to_top_button_image" value="' . esc_url($image_url) . '" />';
}
scroll-to-top.css:
/* Example CSS for additional styling */
#scrollToTopBtn {
    background-color: #0073aa; /* Example background color */
    color: #fff; /* Example text color */
    font-size: 16px;
    font-weight: bold;
    text-align: center;
    line-height: 50px;
    text-transform: uppercase;
    box-shadow: 0 3px 6px rgba(0,0,0,0.1);
    transition: background-color 0.3s ease, color 0.3s ease;
}

#scrollToTopBtn:hover {
    background-color: #005c80; /* Example hover background color */
}

How can we help?