wp fields framork结合polylang翻译

I’ll help you integrate Polylang translation support into your WP Simple Fields framework. Here’s a comprehensive solution:

Solution: Add Polylang Translation Support

You need to modify your framework to make fields translatable. Here’s how:

1. Add Translation Support to Your Framework

Add this code to your main plugin file, after the WP_Field_Framework class:

php

/**
 * Polylang Translation Support for WP Simple Fields
 */
class WPSF_Polylang_Integration
{
    private static $instance = null;
    private $framework;
    
    public static function get_instance()
    {
        return self::$instance ?: (self::$instance = new self());
    }
    
    private function __construct()
    {
        // Only initialize if Polylang is active
        if (!function_exists('pll_current_language')) {
            return;
        }
        
        $this->framework = WP_Field_Framework::get_instance();
        
        // Register strings for translation
        add_action('admin_init', [$this, 'register_polylang_strings'], 100);
        
        // Filter to get translated values
        add_filter('wpsf_get_value', [$this, 'get_translated_value'], 10, 3);
    }
    
    /**
     * Register all theme option strings with Polylang
     */
    public function register_polylang_strings()
    {
        if (!function_exists('pll_register_string')) {
            return;
        }
        
        $option_group = 'theme_options';
        $options = get_option($option_group, []);
        
        foreach ($options as $field_id => $value) {
            // Skip non-translatable field types
            if ($this->is_translatable_field($field_id, $value)) {
                $this->register_string($field_id, $value);
            }
        }
    }
    
    /**
     * Check if a field should be translatable
     */
    private function is_translatable_field($field_id, $value)
    {
        // Don't translate these types
        $non_translatable = ['image', 'file', 'gallery', 'checkbox', 'color'];
        
        // Check if it's a media field (numeric ID)
        if (is_numeric($value)) {
            return false;
        }
        
        // Check if it's an array (multiselect, gallery, group)
        if (is_array($value)) {
            return false;
        }
        
        return true;
    }
    
    /**
     * Register a string with Polylang
     */
    private function register_string($field_id, $value)
    {
        if (empty($value) || !is_string($value)) {
            return;
        }
        
        pll_register_string(
            $field_id,
            $value,
            'WP Simple Fields',
            false // not multiline by default
        );
    }
    
    /**
     * Get translated value
     */
    public function get_translated_value($value, $field_id, $context)
    {
        if ($context !== 'theme_option' || !function_exists('pll__')) {
            return $value;
        }
        
        if ($this->is_translatable_field($field_id, $value)) {
            return pll__($value);
        }
        
        return $value;
    }
}

// Initialize Polylang integration
add_action('plugins_loaded', function() {
    WPSF_Polylang_Integration::get_instance();
});

2. Modify the get_value() Method

Update the get_value() method in your WP_Field_Framework class to support translation:

php

public function get_value($field_id, $context = 'theme_option', $object_id = null)
{
    $value = null;
    
    switch ($context) {
        case 'theme_option':
            $options = get_option($this->option_group, []);
            $value = $options[$field_id] ?? '';
            break;
        case 'post_meta':
            $value = get_post_meta($object_id, $field_id, true);
            break;
        case 'term_meta':
            $value = get_term_meta($object_id, $field_id, true);
            break;
        default:
            return null;
    }
    
    // Apply translation filter
    return apply_filters('wpsf_get_value', $value, $field_id, $context);
}

3. Add Manual Translation Interface (Alternative Approach)

If you want more control, create language-specific fields:

php

/**
 * Add this to your field registration
 */
function register_translatable_fields()
{
    $framework = wp_field_framework();
    
    // Check if Polylang is active
    if (function_exists('pll_languages_list')) {
        $languages = pll_languages_list();
        
        foreach ($languages as $lang) {
            $framework->add_field([
                'id' => 'site_description_' . $lang,
                'type' => 'textarea',
                'title' => 'Site Description (' . strtoupper($lang) . ')',
                'description' => 'Enter site description for ' . strtoupper($lang),
                'context' => 'theme_option',
                'section' => 'general'
            ]);
        }
    } else {
        // Fallback if Polylang is not active
        $framework->add_field([
            'id' => 'site_description',
            'type' => 'textarea',
            'title' => 'Site Description',
            'context' => 'theme_option',
            'section' => 'general'
        ]);
    }
}

4. Helper Function to Get Translated Value

Add this helper function for easy retrieval:

php

/**
 * Get translated field value
 */
function wpsf_get_translated($field_id, $context = 'theme_option', $object_id = null)
{
    $framework = wp_field_framework();
    
    // For language-specific fields approach
    if (function_exists('pll_current_language')) {
        $lang = pll_current_language();
        $lang_field_id = $field_id . '_' . $lang;
        
        // Try to get language-specific field first
        $value = $framework->get_value($lang_field_id, $context, $object_id);
        
        // Fallback to default field
        if (empty($value)) {
            $value = $framework->get_value($field_id, $context, $object_id);
        }
        
        return $value;
    }
    
    // Default behavior
    return $framework->get_value($field_id, $context, $object_id);
}

5. Usage Examples

In your theme files:

php

// Method 1: Using the filter (automatic translation)
$site_title = wp_field_framework()->get_value('site_title', 'theme_option');
echo $site_title; // Automatically translated if registered with Polylang

// Method 2: Using language-specific fields
$description = wpsf_get_translated('site_description', 'theme_option');
echo $description;

// For post meta
$custom_field = wpsf_get_translated('custom_field', 'post_meta', get_the_ID());

// For term meta
$term_field = wpsf_get_translated('term_field', 'term_meta', $term_id);

6. Register Strings in Polylang Admin

Go to Languages > String translations in your WordPress admin. You’ll see all registered strings from “WP Simple Fields” group. You can translate them there for each language.

Which Approach to Use?

  1. Automatic Registration (Recommended for simple sites): Use the Polylang integration class above. It automatically registers strings and translates them.
  2. Language-Specific Fields (Better for complex content): Create separate fields for each language. This gives you more control and better UI in the admin panel.
  3. Hybrid Approach: Use automatic registration for simple text fields and language-specific fields for rich editors and complex content.

Choose the approach that best fits your workflow! The automatic registration is easier to implement, while language-specific fields give you more control and a clearer admin interface.

×
wp fields framork结合polylang翻译

wp fields framork结合polylang翻译

I'll help you integr...

查看更多

AI agent 如何翻译网站(标签+动态内容)

先让AI把网站进行i18n化, …