Instantly share code, notes, and snippets.
Last active
August 29, 2018 19:02
-
Star
(1)
1
You must be signed in to star a gist -
Fork
(2)
2
You must be signed in to fork a gist
-
Save brasofilo/6524329 to your computer and use it in GitHub Desktop.
WordPress plugin to deal with language cookies. Create one page for each language. Then a menu with them. Configure the arguments in the plugin file. Than do your thing in your theme with $_COOKIE[cookie_name].
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Language Cookie | |
* Plugin URI: http://wordpress.stackexchange.com/q/113662/12615 | |
* Description: Create one page for each language. Then a menu with them. Configure the arguments in the plugin file. Than do your thing in your theme with $_COOKIE[cookie_name]. | |
* Version: 0.1 | |
* Requires: PHP 5.3+ | |
* Author: Rodolfo Buaiz | |
* Author URI: http://www.rodbuaiz.com | |
*/ | |
add_action( 'plugins_loaded', function() { | |
$args = array( | |
# Pair of cookie value => page-slug or page-id | |
'translation_pages' => array( | |
'en' => 'english', | |
'ar' => 'arabic', | |
'pt' => 5594 | |
), | |
# Site where plugin is running | |
'domain' => 'plugins.dev', | |
# Name of the croissant stored in your system | |
'cookie_name' => 'my-site-language', | |
# Appended to the language menus, used to get back to the same origin page. | |
# # Ex:http:example.com/somepage?lingo=http%3A%2F%2Fexample.com%2Fsomepage | |
'query_var' => 'lingo', | |
# Check for oscilations in code | |
'debug' => false, | |
); | |
new B5F_Cookie_Translate( $args ); | |
} ); | |
class B5F_Cookie_Translate | |
{ | |
/** | |
* Holds the plugin options | |
* | |
* @var array | |
*/ | |
protected $params; | |
/** | |
* Array of cookie names | |
* | |
* @var array | |
*/ | |
protected $cookies; | |
/** | |
* Array of page names (aligned with $cookies) | |
* | |
* @var array | |
*/ | |
protected $lingo_pages; | |
/** | |
* Set up basic information | |
* | |
* @param array $options | |
* @return void | |
*/ | |
public function __construct( array $options ) | |
{ | |
$defaults = array( | |
'translation_pages' => array( ), | |
'debug' => false, | |
'domain' => 'example.com', | |
'cookie_name' => 'hello_cookie', | |
'query_var' => 'change_lenguage', | |
); | |
$this->params = array_merge( $defaults, $options ); | |
$this->cookies = array_keys( $this->params['translation_pages'] ); | |
$this->lingo_pages = array_values( $this->params['translation_pages'] ); | |
add_filter( 'wp_get_nav_menu_items', array( $this, 'nav_items' ), 11, 3 ); | |
add_action( 'template_redirect', array( $this, 'redirect' ) ); | |
if( $this->params['debug'] && !is_admin() ) | |
add_filter( 'the_content', array( $this, 'debug' ) ); | |
} | |
/** | |
* Add query var to translation menu items | |
* | |
* @param object $items | |
* @param object $menu | |
* @param array $args | |
* @return object | |
*/ | |
public function nav_items( $items, $menu, $args ) | |
{ | |
if( is_admin() ) | |
return $items; | |
# http://kovshenin.com/2012/current-url-in-wordpress/ | |
global $wp; | |
$current_url = urlencode( home_url( $wp->request ) ); | |
# Build array with Page IDs | |
$object_ids = array( ); | |
foreach( $this->params['translation_pages'] as $page ) | |
{ | |
$get_page = is_int( $page ) ? get_page( $page ) : get_page_by_path( $page ); | |
if( $get_page ) | |
$object_ids[] = $get_page->ID; | |
} | |
# Build return array | |
$new_items = array( ); | |
foreach( $items as $item ) | |
{ | |
# object_id is the only reference to the original item | |
if( !empty( $item->url ) && in_array( $item->object_id, $object_ids ) ) | |
$item->url = $item->url . "?{$this->params['query_var']}=$current_url"; | |
$new_items[] = $item; | |
} | |
return $new_items; | |
} | |
/** | |
* Template redirect action | |
* | |
* @return void | |
*/ | |
public function redirect() | |
{ | |
$c_name = $this->params['cookie_name']; | |
# No cookie set, let's do it | |
if( !isset( $_COOKIE[$c_name] ) ) | |
$this->set_cookie( $this->cookies[0] ); | |
# One of our pages, change cookie | |
if( is_page( $this->lingo_pages ) ) | |
{ | |
$q_var = $this->params['query_var']; | |
$goto = isset( $_GET[$q_var] ) ? esc_url( urldecode( $_GET[$q_var] ) ) : ''; | |
foreach( $this->params['translation_pages'] as $lingo => $page ) | |
{ | |
if( is_page( $page ) && $_COOKIE[$c_name] != $lingo ) | |
$this->set_cookie( $lingo ); | |
} | |
$this->do_redirect( $goto ); | |
} | |
} | |
/** | |
* Set cookie | |
* | |
* @param string $lingo Cookie value | |
* @return void | |
*/ | |
private function set_cookie( $lingo ) | |
{ | |
$domain = '.' . $this->params['domain']; | |
setcookie( $this->params['cookie_name'], $lingo, time() + 3600 * 24 * 365 * 3, '/', $domain ); | |
} | |
/** | |
* Redirect browser | |
* | |
* @param string $where URL for redirection | |
* @return void | |
*/ | |
private function do_redirect( $where = '' ) | |
{ | |
$where = empty( $where ) ? home_url() : $where; | |
wp_redirect( $where ); | |
exit(); | |
} | |
/** | |
* Enable printing of the cookie name with the_content | |
* | |
* @param string $content | |
* @return string | |
*/ | |
public function debug( $content ) | |
{ | |
$text = isset( $_COOKIE[$this->params['cookie_name']] ) ? $_COOKIE[$this->params['cookie_name']] : 'Cookie not set'; | |
return "<h1>$text</h1>$content"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment