WordPress как на ладони
wordpress jino

Мультиязычный плагин. Изменение $_SESSION через js

Как изменить значение $_SESSION['lang_val'] при клике на кнопку?

<?
/*
* Plugin Name: zTranslate
*/
session_start();
$_SESSION['lang_val'] = "en";

add_action('wp_ajax_zt_send', 'zt_callback');
add_action('wp_ajax_nopriv_zt_send', 'zt_callback');
add_action( 'wp_enqueue_scripts', 'zt_add_scripts');
add_filter('the_content', 'zt_change_lang');

function zt_add_scripts() {
	$script_url = plugins_url( '/zTranslate-js.js', __FILE__ );

	wp_enqueue_script('zTranslate-js', $script_url, array('jquery'));

	wp_localize_script( 'zTranslate-js', 'info', array(
		'ajaxurl' => admin_url( 'admin-ajax.php' )
	) );
}

function zt_callback($the_content) {
	$_SESSION['lang_val'] = $_REQUEST['lang_val'];
	echo $_SESSION['lang_val'];
	wp_die();
}

function zt_change_lang($the_content) {
	$lang_val = $_SESSION['lang_val'];
	$lang_post_en = stripos($the_content,"[:en]") + 5;
	$lang_post_ru = stripos($the_content,"[:ru]") + 5;

	if($lang_val == "ru") {
		if ( $lang_post_ru > $lang_post_en ) {
			$distance = $lang_post_ru - $lang_post_en - 5;
			$the_content = substr( $the_content, $lang_post_en, $distance );
		}
		$distance = $lang_post_en - $lang_post_ru - 5;
		$the_content = substr( $the_content, $lang_post_ru, $distance );
	}

	if($lang_val == "en") {
		if ( $lang_post_ru > $lang_post_en ) {
			$distance = $lang_post_ru - $lang_post_en - 5;
			$the_content = substr( $the_content, $lang_post_en, $distance );
			$dist1 = $distance;
		}
		if ($lang_post_ru < $lang_post_en) {
			$distance = $lang_post_en - $lang_post_ru - 5;
			$the_content = substr( $the_content, $lang_post_en, $distance );
			$dist2 = $distance;
		}
	}

	return $the_content;
}

zTranslate.js

jQuery(document).ready(function ($) {
		$(".widget_categories").append('<a href="#" class="btn_change_lang">ru</a>');

		if(typeof(localStorage['text']) !== "undefined")
			$('.btn_change_lang').text(localStorage['text']);

		var lang_val =  localStorage['text'];

		var data = {
			action: 'zt_send',
			lang_val: lang_val
		};

		var ajaxurl = info.ajaxurl;
		jQuery.post( ajaxurl, data, function(response) {
			alert(response);
		});

		$('.btn_change_lang').on("click",function (e) {
			e.preventDefault();
			var text = $(this).text();

			if(text === "ru")
				text = "en";
			else
				text = "ru";

			$('.btn_change_lang').html(text);

			localStorage.setItem('text', text);
		});
});
Zeng 27 дней назад

Решил задачку как-то так:

<?
/*
* Plugin Name: zTranslate
*/
session_start();
if(empty($_SESSION['lang_val']))
	$_SESSION['lang_val'] = "ru";

add_action('wp_ajax_zt_send', 'zt_callback');
add_action('wp_ajax_nopriv_zt_send', 'zt_callback');
add_action( 'wp_enqueue_scripts', 'zt_add_scripts');
add_filter('the_title', 'zt_change_lang',10);
add_filter('the_content', 'zt_change_lang',20);

function zt_add_scripts() {
	$script_url = plugins_url( '/zTranslate-js.js', __FILE__ );

	wp_enqueue_script('zTranslate-js', $script_url, array('jquery'));

	wp_localize_script( 'zTranslate-js', 'info', array(
		'ajaxurl' => admin_url( 'admin-ajax.php' )
	) );
}

function zt_callback() {
	$_SESSION['lang_val'] = $_REQUEST['lang_val'];
	wp_die();
}

function zt_change_lang($the_content) {
	$lang_val = $_SESSION['lang_val'];
	$lang_post_en = stripos($the_content,"[:en]") + 5;
	$lang_post_ru = stripos($the_content,"[:ru]") + 5;

	if($lang_val == "ru") {
		if ( $lang_post_ru > $lang_post_en ) {
			$distance = $lang_post_ru - $lang_post_en - 5;
			$the_content = substr( $the_content, $lang_post_en, $distance );
		}
		$distance = $lang_post_en - $lang_post_ru - 5;
		$the_content = substr( $the_content, $lang_post_ru, $distance );
	}

	if($lang_val == "en") {
		if ( $lang_post_ru > $lang_post_en ) {
			$distance = $lang_post_ru - $lang_post_en - 5;
			$the_content = substr( $the_content, $lang_post_en, $distance );
			$dist1 = $distance;
		}
		if ($lang_post_ru < $lang_post_en) {
			$distance = $lang_post_en - $lang_post_ru - 5;
			$the_content = substr( $the_content, $lang_post_en, $distance );
			$dist2 = $distance;
		}
	}

	return $the_content;
}

zTranslate-js.js

jQuery(document).ready(function ($) {
	window.onload = function () {
		$(".widget_categories").append('<a href="#" class="btn_change_lang">ru</a>');

		if(typeof(localStorage['text']) !== "undefined")
			$('.btn_change_lang').text(localStorage['text']);

		//console.log(localStorage['text']);

		$('.btn_change_lang').on("click",function (e) {
			e.preventDefault();
			var text = $(this).text();
			//console.log('2' + text);

			if(text === "ru")
				text = "en";
			else
				text = "ru";
			//console.log('3' + text);
			$('input[name="lang_val"]').val(text);

			localStorage.setItem('text', text);

			$(this).text(localStorage['text']);

			var data = {
				action: 'zt_send',
				lang_val: text
			};

			var ajaxurl = info.ajaxurl;
			jQuery.post( ajaxurl, data);
			setInterval("location.reload();", 500);
		});
	};
});
0
Zeng месяц назад

Нет ответов на этот вопрос.

На вопросы могут отвечать только зарегистрированные пользователи. Регистрация. Вход.