-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalternate-mobile-tag.php
executable file
·47 lines (45 loc) · 1.34 KB
/
alternate-mobile-tag.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
<?php
/*
Plugin Name: Alternate Mobile Tag
Description: Add mobile site url in HEAD sectiomn as Link tag with rel=alternate. Mobile url example http://m.example.com
Plugin URI: http://wpdeveloper.net
Author: WPDeveloper.net
Author URI: http://wpdeveloper.net
Version: 1.1
License: GPL2+
Text Domain: alternate-mobile-tag
Min WP Version: 2.5.0
Max WP Version: 4.2
*/
/**
* Determine the url of current visited page
* @return string
**/
function alternate_curPageURL()
{
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://m.";//for mobile site
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
/**
* Add some HTML <link> tag inside <HEAD> tag
* @return void
* @Usage with wp_head action hook
**/
function add_alternate_link_tag()
{
if(is_home() || (is_single()&& !is_attachment()) || is_page() || is_tag() || is_category()){
?>
<link rel="alternate" media="only screen and (max-width: 640px)" href="<?php echo esc_url(alternate_curPageURL()) ?>" >
<link rel="alternate" media="handheld" href="<?php echo esc_url(alternate_curPageURL()) ?>" >
<?php
}
}
add_action("wp_head", "add_alternate_link_tag");
?>