-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
90 lines (76 loc) · 2.89 KB
/
functions.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
<?php
add_filter( 'the_content_more_link', 'hw_set_frontend_permalink', 10, 2 );
function hw_set_frontend_permalink( $more_link, $more_link_text ) {
global $post;
$permalink = get_permalink( $post->ID );
$blogurl = get_bloginfo('url');
$frontend = get_option('frontend_url', $blogurl);
$url = str_replace($blogurl, $frontend, $permalink);
return "<a class=\"more-link\" href=\"{$url}\">" . $more_link_text . "</a>";
}
// apply_filters('post_link', $permalink, $post, $leavename);
add_filter('post_link', 'hw_permalink', 10, 3);
function hw_permalink($permalink, $post, $leavename) {
if (is_user_logged_in()) {
return $permalink;
} else {
$blogurl = get_bloginfo('url');
$frontend = get_option('frontend_url', $blogurl);
$url = preg_replace('{'.$blogurl.'(/blog)?}', $frontend, $permalink);
return $url;
}
}
add_action('template_redirect', 'hw_redirect');
function hw_redirect() {
// bail out if this is a json request
$json = get_query_var('json');
if ($json) {
// error_log('JSON request: ' . print_r($json, true));
return;
}
$blogurl = get_bloginfo('url');
$frontend = get_option('frontend_url');
if (!$frontend || $blogurl === $frontend) {
// error_log('No frontend URL set.');
return;
}
$protocol = $_SERVER['HTTPS'] ? "https" : "http";
$uri = "$protocol://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
// error_log('Request: ' . $uri);
if (!is_user_logged_in()) {
// redirect non-logged-in users to django frontend
$redirect_to = preg_replace('{'.$blogurl.'(/blog)?}', $frontend, $uri);
// error_log('Redirect to: ' . $redirect_to);
if ($redirect_to === $uri) return;
wp_redirect($redirect_to, 301);
exit;
}
}
add_action( 'admin_menu', 'hw_add_options_page' );
function hw_add_options_page() {
add_options_page( 'Homicide Watch', 'Homicide Watch', 'manage_options',
'hwdc', 'hw_options_page' );
}
function hw_options_page() { ?>
<div>
<h2>Homicide Watch settings</h2>
<form action="options.php" method="post">
<?php settings_fields('hwdc'); ?>
<?php do_settings_sections('hwdc'); ?>
<input name="Submit" class="button-primary" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />
</form>
</div> <?php
}
add_action('admin_init', 'hw_settings_init');
function hw_settings_init() {
add_settings_section( 'hwdc', 'hwdc', 'hw_section_callback', 'hwdc');
add_settings_field( 'frontend_url', 'Frontend URL',
'hw_frontend_url_callback', 'hwdc', 'hwdc');
register_setting('hwdc', 'frontend_url');
}
function hw_section_callback() {}
function hw_frontend_url_callback() {
$option = get_option( 'frontend_url' );
echo "<p><input type='text' value='$option' name='frontend_url' /></p>";
}
?>