Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Amharic Multilingual Content

Amharic Multilingual Content

<?php
/**
 * Plugin Name: Amharic Multilingual Content
 * Description: A plugin to manage multilingual content in Amharic, Afaan Oromo, Tigrinya, and other Ethiopian languages.
 * Version: 1.0.0
 * Author: Ermiyas Chane
 * Author URI: mailto:[email protected]
 * License: GPL2
 */

// Block direct access to the file
if (!defined('ABSPATH')) {
    die('Access Denied');
}

// Load plugin text domain for translations
function amharic_load_textdomain() {
    load_plugin_textdomain('amharic-multilingual-content', false, basename(dirname(__FILE__)) . '/languages');
}
add_action('plugins_loaded', 'amharic_load_textdomain');

// Enqueue necessary scripts and styles
function amharic_enqueue_assets() {
    wp_enqueue_style('amharic-multilingual-style', plugin_dir_url(__FILE__) . 'css/style.css');
    wp_enqueue_script('amharic-multilingual-script', plugin_dir_url(__FILE__) . 'js/script.js', array('jquery'), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'amharic_enqueue_assets');

// Create custom post types for each language
function amharic_register_post_types() {
    $languages = ['amharic', 'afaan_oromo', 'tigrinya'];
    
    foreach ($languages as $language) {
        $labels = array(
            'name' => ucfirst($language) . ' Posts',
            'singular_name' => ucfirst($language) . ' Post',
            'add_new_item' => 'Add New ' . ucfirst($language) . ' Post',
        );

        $args = array(
            'labels' => $labels,
            'public' => true,
            'has_archive' => true,
            'supports' => array('title', 'editor', 'thumbnail', 'excerpt'),
            'show_in_rest' => true,
        );

        register_post_type($language . '_post', $args);
    }
}
add_action('init', 'amharic_register_post_types');

// Add language switcher in the frontend
function amharic_language_switcher() {
    $languages = ['amharic' => 'Amharic', 'afaan_oromo' => 'Afaan Oromo', 'tigrinya' => 'Tigrinya', 'english' => 'English'];
    echo '<div class="amharic-language-switcher">';
    foreach ($languages as $key => $label) {
        echo '<a href="' . add_query_arg('lang', $key) . '">' . $label . '</a> ';
    }
    echo '</div>';
}
add_action('wp_footer', 'amharic_language_switcher');

// Handle content translation
function amharic_get_translated_content($content) {
    if (isset($_GET['lang']) && $_GET['lang'] !== 'english') {
        $language = sanitize_text_field($_GET['lang']);
        $translated_content = get_post_meta(get_the_ID(), 'translation_' . $language, true);
        if ($translated_content) {
            return $translated_content;
        }
    }
    return $content;
}
add_filter('the_content', 'amharic_get_translated_content');

// Add meta boxes for translation fields in post editor
function amharic_add_translation_meta_boxes() {
    $languages = ['amharic', 'afaan_oromo', 'tigrinya'];
    foreach ($languages as $language) {
        add_meta_box('translation_' . $language, ucfirst($language) . ' Translation', 'amharic_translation_meta_box_callback', ['post'], 'normal', 'high', ['language' => $language]);
    }
}
add_action('add_meta_boxes', 'amharic_add_translation_meta_boxes');

function amharic_translation_meta_box_callback($post, $meta) {
    $language = $meta['args']['language'];
    $translation = get_post_meta($post->ID, 'translation_' . $language, true);
    echo '<textarea style="width:100%" rows="5" name="translation_' . $language . '">' . esc_textarea($translation) . '</textarea>';
}

// Save translation fields
function amharic_save_translation_fields($post_id) {
    $languages = ['amharic', 'afaan_oromo', 'tigrinya'];
    
    foreach ($languages as $language) {
        if (isset($_POST['translation_' . $language])) {
            update_post_meta($post_id, 'translation_' . $language, sanitize_text_field($_POST['translation_' . $language]));
        }
    }
}
add_action('save_post', 'amharic_save_translation_fields');

// Create a multilingual SEO feature
function amharic_add_meta_tags() {
    if (isset($_GET['lang']) && $_GET['lang'] !== 'english') {
        $language = sanitize_text_field($_GET['lang']);
        echo '<meta name="language" content="' . $language . '">';
    }
}
add_action('wp_head', 'amharic_add_meta_tags');