Creating a Custom Plugin for WordPress

Tiempo de lectura: 2 minutos
Hello, today we’re going to learn how we can create a custom plugin for WordPress.

Step 1: Create Folder and File Structure

Inside the wp-content/plugins/ folder, create a new folder for your plugin, for example, custom-footer-message. Inside this folder, create two files: custom-footer-message.php (main plugin file) and settings.php (for settings).

Step 2: Main File (custom-footer-message.php)

<?php
/*
Plugin Name: Custom Auto-Description Plugin
Description: A plugin to add custom Description to the post generated by the Auto Description plugin using the post text.
Version: 1.0
Author: ismaDev
*/

require_once(plugin_dir_path(__FILE__) . 'functions.php');
require_once(plugin_dir_path(__FILE__) . 'settings.php');

function custom_description_plugin_settings_link( $links ) {
    $settings_link = '<a href="' . admin_url('options-general.php?page=custom-description-plugin-settings') . '">' . __('Settings') . '</a>';
    array_push( $links, $settings_link );
    return $links;
}

$plugin_basename = plugin_basename(__FILE__);

add_filter("plugin_action_links_$plugin_basename", 'custom_description_plugin_settings_link');

add_action('wp_head', 'add_custom_description');
add_action('admin_menu', 'custom_description_plugin_menu');
add_action('admin_init', 'custom_description_plugin_settings_init');

Remember to modify the plugin name, description, and author.

Step 3: Functions File (functions.php)

<?php
function add_custom_description()
{
    $post_id = get_the_ID();
    $description = get_post_meta($post_id, 'description', true);

    if (is_home()) {
        $default_description = get_option('custom_description_default', '');
        echo '<meta name="description" content="' . esc_attr($default_description) . '">';
    } else {
        if ($description) {
            echo '<meta name="description" content="' . esc_attr($description) . '">';
        } else {
            generate_description();
        }
    }
}

function generate_description()
{
    $title = get_the_title();
    echo '<meta name="description" content="Summary and news of ' . $title . '" />';
}

Step 4: Settings File (settings.php)

<?php
function custom_description_plugin_menu() {
    add_options_page(
        'Custom Description Settings',
        'Default Description',
        'manage_options',
        'custom-description-plugin-settings',
        'custom_description_plugin_settings_page'
    );
}

function custom_description_plugin_settings_page() {
    ?>
    <div class="wrap">
        <h1>Custom Description Settings</h1>
        <form method="post" action="options.php">
            <?php settings_fields('custom_description_plugin_settings'); ?>
            <?php do_settings_sections('custom_description_plugin_settings'); ?>
            <table class="form-table">
                <tr>
                    <th scope="row">Description for Home Page</th>
                    <td>
                        <input type="text" name="custom_description_default" value="<?php echo esc_attr(get_option('custom_description_default', '')); ?>" />
                    </td>
                </tr>
            </table>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}

function custom_description_plugin_settings_init() {
    register_setting('custom_description_plugin_settings', 'custom_description_default');
}

Step 4: Activate the Plugin

  1. Log in to your WordPress admin panel.
  2. Go to the “Plugins” section and activate the plugin named “Custom Footer Message”.
  3. Click on settings and change the footer content to add it.

Step 5: Verify the Result

After activating the plugin, you should see a custom message in the footer of your website.

This is a very basic example, but you can expand it according to your needs. You can add configuration options, improve the style of the message, or even integrate more complex functions. Remember to check the official WordPress documentation for more information on plugin development: WordPress Plugin Developer Handbook.

Leave a Comment