-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.php
126 lines (89 loc) · 2.78 KB
/
plugin.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
<?php
/*
Plugin Name: Custom Header & Footer
Plugin URI: http://scriptgeni.com/
Description: A Yourls plugin administration page to add custom header and footer code
Version: 1.0
Author: ScriptGeni
Author URI: http://scriptgeni.com
*/
// No direct call
if( !defined( 'YOURLS_ABSPATH' ) ) die();
// Add the inline style
yourls_add_action( 'html_head', 'geni_yourls_bootstrap_head' );
function geni_yourls_bootstrap_head() {
$head_option = yourls_get_option( 'head_option' );
echo <<<CSS
$head_option
CSS;
}
// Add the inline style
yourls_add_action( 'html_footer', 'geni_yourls_bootstrap_foot' );
function geni_yourls_bootstrap_foot() {
$foot_option = yourls_get_option( 'foot_option' );
echo <<<HTML
$foot_option
HTML;
}
// Register our plugin admin page
yourls_add_action( 'plugins_loaded', 'geni_yourls_head_foot_code' );
function geni_yourls_head_foot_code() {
yourls_register_plugin_page( 'head_foot_page', 'Header & Footer Admin Page', 'geni_yourls_head_foot_do_page' );
// parameters: page slug, page title, and function that will display the page itself
}
// Display admin page
function geni_yourls_head_foot_do_page() {
// Check if a form was submitted
if( isset( $_POST['head_option'] ) ) {
// Check nonce
yourls_verify_nonce( 'head_foot_page' );
// Process form
geni_yourls_head_foot_update_option();
}
if( isset( $_POST['foot_option'] ) ) {
// Check nonce
yourls_verify_nonce( 'head_foot_page' );
// Process form
geni_yourls_head_foot_update_option();
}
// Get value from database
$head_option = yourls_get_option( 'head_option' );
$foot_option = yourls_get_option( 'foot_option' );
// Create nonce
$nonce = yourls_create_nonce( 'head_foot_page' );
echo <<<HTML
<h2>Header code</h2>
<p>Add extra code to the head</p>
<form method="post">
<input type="hidden" name="nonce" value="$nonce" />
<p><textarea name="head_option">$head_option</textarea></p>
<p><input type="submit" value="Update value" /></p>
</form>
<h2>Footer code</h2>
<p>Add extra code to your footer</p>
<form method="post">
<input type="hidden" name="nonce" value="$nonce" />
<p><textarea name="foot_option">$foot_option</textarea></p>
<p><input type="submit" value="Update value" /></p>
</form>
HTML;
}
// Update option in database
function geni_yourls_head_foot_update_option() {
$head = $_POST['head_option'];
if( $head ) {
// Validate head_option. ALWAYS validate and sanitize user input.
// Here, we want the value
$head = $head;
// Update value in database
yourls_update_option( 'head_option', $head );
}
$foot = $_POST['foot_option'];
if( $foot ) {
// Validate head_option. ALWAYS validate and sanitize user input.
// Here, we want the value
$foot = $foot;
// Update value in database
yourls_update_option( 'foot_option', $foot );
}
}