-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-multilingual-template-hierarchy.php
128 lines (95 loc) · 3.6 KB
/
wp-multilingual-template-hierarchy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/*
Plugin Name: Multilingual Template Hierarchy
Plugin URI: https://github.com/resulto-admin/wordpress-multilingual-template-hierarchy
Description: Enables theme developers using WPML to create type-slug.php templates in the default language. For example, if you create page-my-english-slug.php template, it will be used for the same page in other languages.
Version: 1.0.1
Author: Resulto Developpement Web Inc.
Author URI: http://www.resulto.ca
License: GPL3
License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
*/
// TODO Refactor and extract common code :-)
// TODO Add support for translated taxonomies.
add_filter( 'page_template', 'multi_lingual_page_template');
function multi_lingual_page_template($template) {
global $post;
$template_name = basename($template);
if (!($template_name === 'page.php' || $template_name === 'singular.php' || $template_name === 'index.php')) {
return $template;
}
$current_lang = apply_filters('wpml_current_language', NULL);
$default_lang = apply_filters('wpml_default_language', NULL);
if ($current_lang === $default_lang) {
return $template;
}
$id_in_default_lang = apply_filters('wpml_object_id', $post->ID, 'page', FALSE, $default_lang);
if (!$id_in_default_lang) {
return $template;
}
$temp_post = get_post($id_in_default_lang);
if (!$temp_post) {
return $template;
}
$template_name = 'page-' . $temp_post->post_name . '.php';
$template_path = locate_template($template_name);
if ($template_path) {
return $template_path;
}
return $template;
}
add_filter( 'category_template', 'multi_lingual_category_template');
function multi_lingual_category_template($template) {
$template_name = basename($template);
if (!($template_name === 'category.php' || $template_name === 'archive.php' || $template_name === 'index.php')) {
return $template;
}
$current_lang = apply_filters('wpml_current_language', NULL);
$default_lang = apply_filters('wpml_default_language', NULL);
if ($current_lang === $default_lang) {
return $template;
}
$category_id = get_query_var('cat');
$id_in_default_lang = apply_filters('wpml_object_id', $category_id, 'category', FALSE, $default_lang);
if (!$id_in_default_lang) {
return $template;
}
$category = get_category($id_in_default_lang);
if (!$category) {
return $template;
}
$template_name = 'category-' . $category->slug . '.php';
$template_path = locate_template($template_name);
if ($template_path) {
return $template_path;
}
return $template;
}
add_filter( 'tag_template', 'multi_lingual_tag_template');
function multi_lingual_tag_template($template) {
$template_name = basename($template);
if (!($template_name === 'tag.php' || $template_name === 'archive.php' || $template_name === 'index.php')) {
return $template;
}
$current_lang = apply_filters('wpml_current_language', NULL);
$default_lang = apply_filters('wpml_default_language', NULL);
if ($current_lang === $default_lang) {
return $template;
}
$tag_id = get_query_var('tag');
$id_in_default_lang = apply_filters('wpml_object_id', $tag_id, 'tag', FALSE, $default_lang);
if (!$id_in_default_lang) {
return $template;
}
$tag = get_tag($id_in_default_lang);
if (!$tag) {
return $template;
}
$template_name = 'tag-' . $tag->slug . '.php';
$template_path = locate_template($template_name);
if ($template_path) {
return $template_path;
}
return $template;
}
?>