در این آموزش، شما را با فرآیند ساخت افزونه وردپرس به نام «آمار تعداد کلمات و زمان مورد نیاز برای خواندن پست» آشنا خواهیم کرد. این افزونه تعداد کلمات، تعداد کاراکترها و زمان تخمینی خواندن را برای هر پست در سایت وردپرس شما نمایش می دهد. بیا شروع کنیم!

می خواهید بیشتر بدانید؟ آموزش ساخت قالب وردپرس به صورت گام به گام برای مبتدیان

مرحله 1: فایل پلاگین را تنظیم کنید

یک فایل جدید با نام «wpstorm-wcp.php» ایجاد کنید و کد زیر را در ابتدای فایل قرار دهید:

<?php
/*
  Plugin Name: Wpstorm Word Count And Time Plugin
  Description: A truly amazing plugin.
  Version: 1.0
  Author: Wpstorm DEV team
  Author URI: https://wpstorm.ir/
  Text Domain: wcpdomain
  Domain Path: /languages
*/
PHP

کد بالا نام افزونه، توضیحات، نسخه، جزئیات نویسنده و دامنه متن را برای اهداف ترجمه تنظیم می کند.

مرحله 2: کلاس پلاگین را تعریف کنید

در زیر هدر فایل افزونه، کد زیر را برای تعریف کلاس «WordCountAndTimePlugin» اضافه کنید:

class WordCountAndTimePlugin {
  function __construct() {
    add_action('admin_menu', array($this, 'adminPage'));
    add_action('admin_init', array($this, 'settings'));
    add_filter('the_content', array($this, 'ifWrap'));
    add_action('init', array($this, 'languages'));
  }

  // Add class methods here
}
PHP

در تابعِ سازنده کلاس، یعنی __construct()، ما به اکشن ها و فیلترهای مختلف وردپرس متصل می‌شویم تا متد های خاصی را اجرا کنیم.

مرحله 3: پشتیبانی محلی سازی

برای فعال کردن محلی‌سازی و فعال کردن قابلیت ترجمه افزونه وردپرس، متد زبان languages() زیر را در کلاس «WordCountAndTimePlugin» اضافه کنید:

function languages() {
  load_plugin_textdomain('wcpdomain', false, dirname(plugin_basename(__FILE__)) . '/languages');
}
PHP

این متد فایل های ترجمه را برای افزونه با استفاده از دامنه متنی یا همان Text Domain مشخص شده در بخش کامنت ابتدای این فایل، یعنی wcpdomain بارگیری می کند.

مرحله 4: در ابتدای هر پست، آمار را نشان دهید

متدهای «ifWrap()» و «createHTML()» زیر را در کلاس «WordCountAndTimePlugin» اضافه کنید:

function ifWrap($content) {
  if (is_main_query() && is_single() && (
      get_option('wcp_wordcount', '1') ||
      get_option('wcp_charactercount', '1') ||
      get_option('wcp_readtime', '1')
    )
  ) {
    return $this->createHTML($content);
  }
  return $content;
}

function createHTML($content) {
  $html = '<h3>' . esc_html(get_option('wcp_headline', 'Post Statistics')) . '</h3><p>';

  // Get word count once because both word count and read time will need it.
  if (get_option('wcp_wordcount', '1') || get_option('wcp_readtime', '1')) {
    $wordCount = str_word_count(strip_tags($content));
  }

  if (get_option('wcp_wordcount', '1')) {
    $html .= esc_html__('This post has', 'wcpdomain') . ' ' . $wordCount . ' ' . __('words', 'wcpdomain') . '.<br>';
  }

  if (get_option('wcp_charactercount', '1')) {
    $html .= 'This post has ' . strlen(strip_tags($content)) . ' characters.<br>';
  }

  if (get_option('wcp_readtime', '1')) {
    $html .= 'This post will take about ' . round($wordCount/225) . ' minute(s) to read.<br>';
  }

  $html .= '</p>';

  if (get_option('wcp_location', '0') == '0') {
    return $html . $content;
  }
  return $content . $html;
}
PHP

متد «ifWrap()» بررسی می‌کند که آیا پرس‌و‌جوی فعلی، پرس‌وجو اصلی، یک پست واحد است و آیا هر یک از گزینه‌های افزونه فعال است یا خیر. اگر چنین است، متد «createHTML()» را برای تولید HTML برای نمایش آمار پست فراخوانی می‌کند.

متد «createHTML()» کد مورد نیاز HTML را بر اساس گزینه‌های فعال (تعداد کلمات، تعداد کاراکترها، زمان خواندن) تولید می‌کند و آن را با محتوای پست مرتبط می‌کند.

مرحله 5: ساخت بخش تنظیمات پلاگین

متد «setting()» زیر و متدهای پشتیبانی را در کلاس «WordCountAndTimePlugin» اضافه کنید:

function settings() {
  add_settings_section('wcp_first_section', null, null, 'word-count-settings-page');

  add_settings_field('wcp_location', 'Display Location', array($this, 'locationHTML'), 'word-count-settings-page', 'wcp_first_section');
  register_setting('wordcountplugin', 'wcp_location', array('sanitize_callback' => array($this, 'sanitizeLocation'), 'default' => '0'));

  add_settings_field('wcp_headline', 'Headline Text', array($this, 'headlineHTML'), 'word-count-settings-page', 'wcp_first_section');
  register_setting('wordcountplugin', 'wcp_headline', array('sanitize_callback' => 'sanitize_text_field', 'default' => 'Post Statistics'));

  add_settings_field('wcp_wordcount', 'Word Count', array($this, 'checkboxHTML'), 'word-count-settings-page', 'wcp_first_section', array('theName' => 'wcp_wordcount'));
  register_setting('wordcountplugin', 'wcp_wordcount', array('sanitize_callback' => 'sanitize_text_field', 'default' => '1'));

  add_settings_field('wcp_charactercount', 'Character Count', array($this, 'checkboxHTML'), 'word-count-settings-page', 'wcp_first_section', array('theName' => 'wcp_charactercount'));
  register_setting('wordcountplugin', 'wcp_charactercount', array('sanitize_callback' => 'sanitize_text_field', 'default' => '1'));

  add_settings_field('wcp_readtime', 'Read Time', array($this, 'checkboxHTML'), 'word-count-settings-page', 'wcp_first_section', array('theName' => 'wcp_readtime'));
  register_setting('wordcountplugin', 'wcp_readtime', array('sanitize_callback' => 'sanitize_text_field', 'default' => '1'));
}

function sanitizeLocation($input) {
  if ($input != '0' && $input != '1') {
    add_settings_error('wcp_location', 'wcp_location_error', 'Display location must be either beginning or end.');
    return get_option('wcp_location');
  }
  return $input;
}

// Reusable checkbox function
function checkboxHTML($args) {
  ?>
  <input type="checkbox" name="<?php echo $args['theName'] ?>" value="1" <?php checked(get_option($args['theName']), '1') ?>>
  <?php
}

function headlineHTML() {
  ?>
  <input type="text" name="wcp_headline" value="<?php echo esc_attr(get_option('wcp_headline')) ?>">
  <?php
}

function locationHTML() {
  ?>
  <select name="wcp_location">
    <option value="0" <?php selected(get_option('wcp_location'), '0') ?>>Beginning of post</option>
    <option value="1" <?php selected(get_option('wcp_location'), '1') ?>>End of post</option>
  </select>
  <?php
}
PHP

متد settings() یک بخش تنظیمات و فیلدهایی را به صفحه تنظیمات وردپرس برای افزونه ما اضافه می کند. تابع «register_setting()» این تنظیمات را ثبت می‌کند و فراخوان‌های پاک‌سازی یا همون عمل sanitization مربوطه و مقادیر پیش‌فرض آن‌ها را مشخص می‌کند.

روش‌های پشتیبانی (sanitizeLocation()، “checkboxHTML()”، “headlineHTML()”، “locationHTML()”) کد HTML را برای فیلدهای تنظیمات ایجاد می‌کنند و پاکسازی یا همان sanitization را مدیریت می‌کنند.

مرحله 6: صفحه مدیریت پلاگین

متدهای «adminPage()» و «ourHTML()» زیر را در کلاس «WordCountAndTimePlugin» اضافه کنید:

function adminPage() {
  add_options_page('Word Count Settings', __('Word Count', 'wcpdomain'), 'manage_options', 'word-count-settings-page', array($this, 'ourHTML'));
}

function ourHTML() {
  ?>
  <div class="wrap">
    <h1>Word Count Settings</h1>
    <form action="options.php" method="POST">
      <?php
        settings_fields('wordcountplugin');
        do_settings_sections('word-count-settings-page');
        submit_button();
      ?>
    </form>
  </div>
  <?php
}
PHP

متد «adminPage()» لینک صفحه تنظیمات افزونه را به منوی مدیریت وردپرس اضافه می‌کند. متد «ourHTML()» کد مورد نیاز HTML صفحه تنظیمات را تولید می‌کند، از جمله فرم ذخیره تنظیمات افزونه.

مرحله 7: پلاگین را راه اندازی کنید

در انتهای فایل افزونه، کد زیر را برای ایجاد نمونه ای از کلاس «WordCountAndTimePlugin» اضافه کنید و افزونه را مقداردهی اولیه کنید:

$wordCountAndTimePlugin = new WordCountAndTimePlugin();
PHP

این خط نمونه‌ای از کلاس «WordCountAndTimePlugin» ایجاد می‌کند، که اجرای سازنده افزونه را آغاز می‌کند و اکشن ها و فیلترهای لازم را متصل و قلاب یا همون Hook می‌کند.

مرحله 8: نصب و فعال سازی

برای استفاده از افزونه مراحل زیر را دنبال کنید:

  1. یک دایرکتوری جدید به نام «wpstorm-wcp» در دایرکتوری «wp-content/plugins/» نصب وردپرس خود ایجاد کنید.
  2. یک ویرایشگر متن باز کنید و کل کدهایی که تا الآن نوشته اید را در یک فایل PHP قرار دهید.
  3. فایل را به عنوان «wpstorm-wcp.php» ذخیره کنید.
  4. فایل «wpstorm-wcp.php» را در پوشه «wpstorm-wcp» که ایجاد کردید آپلود کنید.
  5. وارد داشبورد مدیریت وردپرس خود شوید.
  6. به بخش “افزونه ها” بروید و “Wpstorm Word Count And Time Plugin” را در لیست پیدا کنید.
  7. برای فعال سازی افزونه روی «فعال سازی» کلیک کنید.

نمی دانید چطور وردپرس را به صورت کاملاً رایگان در سیستم شخصی خودتان تست کنید؟ چگونگی نصب وردپرس در لوکال هاست را دنبال کنید.

کد کامل افزونه:

<?php

/*
  Plugin Name: Wpstorm Word Count And Time Plugin
  Description: A truly amazing plugin.
  Version: 1.0
  Author: Wpstorm DEV team
  Author URI: https://wpstorm.ir/
  Text Domain: wcpdomain
  Domain Path: /languages
*/

class WordCountAndTimePlugin {
  function __construct() {
    add_action('admin_menu', array($this, 'adminPage'));
    add_action('admin_init', array($this, 'settings'));
    add_filter('the_content', array($this, 'ifWrap'));
    add_action('init', array($this, 'languages'));
  }

  function languages() {
    load_plugin_textdomain('wcpdomain', false, dirname(plugin_basename(__FILE__)) . '/languages');
  }

  function ifWrap($content) {
    if (is_main_query() AND is_single() AND
    (
      get_option('wcp_wordcount', '1') OR
      get_option('wcp_charactercount', '1') OR
      get_option('wcp_readtime', '1')
    )) {
      return $this->createHTML($content);
    }
    return $content;
  }

  function createHTML($content) {
    $html = '<h3>' . esc_html(get_option('wcp_headline', 'Post Statistics')) . '</h3><p>';

    // get word count once because both wordcount and read time will need it.
    if (get_option('wcp_wordcount', '1') OR get_option('wcp_readtime', '1')) {
      $wordCount = str_word_count(strip_tags($content));
    }

    if (get_option('wcp_wordcount', '1')) {
      $html .= esc_html__('This post has', 'wcpdomain') . ' ' . $wordCount . ' ' . __('words', 'wcpdomain') . '.<br>';
    }

    if (get_option('wcp_charactercount', '1')) {
      $html .= 'This post has ' . strlen(strip_tags($content)) . ' characters.<br>';
    }

    if (get_option('wcp_readtime', '1')) {
      $html .= 'This post will take about ' . round($wordCount/225) . ' minute(s) to read.<br>';
    }

    $html .= '</p>';

    if (get_option('wcp_location', '0') == '0') {
      return $html . $content;
    }
    return $content . $html;
  }

  function settings() {
    add_settings_section('wcp_first_section', null, null, 'word-count-settings-page');

    add_settings_field('wcp_location', 'Display Location', array($this, 'locationHTML'), 'word-count-settings-page', 'wcp_first_section');
    register_setting('wordcountplugin', 'wcp_location', array('sanitize_callback' => array($this, 'sanitizeLocation'), 'default' => '0'));

    add_settings_field('wcp_headline', 'Headline Text', array($this, 'headlineHTML'), 'word-count-settings-page', 'wcp_first_section');
    register_setting('wordcountplugin', 'wcp_headline', array('sanitize_callback' => 'sanitize_text_field', 'default' => 'Post Statistics'));

    add_settings_field('wcp_wordcount', 'Word Count', array($this, 'checkboxHTML'), 'word-count-settings-page', 'wcp_first_section', array('theName' => 'wcp_wordcount'));
    register_setting('wordcountplugin', 'wcp_wordcount', array('sanitize_callback' => 'sanitize_text_field', 'default' => '1'));

    add_settings_field('wcp_charactercount', 'Character Count', array($this, 'checkboxHTML'), 'word-count-settings-page', 'wcp_first_section', array('theName' => 'wcp_charactercount'));
    register_setting('wordcountplugin', 'wcp_charactercount', array('sanitize_callback' => 'sanitize_text_field', 'default' => '1'));

    add_settings_field('wcp_readtime', 'Read Time', array($this, 'checkboxHTML'), 'word-count-settings-page', 'wcp_first_section', array('theName' => 'wcp_readtime'));
    register_setting('wordcountplugin', 'wcp_readtime', array('sanitize_callback' => 'sanitize_text_field', 'default' => '1'));
  }

  function sanitizeLocation($input) {
    if ($input != '0' AND $input != '1') {
      add_settings_error('wcp_location', 'wcp_location_error', 'Display location must be either beginning or end.');
      return get_option('wcp_location');
    }
    return $input;
  }

  // reusable checkbox function
  function checkboxHTML($args) { ?>
    <input type="checkbox" name="<?php echo $args['theName'] ?>" value="1" <?php checked(get_option($args['theName']), '1') ?>>
  <?php }

  function headlineHTML() { ?>
    <input type="text" name="wcp_headline" value="<?php echo esc_attr(get_option('wcp_headline')) ?>">
  <?php }

  function locationHTML() { ?>
    <select name="wcp_location">
      <option value="0" <?php selected(get_option('wcp_location'), '0') ?>>Beginning of post</option>
      <option value="1" <?php selected(get_option('wcp_location'), '1') ?>>End of post</option>
    </select>
  <?php }

  function adminPage() {
    add_options_page('Word Count Settings', __('Word Count', 'wcpdomain'), 'manage_options', 'word-count-settings-page', array($this, 'ourHTML'));
  }

  function ourHTML() { ?>
    <div class="wrap">
      <h1>Word Count Settings</h1>
      <form action="options.php" method="POST">
      <?php
        settings_fields('wordcountplugin');
        do_settings_sections('word-count-settings-page');
        submit_button();
      ?>
      </form>
    </div>
  <?php }
}

$wordCountAndTimePlugin = new WordCountAndTimePlugin();
PHP

به یادگیری ادامه دهید: ساخت افزونه فیلتر کردن کلمات خاص در سایت وردپرس

تبریک می گویم! شما با موفقیت یک افزونه وردپرس ایجاد کرده اید که تعداد کلمات، تعداد کاراکترها و زمان تخمینی خواندن را به محتوای پست شما اضافه می کند.

می‌توانید افزونه را با تغییر HTML صفحه تنظیمات، افزودن سبک‌های CSS، یا بهبود عملکرد برای برآورده کردن نیازهای خاص خود، سفارشی کنید.

توجه: مطمئن شوید که افزونه را به طور کامل تست کرده و بهترین روش های امنیتی را قبل از استقرار آن در یک سایت تولیدی در نظر بگیرید.

تموم شد! شما آموزش ساخت افزونه وردپرس “Wpstorm Word Count And Time Plugin” را تکمیل کرده اید. در صورت داشتن هرگونه سوال بیشتر با ما تماس بگیرید.

تیم طوفان وردپرس

نوشته‌های مشابه

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *