### TranslatePress Example Usage Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/10-api-TRP_Translation_Manager.md Demonstrates how to get the TranslatePress instance and access the translation manager component. Includes checks for editor mode and frontend AJAX requests. ```php $trp = TRP_Translate_Press::get_trp_instance(); $translator = $trp->get_component('translation_manager'); // Check if viewing editor if (isset($_REQUEST['trp-edit-translation'])) { $mode = $_REQUEST['trp-edit-translation']; if ($mode === 'preview') { // Preview mode - show live preview } else if ($mode === 'true') { // Full editor mode } } // Check if AJAX from frontend if (TRP_Translation_Manager::is_ajax_on_frontend()) { // Handle frontend AJAX differently } ``` -------------------------------- ### URL Structure Examples Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/05-api-TRP_Url_Converter.md Illustrates URL structures with and without a subdirectory for the default language. ```plaintext https://example.com/ (default language) https://example.com/fr/ (French) https://example.com/de/ (German) ``` ```plaintext https://example.com/en/ (English) https://example.com/fr/ (French) https://example.com/de/ (German) ``` -------------------------------- ### Example Usage of Translation Editor Trigger Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/10-api-TRP_Translation_Manager.md This example demonstrates how visiting a URL with the '?trp-edit-translation=true' parameter will load the translation editor instead of the regular page. No code is executed here, it's a usage illustration. ```php // When visiting: // https://example.com/?trp-edit-translation=true // The translation editor template loads instead of the normal page ``` -------------------------------- ### Get List of Languages and ISO Codes Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/INDEX.md Retrieve a list of all configured languages, optionally formatted by English name, and get their corresponding ISO codes. Requires access to the 'languages' component. ```php $languages = $trp->get_component('languages'); $all_langs = $languages->get_languages('english_name'); $iso_codes = $languages->get_iso_codes($active_langs); ``` -------------------------------- ### TranslatePress URL Converter Usage Example Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/05-api-TRP_Url_Converter.md Demonstrates how to use the TRP_Url_Converter component to get language slugs, check for admin requests, and retrieve current URLs. ```php $trp = TRP_Translate_Press::get_trp_instance(); $url_converter = $trp->get_component('url_converter'); // Get URL slug for a language $slug = $url_converter->get_url_slug('fr_FR'); // 'fr' // Check if current request is from admin if ($url_converter->is_admin_request()) { // Don't translate admin requests } // Get current page URL $current_url = $url_converter->cur_page_url(); // Get home URL without trailing slash $home = $url_converter->get_abs_home(); // These methods are typically called by WordPress hooks automatically // add_hreflang_to_head() - called on wp_head // change_lang_attr_in_html_tag() - called on language_attributes filter ``` -------------------------------- ### Setup Gettext String Processing Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/10-api-TRP_Translation_Manager.md Sets up the necessary hooks and filters for collecting and processing gettext strings. This method is also hooked to the 'init' action. ```php public function initialize_gettext_processing(): void ``` -------------------------------- ### Example: Getting Translation Status Constant Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/06-types.md Demonstrates how to retrieve the constant for 'not translated' and use it to filter translations. ```php $query = $trp->get_component('query'); $not_trans = $query->get_constant_not_translated(); // Get only translated strings $translated = $query->get_existing_translations($strings, 'fr_FR'); // Returns only entries where status != 0 ``` -------------------------------- ### Example: Querying for Strings in Active Blocks Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/06-types.md Shows how to query for translations specifically from active translation blocks using the block type constant. ```php // Query for strings in active blocks only $blocks = $query->get_existing_translations( $strings, 'fr_FR', $query->get_constant_block_type_active() ); ``` -------------------------------- ### Example of Language Switcher Option Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/07-configuration.md Demonstrates how to check the 'shortcode-options' setting to conditionally display language names and flags. This is useful for customizing the appearance of the language switcher. ```php $settings = $settings_obj->get_settings(); if ($settings['shortcode-options'] === 'flags-full-names') { // Display like: πŸ‡«πŸ‡· French (France) } ``` -------------------------------- ### Get Component Instance Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/01-api-TRP_Translate_Press.md Returns a specific component instance by its name, allowing direct access to individual plugin modules. ```APIDOC ## get_component() ### Description Returns a particular component instance by name for direct access. ### Method `public function get_component(string $component): mixed` ### Parameters #### Path Parameters - **$component** (string) - Required - Component name (e.g., 'loader', 'settings', 'query'). ### Returns Component instance (type varies by component) ### Available Components - `loader` β†’ TRP_Hooks_Loader - `settings` β†’ TRP_Settings - `translation_render` β†’ TRP_Translation_Render - `machine_translator` β†’ TRP_Machine_Translator - `query` β†’ TRP_Query - `language_switcher` β†’ TRP_Language_Switcher - `translation_manager` β†’ TRP_Translation_Manager - `url_converter` β†’ TRP_Url_Converter - `languages` β†’ TRP_Languages - `machine_translator_logger` β†’ TRP_Machine_Translator_Logger - `translation_memory` β†’ TRP_Translation_Memory - `editor_api_regular_strings` β†’ TRP_Editor_Api_Regular_Strings - `editor_api_gettext_strings` β†’ TRP_Editor_Api_Gettext_Strings - `notifications` β†’ TRP_Trigger_Plugin_Notifications - `upgrade` β†’ TRP_Upgrade - `plugin_updater` β†’ TRP_Plugin_Updater - `license_page` β†’ TRP_LICENSE_PAGE - `advanced_tab` β†’ TRP_Advanced_Tab - `machine_translation_tab` β†’ TRP_Machine_Translation_Tab - `error_manager` β†’ TRP_Error_Manager ### Example ```php $trp = TRP_Translate_Press::get_trp_instance(); $query = $trp->get_component('query'); $languages = $trp->get_component('languages'); $settings = $trp->get_component('settings')->get_settings(); ``` ``` -------------------------------- ### TRP Query Example Usage Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Demonstrates common TRP_Query operations including checking table existence, retrieving translations, inserting new strings, updating existing ones, and fetching constants. ```php // Get component $query = $trp->get_component('query'); // Check if table exists if (!$query->table_exists($query->get_table_name('fr_FR'))) { $query->check_table('en_US', 'fr_FR'); } // Get translations for strings $strings = ['Hello', 'World']; $translations = $query->get_existing_translations($strings, 'fr_FR'); if ($translations && isset($translations['Hello'])) { echo $translations['Hello']->translated; // Output: 'Bonjour' } // Insert new strings to translate $query->insert_strings(['New product', 'New service'], 'fr_FR'); // Update translation $updates = [ ['id' => 1, 'original' => 'Hello', 'translated' => 'Bonjour', 'status' => 2] ]; $query->update_strings($updates, 'fr_FR'); // Get constants $untranslated = $query->get_constant_not_translated(); $reviewed = $query->get_constant_human_reviewed(); ``` -------------------------------- ### WordPress Initialization Hooks Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/09-hooks.md Used for multiple purposes during early WordPress initialization. Includes starting output buffering, adding callbacks for REST API translation, initializing gettext processing, and setting up view-as-user functionality. ```php add_action('init', [$translation_render, 'start_output_buffer'], 0); ``` ```php add_action('init', [$translation_render, 'add_callbacks_for_translating_rest_api'], 10); ``` ```php add_action('init', [$translation_manager, 'create_gettext_translated_global'], 10); ``` ```php add_action('init', [$translation_manager, 'initialize_gettext_processing'], 10); ``` ```php add_action('init', [$translation_manager, 'trp_view_as_user'], 10); ``` ```php add_action('init', [$trp_main, 'init_translation'], 8); ``` -------------------------------- ### Accessing TranslatePress Instance and Components Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/01-api-TRP_Translate_Press.md Get the main plugin instance and access its components like languages and settings. Useful for retrieving plugin data or configuration. ```php // Get the main plugin instance $trp = TRP_Translate_Press::get_trp_instance(); // Access components $languages = $trp->get_component('languages'); $all_langs = $languages->get_languages('english_name'); // Get settings $settings_obj = $trp->get_component('settings'); $settings = $settings_obj->get_settings(); echo $settings['default-language']; // 'en_US' ``` -------------------------------- ### Access Plugin Constants After Initialization Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/07-configuration.md Shows how to check if a plugin constant like TRP_PLUGIN_DIR is defined and then use it to construct paths, for example, to access plugin includes. ```php if (defined('TRP_PLUGIN_DIR')) { $path = TRP_PLUGIN_DIR . 'includes/'; } ``` -------------------------------- ### Get All Gettext Table Names Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Retrieves a list of all available gettext translation table names. ```php public function get_all_gettext_table_names(): array ``` -------------------------------- ### Get All Plugin Settings Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/02-api-TRP_Settings.md Retrieves the complete plugin settings array, merging options from WordPress and machine translation configurations. Useful for accessing all available settings at once. ```php public function get_settings(): array ``` ```php $settings = $trp_settings->get_settings(); echo $settings['default-language']; // 'en_US' foreach ($settings['translation-languages'] as $lang) { echo $lang; // 'en_US', 'fr_FR', etc. } ``` -------------------------------- ### CSS Styling for Language Switcher Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/11-api-TRP_Language_Switcher.md Example CSS to customize the appearance of the language switcher, including general styling, individual language options, active state, and floating widget positioning. ```css /* Customize language switcher */ .trp-language-switcher { display: flex; gap: 10px; background: #f5f5f5; padding: 15px; border-radius: 8px; } .trp-language-option { padding: 8px 12px; cursor: pointer; border-radius: 4px; text-decoration: none; color: #333; transition: all 0.2s; } .trp-language-option:hover { background-color: #e0e0e0; } .trp-language-option.active { background-color: #007cba; color: white; font-weight: bold; } /* Floating widget position */ .trp-floater-ls { position: fixed; z-index: 9999; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.15); } .trp-floater-ls.bottom-right { bottom: 20px; right: 20px; } .trp-floater-ls.bottom-left { bottom: 20px; left: 20px; } .trp-floater-ls.top-right { top: 20px; right: 20px; } .trp-floater-ls.top-left { top: 20px; left: 20px; } ``` -------------------------------- ### Get Language Switcher Options Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/02-api-TRP_Settings.md Retrieves all available display options for the language switcher. This method returns an associative array that can be iterated to display labels and configurations. ```php public function get_language_switcher_options(): array ``` ```php [ 'full-names' => [ 'full_names' => true, 'short_names' => false, 'flags' => false, 'label' => 'Full Language Names' ], 'short-names' => [ 'full_names' => false, 'short_names' => true, 'flags' => false, 'label' => 'Short Language Names' ], 'flags-full-names' => [ 'full_names' => true, 'short_names' => false, 'flags' => true, 'label' => 'Flags with Full Language Names' ], 'flags-short-names' => [ 'full_names' => false, 'short_names' => true, 'flags' => true, 'label' => 'Flags with Short Language Names' ], 'only-flags' => [ 'full_names' => false, 'short_names' => false, 'flags' => true, 'label' => 'Only Flags' ] ] ``` ```php $options = $trp_settings->get_language_switcher_options(); foreach ($options as $key => $option) { echo $option['label']; } ``` -------------------------------- ### Get String IDs Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Fetches the database IDs for an array of original strings in a specified language. The output format can be controlled. ```php public function get_string_ids( array $original_strings, string $language_code, int $output = OBJECT_K ): array ``` -------------------------------- ### Access TranslatePress Plugin Instance Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/INDEX.md Access the main TranslatePress plugin instance to utilize its API. Ensure the class exists before attempting to get the instance. ```php if (class_exists('TRP_Translate_Press')) { $trp = TRP_Translate_Press::get_trp_instance(); // Use TranslatePress API } ``` -------------------------------- ### WordPress Language Data Structure Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/04-api-TRP_Languages.md An example of the language data structure returned by the get_wp_languages() function, detailing fields like language code, names, ISO codes, and package information. ```php [ 'en_US' => [ 'language' => 'en_US', 'english_name' => 'English (United States)', 'native_name' => 'English', 'iso' => ['en', 'eng'], 'package' => 'http://...', 'version' => '5.2', 'updated' => '2019-06-21 12:00:00', 'strings' => ['continue' => 'Continue'] ], 'fr_FR' => [ 'language' => 'fr_FR', 'english_name' => 'French (France)', 'native_name' => 'FranΓ§ais', 'iso' => ['fr', 'fra'], // ... other fields ] ] ``` -------------------------------- ### Handle AJAX Requests Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/INDEX.md Process AJAX requests from the WordPress frontend. This example shows how to hook into 'wp_ajax_my_action' and access TranslatePress components. ```php add_action('wp_ajax_my_action', function() { $query = $trp->get_component('query'); // Use query methods }); ``` -------------------------------- ### Get Singleton Instance Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/01-api-TRP_Translate_Press.md Retrieves the singleton instance of the TRP_Translate_Press class. This is the primary way to access the plugin's main functionality. ```php ``` -------------------------------- ### Get All Table Names Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Returns all translation dictionary table names for a given source language, optionally excluding specific translation languages. ```php public function get_all_table_names( string $original_language, array $exception_translation_languages = [] ): array ``` -------------------------------- ### Get Single Plugin Setting Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/02-api-TRP_Settings.md Retrieves the value of a specific plugin setting by its name. Provides a default value if the setting is not found, preventing errors. ```php public function get_setting(string $name, mixed $default = null): mixed ``` ```php $default_lang = $trp_settings->get_setting('default-language', 'en_US'); $floater_enabled = $trp_settings->get_setting('trp-ls-floater', 'no'); ``` -------------------------------- ### Access Plugin Components Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/INDEX.md Retrieve instances of core TranslatePress components like settings, languages, query, and URL converter. This is a common starting point for interacting with the plugin's functionality. ```php $trp = TRP_Translate_Press::get_trp_instance(); $settings = $trp->get_component('settings')->get_settings(); $languages = $trp->get_component('languages'); $query = $trp->get_component('query'); $url_converter = $trp->get_component('url_converter'); ``` -------------------------------- ### TRP_Translate_Press Class Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/INDEX.md Reference for the main singleton class of TranslatePress, including methods to get the plugin instance, access components, and execute hooks. ```APIDOC ## TRP_Translate_Press Class ### Description Provides access to the main TranslatePress plugin singleton and its core functionalities. ### Methods - **get_trp_instance()**: Retrieves the singleton instance of the TranslatePress plugin. - **get_component(string $component_name)**: Accesses specific components of the plugin (e.g., settings, query). - **run()**: Executes the necessary WordPress hooks and initializes plugin functionality. ``` -------------------------------- ### Responsive Styling for Language Switcher Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/11-api-TRP_Language_Switcher.md Example CSS media query to adjust the language switcher's layout for smaller screens (e.g., mobile devices). It changes the flex direction and width for the main switcher container. ```css @media (max-width: 768px) { .trp-language-switcher { flex-direction: column; width: 100%; } .trp-floater-ls { bottom: 10px; right: 10px; } } ``` -------------------------------- ### Registering Shortcodes in WordPress Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/09-hooks.md Shortcodes are registered using the `add_shortcode` function. This example shows how to register the 'trp_language' shortcode and a 'language-switcher' shortcode using a class method. ```php add_shortcode('trp_language', 'trp_language_content'); add_shortcode('language-switcher', [$language_switcher, 'language_switcher']); ``` -------------------------------- ### Restrict Settings Access with a Filter Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/07-configuration.md Example of using the 'trp_settings_capability' filter to restrict access to TranslatePress settings to users with the 'activate_plugins' capability instead of the default 'manage_options'. ```php add_filter('trp_settings_capability', function($cap) { return 'activate_plugins'; }); ``` -------------------------------- ### Constructor Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/05-api-TRP_Url_Converter.md Initializes the TRP_Url_Converter with plugin settings. ```APIDOC ## Constructor ### Description Initializes the TRP_Url_Converter with plugin settings. ### Method `__construct` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **settings** (array) - Required - Plugin settings from TRP_Settings ``` -------------------------------- ### Get URL Slug for Language Code Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/05-api-TRP_Url_Converter.md Retrieves the URL slug associated with a given language code. For example, 'fr_FR' might map to the slug 'fr'. ```php public function get_url_slug(string $language_code): string ``` ```php $slug = $url_converter->get_url_slug('fr_FR'); // 'fr' $slug = $url_converter->get_url_slug('en_US'); // 'en' (or configured value from settings) ``` -------------------------------- ### TRP_Query Constructor Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Initializes the TRP_Query class with plugin settings and sets up the database connection. ```APIDOC ## Constructor ### `__construct(array $settings)` Initializes the TRP_Query object with plugin settings. #### Parameters - **$settings** (array) - Required - Plugin settings from TRP_Settings. ``` -------------------------------- ### Get Last ID Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Returns the highest ID in a specified table, useful for optimization purposes. ```php public function get_last_id(string $table_name): int ``` -------------------------------- ### Initialize Settings Registration Hook Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/09-hooks.md Initializes settings registration and license handling. Used for registering trp_settings option, license activation/deactivation, and advanced/machine translation settings. ```php add_action('admin_init', [$settings, 'register_setting']); ``` -------------------------------- ### TRP_Query Constructor Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Initializes the TRP_Query class with plugin settings. It sets up the database connection using the global $wpdb object. ```php public function __construct(array $settings) { // Initializes: $this->db to global $wpdb } ``` -------------------------------- ### Get Constant for Not Translated Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Returns the integer constant value representing an untranslated entry (0). ```php public function get_constant_not_translated(): int { // Returns constant for untranslated entries (0) } ``` -------------------------------- ### Get Untranslated Strings Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Identifies and returns only the strings from the provided array that have not yet been translated into the target language. ```php public function get_untranslated_strings( array $strings_array, string $language_code ): array ``` -------------------------------- ### initialize_gettext_processing() Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/10-api-TRP_Translation_Manager.md Sets up the necessary hooks and filters for collecting and processing gettext strings. This method is hooked to the `init` action. ```APIDOC ## initialize_gettext_processing() ### Description Sets up gettext string collection and processing. ### Method Initializes processing ### Hooked To init action ### Prepares Gettext hooks and filters for capturing translated strings ``` -------------------------------- ### Get Constant for Human Reviewed Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Returns the integer constant value representing a human-reviewed entry (2). ```php public function get_constant_human_reviewed(): int { // Returns constant for human-reviewed entries (2) } ``` -------------------------------- ### Accessing TranslatePress Components Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/00-overview.md Demonstrates how to access various components of the TranslatePress plugin via its main singleton instance. This pattern is common for interacting with plugin functionalities. ```php get_component('settings'); $languages = $trp->get_component('languages'); $query = $trp->get_component('query'); ?> ``` -------------------------------- ### Get Constant for Machine Translated Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Returns the integer constant value representing a machine-translated entry (1). ```php public function get_constant_machine_translated(): int { // Returns constant for machine-translated entries (1) } ``` -------------------------------- ### Get Constant for Deprecated Block Type Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Returns the integer constant value for the deprecated block type (2). ```php public function get_constant_block_type_deprecated(): int { // Returns constant for deprecated block type (2) } ``` -------------------------------- ### Initialize Translation Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/01-api-TRP_Translate_Press.md Loads the plugin's text domain for localization, enabling translation of plugin strings. ```APIDOC ## init_translation() ### Description Loads the plugin text domain for localization. ### Method `public function init_translation(): void` ### Text Domain `translatepress-multilingual` ### Language Files Path `/languages/` ### Example ```php $trp->init_translation(); ``` ``` -------------------------------- ### Get Constant for Active Block Type Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Returns the integer constant value for the active translation block type (1). ```php public function get_constant_block_type_active(): int { // Returns constant for active translation block type (1) } ``` -------------------------------- ### Get All Translation Blocks Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Fetches all translation blocks, both active and deprecated, for a given language code. Results are returned as an associative array. ```php public function get_all_translation_blocks(string $language_code): array ``` -------------------------------- ### Accessing Plugin Settings Programmatically Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/07-configuration.md Demonstrates multiple ways to retrieve plugin settings, including accessing the main TRP_Settings component, directly querying WordPress options, and retrieving language switcher or machine translation configurations. ```php // Get main plugin instance $trp = TRP_Translate_Press::get_trp_instance(); // Method 1: Via TRP_Settings component $settings_obj = $trp->get_component('settings'); $all_settings = $settings_obj->get_settings(); $default_lang = $settings_obj->get_setting('default-language'); // Method 2: Direct WordPress options $all_settings = get_option('trp_settings', []); $default_lang = $all_settings['default-language'] ?? 'en_US'; // Method 3: Check language switcher options $ls_options = $settings_obj->get_language_switcher_options(); $current_style = $all_settings['shortcode-options']; // Method 4: Machine translation settings $mt_settings = $all_settings['trp_machine_translation_settings']; $is_enabled = $mt_settings['machine-translation'] === 'yes'; ``` -------------------------------- ### Get Constant for Regular String Block Type Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Returns the integer constant value for the regular string block type (0). ```php public function get_constant_block_type_regular_string(): int { // Returns constant for regular string block type (0) } ``` -------------------------------- ### Implement 'View as User Role' Functionality Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/10-api-TRP_Translation_Manager.md Implements the 'View as User Role' functionality, a pro feature. Hooked to the 'init' action. Allows translators to see content restricted to specific user roles. ```php public function trp_view_as_user(): void ``` -------------------------------- ### Get Current Language Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/INDEX.md Access the globally defined current language code. This constant is set by TranslatePress and indicates the language being viewed. ```php global $TRP_LANGUAGE; echo $TRP_LANGUAGE; // 'fr_FR' ``` -------------------------------- ### Get Default Machine Translation Settings Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/02-api-TRP_Settings.md Returns an array containing the default settings for machine translation. This can be filtered using 'trp_get_default_trp_machine_translation_settings'. ```php public function get_default_trp_machine_translation_settings(): array ``` ```php [ 'machine-translation' => 'no', 'translation-engine' => 'google_translate_v2', 'block-crawlers' => 'yes', 'machine_translation_counter_date' => date("Y-m-d"), 'machine_translation_counter' => 0, 'machine_translation_limit' => 1000000 ] ``` -------------------------------- ### get_language_switcher_options() Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/02-api-TRP_Settings.md Fetches all available display options for the language switcher. This method returns an associative array detailing various configurations for how the language switcher can be presented. ```APIDOC ## get_language_switcher_options() ### Description Returns all available language switcher display options. ### Method ```php public function get_language_switcher_options(): array ``` ### Returns Associative array of options: ```php [ 'full-names' => [ 'full_names' => true, 'short_names' => false, 'flags' => false, 'label' => 'Full Language Names' ], 'short-names' => [ 'full_names' => false, 'short_names' => true, 'flags' => false, 'label' => 'Short Language Names' ], 'flags-full-names' => [ 'full_names' => true, 'short_names' => false, 'flags' => true, 'label' => 'Flags with Full Language Names' ], 'flags-short-names' => [ 'full_names' => false, 'short_names' => true, 'flags' => true, 'label' => 'Flags with Short Language Names' ], 'only-flags' => [ 'full_names' => false, 'short_names' => false, 'flags' => true, 'label' => 'Only Flags' ] ] ``` ### Filter `trp_language_switcher_output` - Modify available options ### Example ```php $options = $trp_settings->get_language_switcher_options(); foreach ($options as $key => $option) { echo $option['label']; } ``` ``` -------------------------------- ### TRP_Settings Class Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/INDEX.md Documentation for managing plugin settings, including retrieving all settings or individual settings, and information on language switcher options. ```APIDOC ## TRP_Settings Class ### Description Manages all settings for the TranslatePress plugin. ### Methods - **get_settings()**: Retrieves all plugin settings as an associative array. - **get_setting(string $key, $default = null)**: Retrieves a single setting by its key, with an optional default value. ### Features - Language switcher options configuration. - Settings registration and sanitization processes. ``` -------------------------------- ### TranslatePress Component Hierarchy Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/INDEX.md Illustrates the singleton pattern and the relationship between the main TRP_Translate_Press class and its various components. ```text TRP_Translate_Press (Singleton) β”œβ”€β”€ TRP_Settings - Configuration management β”œβ”€β”€ TRP_Languages - Language data β”œβ”€β”€ TRP_Query - Database operations β”œβ”€β”€ TRP_Translation_Render - Frontend rendering β”œβ”€β”€ TRP_Translation_Manager - Editor interface β”œβ”€β”€ TRP_Url_Converter - Language URLs β”œβ”€β”€ TRP_Language_Switcher - UI switching β”œβ”€β”€ TRP_Machine_Translator - Auto-translation β”œβ”€β”€ TRP_Editor_Api_Regular_Strings - AJAX for strings β”œβ”€β”€ TRP_Editor_Api_Gettext_Strings - AJAX for gettext β”œβ”€β”€ TRP_Hooks_Loader - Hook management β”œβ”€β”€ TRP_Translation_Memory - Similar translations β”œβ”€β”€ TRP_Error_Manager - Error tracking └── (Various other components...) ``` -------------------------------- ### Get Gettext String Rows by IDs Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Retrieves gettext string rows from the database based on an array of their IDs and the specified language code. ```php public function get_gettext_string_rows_by_ids( array $id_array, string $language_code ): array ``` -------------------------------- ### Get All Translated Gettext Strings Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Retrieves only the translated gettext strings for a given language code. This is useful for filtering strings that have already been processed. ```php public function get_all_gettext_translated_strings(string $language_code): array ``` -------------------------------- ### get_settings() Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/02-api-TRP_Settings.md Retrieves the complete plugin settings array. This includes general settings and machine translation configurations stored in WordPress options. ```APIDOC ## get_settings() ### Description Returns complete plugin settings array. Settings are retrieved from WordPress options and merged with machine translation settings. ### Method ```php public function get_settings(): array ``` ### Returns Array with keys: - `default-language` (string) - Default site language code (e.g., 'en_US') - `translation-languages` (array) - Array of active translation language codes - `publish-languages` (array) - Array of published language codes - `native_or_english_name` (string) - 'english_name' or 'native_name' - `add-subdirectory-to-default-language` (string) - 'yes' or 'no' - `force-language-to-custom-links` (string) - 'yes' or 'no' - `trp-ls-floater` (string) - 'yes' or 'no' - `shortcode-options` (string) - Language switcher shortcode appearance option - `menu-options` (string) - Language switcher menu appearance option - `floater-options` (string) - Language switcher floating widget appearance option - `floater-position` (string) - 'bottom-right', 'bottom-left', 'top-right', or 'top-left' - `url-slugs` (array) - Mapping of language codes to URL slugs - `trp_advanced_settings` (array) - Advanced options - `trp_machine_translation_settings` (array) - Machine translation config ### Example ```php $settings = $trp_settings->get_settings(); echo $settings['default-language']; // 'en_US' foreach ($settings['translation-languages'] as $lang) { echo $lang; // 'en_US', 'fr_FR', etc. } ``` ``` -------------------------------- ### Get Gettext Table Name Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Returns the name of the gettext strings table for a specified language code. The format is `{blog_prefix}trp_gettext_{language_code}`. ```php public function get_gettext_table_name(string $language_code): string { // ... } ``` -------------------------------- ### Run Plugin Hooks Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/01-api-TRP_Translate_Press.md Executes all registered hooks to activate the plugin's core functionality. This method must be called for the plugin to operate. ```APIDOC ## run() ### Description Executes all registered hooks. Must be called to activate the plugin's functionality. ### Method `public function run(): void` ### Filters - `trp_allow_tp_to_run` (bool) - Can prevent hook execution if returns false. ### Note Some plugin code (like constructors) still executes even if this filter returns false. ### Example ```php $trp = TRP_Translate_Press::get_trp_instance(); $trp->run(); // Usually called from plugins_loaded hook in index.php ``` ``` -------------------------------- ### Initialize Translation Text Domain Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/01-api-TRP_Translate_Press.md Loads the plugin's text domain for localization, enabling translations of the plugin's strings. This is essential for multilingual support. ```php init_translation(); ?> ``` -------------------------------- ### enqueue_preview_scripts_and_styles() Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/10-api-TRP_Translation_Manager.md Loads assets required for the live preview mode within the editor. This method is hooked to the `wp_enqueue_scripts` action. ```APIDOC ## enqueue_preview_scripts_and_styles() ### Description Loads assets needed for live preview mode in editor. ### Method Loads assets ### Hooked To wp_enqueue_scripts action ### Used For When in editor preview mode to show translated content live ``` -------------------------------- ### Get Rows from Location Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Retrieves rows from a specific location for batch processing. Specify language code, limits, batch size, and columns to retrieve. ```php public function get_rows_from_location( string $language_code, int $inferior_limit, int $batch_size, array $columns_to_retrieve ): array ``` -------------------------------- ### Get All Gettext Strings Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Retrieves all gettext strings for a specified language code. The returned array includes id, original, translated, and domain for each string. ```php public function get_all_gettext_strings(string $language_code): array ``` -------------------------------- ### TRP_Language_Switcher Constructor Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/11-api-TRP_Language_Switcher.md Initializes the language switcher component with plugin settings and the main plugin instance. ```php public function __construct(array $settings, TRP_Translate_Press $trp_main) ``` -------------------------------- ### Verify Database Table Name Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/INDEX.md Get the dynamically generated name for a translation dictionary table. Table names are based on the source and target languages. ```php $query = $trp->get_component('query'); $table = $query->get_table_name('fr_FR', 'en_US'); // wp_trp_dictionary_en_us_fr_fr ``` -------------------------------- ### TranslatePress Settings Object Structure Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/06-types.md An array representing the configuration settings for TranslatePress, covering language, display, switcher, and URL options. ```php [ // Language Configuration 'default-language' => 'en_US', 'translation-languages' => ['en_US', 'fr_FR'], 'publish-languages' => ['en_US', 'fr_FR'], // Display Options 'native_or_english_name' => 'english_name', 'add-subdirectory-to-default-language' => 'no', 'force-language-to-custom-links' => 'yes', // Language Switcher Configuration 'trp-ls-floater' => 'yes', 'shortcode-options' => 'flags-full-names', 'menu-options' => 'flags-full-names', 'floater-options' => 'flags-full-names', 'floater-position' => 'bottom-right', // URL Configuration 'url-slugs' => [ 'en_US' => 'en', 'fr_FR' => 'fr', 'de_DE' => 'de' ], // Advanced Settings (merged from separate option) 'trp_advanced_settings' => [], // Machine Translation Settings (merged from separate option) 'trp_machine_translation_settings' => [ 'machine-translation' => 'no', 'translation-engine' => 'google_translate_v2', 'block-crawlers' => 'yes', 'machine_translation_counter_date' => '2024-05-28', 'machine_translation_counter' => 0, 'machine_translation_limit' => 1000000 ] ] ``` -------------------------------- ### Get Specific Language Names Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/04-api-TRP_Languages.md Retrieves the formatted names for a specified list of language codes. This is useful for displaying language options or details to the user. ```php $names = $languages->get_language_names(['fr_FR', 'de_DE']); ``` -------------------------------- ### Shortcodes and Template Functions Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/INDEX.md Reference for available shortcodes and template functions for displaying language switchers and conditional content. ```APIDOC ## Shortcodes and Template Functions ### Description Provides shortcodes and template functions for integrating TranslatePress features into WordPress themes and content. ### Shortcodes - **`[language-switcher]`**: Displays the language switcher. - **`[trp_language]`**: Conditionally displays content based on the current language. ### Template Functions - **`trp_the_language_switcher()`**: Function to programmatically display the language switcher. ### Usage - Includes examples for styling and customization. ``` -------------------------------- ### Optimizing Shortcode Usage: Using Switcher Sparingly Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/08-shortcodes.md Avoid placing the language switcher shortcode within loops, as it can be performance-intensive. It's recommended to place it once in the header or footer. ```php // Switcher can be heavy in loops // Place once in header/footer instead of in loop echo do_shortcode('[language-switcher]'); ``` -------------------------------- ### Get All Available Languages Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/04-api-TRP_Languages.md Retrieves all available languages and iterates through them to display their codes and English names. This is useful for populating language selection dropdowns or lists. ```php $trp = TRP_Translate_Press::get_trp_instance(); $languages = $trp->get_component('languages'); // Get all available languages $all_langs = $languages->get_languages('english_name'); foreach ($all_langs as $code => $name) { echo "$code: $name\n"; } ``` -------------------------------- ### Get Gettext String Rows by Original Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Retrieves gettext string rows from the database based on an array of original string values and the specified language code. ```php public function get_gettext_string_rows_by_original( array $original_array, string $language_code ): array ``` -------------------------------- ### enqueue_language_switcher_scripts() Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/11-api-TRP_Language_Switcher.md Loads the necessary JavaScript and CSS files for the language switcher's frontend functionality. This includes scripts for the switcher itself, styling, and potentially third-party libraries like Select2. ```APIDOC ## enqueue_language_switcher_scripts() ### Description Loads JavaScript and CSS for language switcher functionality. ### Method public function enqueue_language_switcher_scripts(): void ### Hooked To `wp_enqueue_scripts` action ### Scripts/Styles Loaded - Language switcher JavaScript - Language switcher CSS - Select2 library (if using dropdown) - Animation effects ### Files - `assets/js/trp-frontend-script.js` - `assets/css/trp-front-end-style.css` ``` -------------------------------- ### TRP_Language_Switcher Class Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/INDEX.md Documentation for the language switching UI, covering generation of the switcher HTML, menu integration, and display options. ```APIDOC ## TRP_Language_Switcher Class ### Description Generates and manages the user interface for switching between languages on the frontend. ### Methods - **language_switcher()**: Generates the HTML output for the language switcher. ### Features - Supports floating widget display. - Integrates with WordPress navigation menus. - Provides options for display and styling. - Ensures mobile responsiveness. ``` -------------------------------- ### Get All Available Language Codes Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/04-api-TRP_Languages.md Retrieves a simple array containing all available language codes supported by WordPress. This is useful for iterating through all possible language identifiers. ```PHP $trp_languages = $trp->get_component('languages'); $all_codes = $trp_languages->get_all_language_codes(); foreach ($all_codes as $code) { echo $code; } ``` -------------------------------- ### Main Plugin Initialization Hook Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/09-hooks.md The primary hook for initializing the TranslatePress plugin very early in the WordPress loading process. It handles loading files, initializing the singleton, setting up components, and registering hooks. ```php add_action('plugins_loaded', 'trp_run_translatepress_hooks', 1); ``` -------------------------------- ### get_setting() Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/02-api-TRP_Settings.md Retrieves the value of a single plugin setting by its name. If the setting is not found, it returns a specified default value. ```APIDOC ## get_setting() ### Description Returns value of a single setting or provided default if not found. ### Method ```php public function get_setting(string $name, mixed $default = null): mixed ``` ### Parameters #### Path Parameters - **$name** (string) - Required - Setting key name - **$default** (mixed) - Optional - Default value if setting not found ### Returns Mixed - Setting value or default ### Example ```php $default_lang = $trp_settings->get_setting('default-language', 'en_US'); $floater_enabled = $trp_settings->get_setting('trp-ls-floater', 'no'); ``` ``` -------------------------------- ### Initialize Gettext Translation Tracking Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/10-api-TRP_Translation_Manager.md Creates a global variable to track gettext translations. This method is hooked to the 'init' action and initializes `$GLOBALS['trp_gettext_translations']`. ```php public function create_gettext_translated_global(): void ``` -------------------------------- ### enqueue_scripts_and_styles() Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/10-api-TRP_Translation_Manager.md Loads necessary JavaScript and CSS files for the translation editor interface. This method is hooked to the `trp_translation_manager_footer` action. ```APIDOC ## enqueue_scripts_and_styles() ### Description Loads JavaScript and CSS for translation editor interface. ### Method Loads assets ### Hooked To trp_translation_manager_footer action ### Scripts Loaded - Translation editor Vue.js application - Axios HTTP client - String similarity library - Other editor utilities ### Styles Loaded - Editor interface CSS - Theme styling - Layout CSS ``` -------------------------------- ### Plugin Constants Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/INDEX.md Key constants related to the plugin's directory, URL, version, and slug. These are useful for referencing plugin files and identifying the plugin. ```php TRP_PLUGIN_DIR Plugin directory path TRP_PLUGIN_URL Plugin directory URL TRP_PLUGIN_VERSION Current version (1.6.1) TRP_PLUGIN_SLUG 'translatepress-multilingual' TRP_PLUGIN_BASE Plugin basename for hooks ``` -------------------------------- ### Get String Rows Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Retrieves complete row data for strings, identified either by their database IDs or original string values. Supports different output formats. ```php public function get_string_rows( array $id_array, array $original_array, string $language_code, int $output = OBJECT_K ): array ``` -------------------------------- ### Get Existing Translations Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Retrieves translations for a given array of original strings in a specific language. Results are returned as an associative array. Optionally filter by block type. ```php public function get_existing_translations( array $strings_array, string $language_code, int $block_type = null ): array|false ``` ```php $strings = ['Hello', 'Goodbye']; $translations = $query->get_existing_translations($strings, 'fr_FR'); // ['Hello' => (object)['translated' => 'Bonjour', 'status' => 2]] ``` -------------------------------- ### Access Machine Translation Settings Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/07-configuration.md Demonstrates retrieving machine translation settings and checking if automatic translation is enabled. ```php $settings = get_option('trp_machine_translation_settings'); if ($settings['machine-translation'] === 'yes') { $engine = $settings['translation-engine']; } ``` -------------------------------- ### Get Translation Table Name Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/03-api-TRP_Query.md Retrieves the database table name for a specific language pair. The table name follows the format: `{prefix}trp_dictionary_{default_lang}_{language_code}`. ```php public function get_table_name(string $language_code, string $default_language = null): string { // ... } $table = $query->get_table_name('fr_FR', 'en_US'); // wp_trp_dictionary_en_us_fr_fr ``` -------------------------------- ### Language Switcher Display Options Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/06-types.md Defines various configurations for displaying language switchers, including options for showing full/short names and flags. Each option includes a localized label for UI display. ```php [ 'full-names' => [ 'full_names' => true, 'short_names' => false, 'flags' => false, 'label' => 'Full Language Names' // Localized string ], 'short-names' => [ 'full_names' => false, 'short_names' => true, 'flags' => false, 'label' => 'Short Language Names' ], 'flags-full-names' => [ 'full_names' => true, 'short_names' => false, 'flags' => true, 'label' => 'Flags with Full Language Names' ], 'flags-short-names' => [ 'full_names' => false, 'short_names' => true, 'flags' => true, 'label' => 'Flags with Short Language Names' ], 'only-flags' => [ 'full_names' => false, 'short_names' => false, 'flags' => true, 'label' => 'Only Flags' ] ] ``` -------------------------------- ### Get ISO Codes for Active Languages Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/04-api-TRP_Languages.md Fetches the ISO codes for a given array of active language codes. This is often required for integration with translation engines or other services. ```php $active_langs = ['en_US', 'fr_FR', 'de_DE']; $iso_codes = $languages->get_iso_codes($active_langs); // ['en_US' => 'en', 'fr_FR' => 'fr', 'de_DE' => 'de'] ``` -------------------------------- ### Access TranslatePress Settings Object Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/07-configuration.md Shows how to access the settings object and individual settings from the TranslatePress components in code. ```php $settings_obj = $trp->get_component('settings'); $settings = $settings_obj->get_settings(); $default_lang = $settings_obj->get_setting('default-language', 'en_US'); ``` -------------------------------- ### File Organization Structure Source: https://github.com/phpzio/translatepress/blob/master/_autodocs/INDEX.md Overview of the TranslatePress plugin's directory structure. This helps in understanding where different modules and documentation files are located. ```text output/ β”œβ”€β”€ INDEX.md (this file) ................. Navigation and overview β”œβ”€β”€ 00-overview.md ....................... Project architecture β”œβ”€β”€ 01-api-TRP_Translate_Press.md ....... Main plugin class β”œβ”€β”€ 02-api-TRP_Settings.md .............. Settings management β”œβ”€β”€ 03-api-TRP_Query.md ................. Database operations (40+ methods) β”œβ”€β”€ 04-api-TRP_Languages.md ............ Language management β”œβ”€β”€ 05-api-TRP_Url_Converter.md ........ URL/hreflang handling β”œβ”€β”€ 06-types.md ......................... Constants and data types β”œβ”€β”€ 07-configuration.md ................. WordPress options reference β”œβ”€β”€ 08-shortcodes.md .................... Shortcodes and template functions β”œβ”€β”€ 09-hooks.md ......................... WordPress hooks and filters β”œβ”€β”€ 10-api-TRP_Translation_Manager.md .. Editor interface └── 11-api-TRP_Language_Switcher.md ... Language switching UI ```