Blogs, การพัฒนาเว็บไซต์, การสร้างเว็บไซต์

แจกฟรี! ปลั๊กอิน WordPress สร้าง Permalink (URL) ภาษาอังกฤษให้อัตโนมัติ เพื่อ SEO ที่ดีกว่า

สำหรับคนทำเว็บด้วย WordPress ที่เขียนบทความภาษาไทย เคยเจอปัญหานี้กันไหมครับ? เวลาที่เราตั้งชื่อเรื่อง (Title) ของบทความใหม่เป็นภาษาไทย เช่น “5 เทคนิคการตลาดออนไลน์สำหรับมือใหม่” พอเรากด Save Draft หรือ Publish ตัว WordPress ก็จะสร้าง URL หรือที่เรียกว่า Permalink ให้เป็นภาษาไทยตามไปด้วย กลายเป็น:

https://yourwebsite.com/5-เทคนิคการตลาดออนไลน์สำหรับมือใหม่/

ซึ่ง URL ที่เป็นภาษาไทยยาวๆ แบบนี้ นอกจากจะดูไม่สวยงามเวลาคัดลอกไปแชร์ในโซเชียลมีเดียแล้ว ยังส่งผลเสียต่อการทำ SEO (Search Engine Optimization) อีกด้วย เพราะ Search Engine อย่าง Google จะไม่ค่อยเข้าใจ URL ที่เป็นภาษาอื่นที่ไม่ใช่ภาษาอังกฤษเท่าไหร่นัก

แน่นอนว่าหลายคนแก้ปัญหานี้ด้วยการกด “แก้ไข” (Edit) ที่ช่อง Permalink แล้วนั่งพิมพ์เป็นภาษาอังกฤษเองทุกครั้ง ซึ่งก็เป็นวิธีที่ใช้ได้ แต่บางครั้งก็ลืม หรือบางทีก็คิดไม่ออกว่าจะใช้คำว่าอะไรดี ทำให้เสียเวลาและอาจเกิดความผิดพลาดได้

วันนี้ผมจึงมีตัวช่วยดีๆ ที่จะมาแก้ปัญหาน่ารำคาญนี้ให้หมดไปมาแจกกันครับ! นั่นก็คือ ดาวน์โหลด  “[Translate Slug to English]” ปลั๊กอินขนาดเล็กที่ทีมงานเราพัฒนาขึ้นมาเองเพื่อคนทำเว็บชาวไทยโดยเฉพาะ

เจ้าปลั๊กอินตัวนี้จะทำการ “แปลงชื่อเรื่องบทความภาษาไทยของคุณ ให้กลายเป็น Permalink ภาษาอังกฤษที่สวยงามและเป็นมิตรต่อ SEO โดยอัตโนมัติ” ทันทีที่คุณกดบันทึกบทความ

ตัวอย่างเช่น:

  • ชื่อเรื่อง: รีวิวคาเฟ่เปิดใหม่ย่านอารีย์
  • Permalink ที่สร้างอัตโนมัติ: review-new-cafe-in-ari/

ง่ายๆ แบบนี้เลยครับ คุณไม่ต้องมานั่งแก้ Permalink เองอีกต่อไป ช่วยประหยัดเวลาและทำให้เว็บไซต์ของคุณมีโครงสร้าง URL ที่ดีขึ้นทันที

เพิ่มเมนูตั้งค่าใน Admin Dashboard:

สร้างเมนูในแผงควบคุมของ WordPress (Settings page) Setting > Translate Slug ที่ให้ผู้ใช้สามารถเลือกได้ว่าอยากให้ Plugin นี้ทำงานกับ post, page, หรือ custom post type.


				
					<?php
/*
Plugin Name: Translate Slug to English
Plugin URI: https://jodbush.com
Description: A plugin to automatically translate post slugs to English.
Version: 1.2
Author: Jodbush
Author URI: https://jodbush.com
License: GPL2
*/

// สร้างเมนูการตั้งค่าใน Dashboard
function translate_slug_add_settings_menu() {
    add_options_page(
        'Translate Slug Settings',
        'Translate Slug',
        'manage_options',
        'translate-slug-settings',
        'translate_slug_settings_page'
    );
}
add_action('admin_menu', 'translate_slug_add_settings_menu');

// หน้าการตั้งค่าใน admin
function translate_slug_settings_page() {
    ?>
    <div class="wrap">
        <h1>Translate Slug Settings</h1>
        <form method="post" action="options.php">
            <?php
            settings_fields('translate_slug_options_group');
            do_settings_sections('translate-slug-settings');
            ?>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">เลือก Post Types ที่จะใช้แปล Slug:</th>
                    <td>
                        <fieldset>
                            <?php
                            $selected_post_types = get_option('translate_slug_post_types', array('post'));
                            ?>
                            <label><input type="checkbox" name="translate_slug_post_types[]" value="post" <?php checked(in_array('post', $selected_post_types)); ?> /> Post</label><br>
                            <label><input type="checkbox" name="translate_slug_post_types[]" value="page" <?php checked(in_array('page', $selected_post_types)); ?> /> Page</label><br>
                            <label><input type="checkbox" name="translate_slug_post_types[]" value="product" <?php checked(in_array('product', $selected_post_types)); ?> /> Product (WooCommerce)</label><br>
                            <?php
                            $post_types = get_post_types(array('public' => true), 'objects');
                            foreach ($post_types as $post_type) {
                                if ($post_type->name != 'post' && $post_type->name != 'product') {
                                    echo '<label><input type="checkbox" name="translate_slug_post_types[]" value="' . esc_attr($post_type->name) . '" ' . checked(in_array($post_type->name, $selected_post_types), true, false) . ' /> ' . esc_html($post_type->label) . '</label><br>';
                                }
                            }
                            ?>
                        </fieldset>
                    </td>
                </tr>
            </table>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}

// ลงทะเบียนตัวเลือกการตั้งค่า
function translate_slug_register_settings() {
    register_setting('translate_slug_options_group', 'translate_slug_post_types');
}
add_action('admin_init', 'translate_slug_register_settings');

// ฟังก์ชั่นสำหรับแปล slug เป็นภาษาอังกฤษ (ใช้ Google Translate API ฟรี)
function translate_slug_to_english($title) {
    $url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=th&tl=en&dt=t&q=' . urlencode($title);
    $response = wp_remote_get($url);

    if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
        $body = wp_remote_retrieve_body($response);
        $data = json_decode($body);
        if (!empty($data[0][0][0])) {
            return sanitize_title($data[0][0][0]);
        }
    }
    // หากเชื่อมต่อ API ไม่ได้หรือแปลล้มเหลว ให้คืนค่าเดิมกลับไปทำเป็น slug
    return sanitize_title($title); 
}

// ฟังก์ชั่นเมื่อมีการบันทึกโพสต์
function handle_post_save($post_id) {
    // 1. ตรวจสอบการทำ Autosave หรือ Revision ไม่ให้โค้ดทำงานซ้ำซ้อน
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
    if (wp_is_post_revision($post_id)) return $post_id;

    // 2. ถอด Action ออกชั่วคราว ป้องกันปัญหา Infinite Loop ตอนใช้ wp_update_post
    remove_action('save_post', 'handle_post_save');

    // 3. ตรวจสอบว่าเป็น post type ที่ผู้ใช้เลือกไว้หรือไม่
    $post_type = get_post_type($post_id);
    $selected_post_types = get_option('translate_slug_post_types', array('post'));
    
    // ตรวจสอบสถานะว่าไม่ใช่ auto-draft และอยู่ใน Post Type ที่เลือก
    if (in_array($post_type, $selected_post_types) && get_post_status($post_id) !== 'auto-draft') {

        $post_title = get_post_field('post_title', $post_id);
        $current_slug = get_post_field('post_name', $post_id);
        
        // ตรวจสอบว่ามีภาษาไทยใน Title ไหม เพื่อป้องกันการเรียก API โดยไม่จำเป็น (ถ้า Title เป็นอังกฤษอยู่แล้ว)
        if (preg_match('/[ก-๙]/u', $post_title)) {
            $translated_slug = translate_slug_to_english($post_title);

            // หาก slug เปลี่ยนแปลง ก็อัพเดต
            if ($translated_slug !== $current_slug && !empty($translated_slug)) {
                wp_update_post(array(
                    'ID' => $post_id,
                    'post_name' => $translated_slug // ตั้งค่า slug ใหม่ที่แปลแล้ว
                ));
            }
        }
    }

    // 4. ใส่ Action กลับคืนมา
    add_action('save_post', 'handle_post_save');

    return $post_id;
}
add_action('save_post', 'handle_post_save');