نحوه تنظیم Heartbeat وردپرس

0

Heartbeat وردپرس چیست

وردپرس در سال ۲۰۱۳ ویژگی Heartbeat API یا ضربان قلب وردپرس را معرفی کرد، WordPress Heartbeat به مرورگر شما این امکان را می‌دهد تا به صورت خودکار با سرور ارتباط برقرار کند اما این ویژگی ممکن است برای همه سایت‌ها مناسب نباشد مخصوصا اگر از سرویس های اشتراکی استفاده می کنید. ضربان قلب وردپرس ممکن است استفاده از منابع سرور و CPU را تا حد زیادی افزایش دهد پس نتیجه می گیریم که Heartbeat API معمولاً باید غیرفعال و یا محدود شود چرا یکی از مهمترین موارد در افزایش منابع سرور است.

توجه کنید که با غیرفعال کردن Heartbeat API ویژگی دیگر نمی توانید از قابلیت Revisions (ذخیره خودکار وردپرس) و یا پیش‌نویس خودکار وردپرس استفاده کنید، بنا بر این پس از غیر فعال سازی اگر هنگام انتشار یک مطلب آن را ذخیره نکرده باشید و با مشکلاتی چون رفتن برق مواجه شوید تمامی اطلاعات نوشته در حال انتشار از بین خواهد رفت همچنین شما دیگر به ویرایش های قبلی دسترسی نخواهید داشت.

نحوه محدود کردن Heartbeat وردپرس

روند محدود کردن بسیار ساده بوده و کافیست قطعه کد زیر را در فایل functions.php قالب فعال خود اضافه کنید تا این قابلیت پیشفرض در وردپرس غیرفعال شود:

add_action( 'init', 'hgw_disable_heartbeat_api', 1 );
function hgw_disable_heartbeat_api() {
	wp_deregister_script('heartbeat');
}

اگر به وردپرس مسلط نیستید و با کدنویسی آشنایی ندارید برای ویرایش فایل functions.php ابتدا وارد پنل پیشخوان وردپرس شده در بخش نمایش روی ویرایشگر پوسته کلیک کنید و سپس در منو سمت چپ روی فایل functions.php یا همان توابع پوسته کلیک کنید. پس از باز شدن فایل کد بالا را بعد از <?php اضافه کنید.

How to Completely Stop Heartbeat API Without a Plugin

Disable the Heartbeat API

Adding the action below allows you to stop the Heartbeat API, effectively disabling all API calls. To achieve that, add the code snippet below to the bottom of functions.php.

function wb_stop_heartbeat() {
 wp_deregister_script('heartbeat');
}
add_action('init', 'wb_stop_heartbeat', 1);

You can disable a specific feature in the WordPress Heartbeat API the same way.

function disable_autosave() {
 wp_deregister_script('autosave');
}
add_action('admin_init', 'disable_autosave');

Configure a Custom Interval for the Heartbeat API Calls

To limit WordPress Heartbeat API calls, you can specify a filter that will configure a custom interval for the API requests. The following code snippet limits the Heartbeat API calls to one request per minute.

function wb_set_heartbeat_time_interval($settings) {
 $settings['interval']=60;
 return $settings;
}
add_filter('heartbeat_settings', 'wb_set_heartbeat_time_interval');

Configure a Custom Interval for WordPress Autosaves

WordPress allows you to configure a custom interval for autosaves by redefining the AUTOSAVE_INTERVAL constant. Similar to other constants, you can assign it a new value by adding the following line to your main WordPress configuration file:

define('AUTOSAVE_INTERVAL', 300);

You can assign the interval a bigger value, such as 3600, which is an hour, to disable WordPress autosaves altogether. WordPress constants configured in wp-config.php do not get modified during any WordPress updates. You will not have to redefine them later.

ممکن است شما دوست داشته باشید
ارسال یک پاسخ