<?php
/*
* Plugin Name: Google Translate
* Plugin URI: https://example.com/google-translate
* Description: A plugin that uses Google Translate to translate content on your website.
* Version: 1.0
* Author: Your Name
* Author URI: https://example.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: google-translate
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Load the Google Translate API library.
*/
require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
/**
* Load the plugin text domain.
*/
function google_translate_load_textdomain() {
load_plugin_textdomain( 'google-translate', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'plugins_loaded', 'google_translate_load_textdomain' );
/**
* Add a settings page for the plugin.
*/
function google_translate_add_settings_page() {
add_options_page(
__( 'Google Translate', 'google-translate' ),
__( 'Google Translate', 'google-translate' ),
'manage_options',
'google-translate',
'google_translate_render_settings_page'
);
}
add_action( 'admin_menu', 'google_translate_add_settings_page' );
/**
* Render the settings page.
*/
function google_translate_render_settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.', 'google-translate' ) );
}
?>
<div class="wrap">
<h1><?php _e( 'Google Translate Settings', 'google-translate' ); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields( 'google_translate_options' );
do_settings_sections( 'google-translate' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register the plugin settings.
*/
function google_translate_register_settings() {
register_setting(
'google_translate_options',
'google_translate_options',
'google_translate_sanitize_options'
);
add_settings_section(
'google_translate_section_api_key',
__( 'API Key', 'google-translate' ),
'google_translate_render_section_api_key',
'google-translate'
);
add_settings_field(
'google_translate_field_api_key',
__( 'API Key', 'google-translate' ),
'google_translate_render_field_api_key',
'google-translate',
'google_translate_section_api_key'
);
}
add_action( 'admin_init', 'google_translate_register_settings' );
/**
* Render the API Key section.
*/
function google_translate_render_section_api_key() {
?>
<p><?php _e( 'Enter your Google Translate API key here.', 'google-translate' ); ?></p>
<?php
}
/**
* Render the API Key field.
*/
function google_translate_render_field_api_key() {
$options = get_option( 'google_translate_options' );
$api_key = isset( $options['api_key'] ) ? $options['api_key'] : '';
?>
<input type="text" name="google_translate_options[api_key]" value="<?php echo esc_attr( $api_key ); ?>">
<?php
}
/**
* Sanitize the plugin options.
*
* @param array $input
* @return array
*/
function google_translate_sanitize_options( $input ) {
$output = array();
if ( isset( $input['api_key'] ) ) {
$output['api_key'] = sanitize_text_field( $input['api_key'] );
}
return $output;
}
/**
* Translate a string of text.
*
* @param string $text
* @param string $source_language
* @param string $target_language
* @return string
*/
function google_translate_translate( $text, $source_language, $target_language ) {
$options = get_option( 'google_translate_options' );
$api_key = isset( $options['api_key'] ) ? $options['api_key'] : '';
if ( ! $api_key ) {
return $text;
}
try {
$client = new Google_Client();
$client->setDeveloperKey( $api_key );
$translate = new Google_Service_Translate( $client );
$result = $translate->translate( $text, $target_language, $source_language );
return $result['translatedText'];
} catch ( Exception $e ) {
return $text;
}
}
This code creates a plugin that adds a settings page to the WordPress admin area, where users can enter their Google Translate API key. The plugin also includes a function for translating a string of text from one language to another using the Google Translate API.