Version Description
- Add: custom head, body and footer code for Homepage in Blog Posts mode.
- Fix: Code Editor broken in WordPress 5.5 release.
- Fix: Invalid requests while saving settings https://github.com/urosevic/head-footer-code/issues/1
- Improve: DRY for front-end conditions.
- Improve: translators tips for complex strings.
- Improve: all strings available to localize.
Download this release
Release Info
Developer | urkekg |
Plugin | Head & Footer Code |
Version | 1.2 |
Comparing to | |
See all releases |
Code changes from version 1.1.1 to 1.2
- head-footer-code.php +9 -9
- inc/class-auhfc-meta-box.php +27 -14
- inc/front.php +114 -103
- inc/helpers.php +110 -18
- inc/settings.php +243 -83
- inc/update.php +65 -0
- readme.txt +20 -7
- templates/settings.php +12 -13
- uninstall.php +7 -1
head-footer-code.php
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
<?php
|
2 |
/**
|
3 |
-
* @link https://urosevic.net
|
4 |
* @since 1.0.0
|
5 |
* @package Head_Footer_Code
|
6 |
*
|
@@ -8,13 +8,12 @@
|
|
8 |
* Plugin Name: Head & Footer Code
|
9 |
* Plugin URI: https://urosevic.net/wordpress/plugins/head-footer-code/
|
10 |
* Description: Easy add site-wide and/or article specific custom code to head and/or footer sections (before the </head> or </body> or opening <body>) by hooking to <code>wp_head</code>, <code>wp_footer</code> and <code>wp_body_open</code>.
|
11 |
-
* Version: 1.
|
12 |
* Author: Aleksandar Urosevic
|
13 |
-
* Author URI: https://urosevic.net
|
14 |
-
* License:
|
15 |
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt
|
16 |
* Text Domain: head-footer-code
|
17 |
-
* Domain Path: /languages
|
18 |
*/
|
19 |
|
20 |
// If this file is called directly, abort.
|
@@ -22,10 +21,11 @@ if ( ! defined( 'WPINC' ) ) {
|
|
22 |
die;
|
23 |
}
|
24 |
|
25 |
-
define( 'WPAU_HEAD_FOOTER_CODE_VER', '1.
|
26 |
-
define( 'WPAU_HEAD_FOOTER_CODE_DB_VER', '
|
27 |
-
define( 'WPAU_HEAD_FOOTER_CODE_FILE',
|
28 |
-
define( '
|
|
|
29 |
|
30 |
// Load files.
|
31 |
require_once 'inc/helpers.php';
|
1 |
<?php
|
2 |
/**
|
3 |
+
* @link https://urosevic.net/
|
4 |
* @since 1.0.0
|
5 |
* @package Head_Footer_Code
|
6 |
*
|
8 |
* Plugin Name: Head & Footer Code
|
9 |
* Plugin URI: https://urosevic.net/wordpress/plugins/head-footer-code/
|
10 |
* Description: Easy add site-wide and/or article specific custom code to head and/or footer sections (before the </head> or </body> or opening <body>) by hooking to <code>wp_head</code>, <code>wp_footer</code> and <code>wp_body_open</code>.
|
11 |
+
* Version: 1.2
|
12 |
* Author: Aleksandar Urosevic
|
13 |
+
* Author URI: https://urosevic.net/
|
14 |
+
* License: GPLv3
|
15 |
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt
|
16 |
* Text Domain: head-footer-code
|
|
|
17 |
*/
|
18 |
|
19 |
// If this file is called directly, abort.
|
21 |
die;
|
22 |
}
|
23 |
|
24 |
+
define( 'WPAU_HEAD_FOOTER_CODE_VER', '1.2' );
|
25 |
+
define( 'WPAU_HEAD_FOOTER_CODE_DB_VER', '5' );
|
26 |
+
define( 'WPAU_HEAD_FOOTER_CODE_FILE', __FILE__ );
|
27 |
+
define( 'WPAU_HEAD_FOOTER_CODE_DIR', dirname( WPAU_HEAD_FOOTER_CODE_FILE ) . '/');
|
28 |
+
define( 'WPAU_HEAD_FOOTER_CODE_INC', WPAU_HEAD_FOOTER_CODE_DIR . 'inc/' );
|
29 |
|
30 |
// Load files.
|
31 |
require_once 'inc/helpers.php';
|
inc/class-auhfc-meta-box.php
CHANGED
@@ -11,15 +11,15 @@ abstract class AUHfc_Meta_Box {
|
|
11 |
*/
|
12 |
public static function add() {
|
13 |
|
14 |
-
$
|
15 |
|
16 |
-
if ( empty( $
|
17 |
return;
|
18 |
}
|
19 |
-
foreach ( $
|
20 |
add_meta_box(
|
21 |
'auhfc-head-footer-code',
|
22 |
-
|
23 |
[ self::class, 'html' ],
|
24 |
$post_type,
|
25 |
'normal',
|
@@ -66,24 +66,37 @@ abstract class AUHfc_Meta_Box {
|
|
66 |
*/
|
67 |
public static function html( $post ) {
|
68 |
wp_nonce_field( '_head_footer_code_nonce', 'head_footer_code_nonce' ); ?>
|
69 |
-
<p
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
<select name="auhfc[behavior]" id="auhfc_behavior_replace">
|
72 |
-
<option value="append" <?php echo ( 'append' === auhfc_get_meta( 'behavior' ) ) ? 'selected' : '';
|
73 |
-
<option value="replace" <?php echo ( 'replace' === auhfc_get_meta( 'behavior' ) ) ? 'selected' : '';
|
74 |
</select>
|
75 |
<br /><br />
|
76 |
-
<label for="auhfc_head"><?php
|
77 |
<textarea name="auhfc[head]" id="auhfc_head" class="widefat code" rows="5"><?php echo auhfc_get_meta( 'head' ); ?></textarea>
|
78 |
-
<p class="description"
|
79 |
<br />
|
80 |
-
<label for="auhfc_body"><?php
|
81 |
<textarea name="auhfc[body]" id="auhfc_body" class="widefat code" rows="5"><?php echo auhfc_get_meta( 'body' ); ?></textarea>
|
82 |
-
<p class="description"
|
83 |
<br />
|
84 |
-
<label for="auhfc_footer"><?php
|
85 |
<textarea name="auhfc[footer]" id="auhfc_footer" class="widefat code" rows="5"><?php echo auhfc_get_meta( 'footer' ); ?></textarea>
|
86 |
-
<p class="description"
|
87 |
<?php
|
88 |
} // END public static function html()
|
89 |
|
11 |
*/
|
12 |
public static function add() {
|
13 |
|
14 |
+
$auhfc_settings = auhfc_settings();
|
15 |
|
16 |
+
if ( empty( $auhfc_settings['article']['post_types'] ) ) {
|
17 |
return;
|
18 |
}
|
19 |
+
foreach ( $auhfc_settings['article']['post_types'] as $post_type ) {
|
20 |
add_meta_box(
|
21 |
'auhfc-head-footer-code',
|
22 |
+
esc_html__( 'Head & Footer Code', 'head-footer-code' ),
|
23 |
[ self::class, 'html' ],
|
24 |
$post_type,
|
25 |
'normal',
|
66 |
*/
|
67 |
public static function html( $post ) {
|
68 |
wp_nonce_field( '_head_footer_code_nonce', 'head_footer_code_nonce' ); ?>
|
69 |
+
<p><?php
|
70 |
+
printf(
|
71 |
+
/* translators: %1$s will be replaced with preformatted HTML tag </head>
|
72 |
+
%2$s will be replaced with preformatted HTML tag <body>
|
73 |
+
%3$s will be replaced with preformatted HTML tag </body>
|
74 |
+
%4$s will be replaced with a link to Head & Footer Code Settings page */
|
75 |
+
esc_html__( 'Here you can insert article specific code for Head (before the %1$s), Body (after the %2$s) and Footer (before the %3$s) sections. They work in exactly the same way as site-wide code, which you can configure under %4$s.', 'head-footer-code'),
|
76 |
+
'<code></head></code>',
|
77 |
+
'<code><body></code>',
|
78 |
+
'<code></body></code>',
|
79 |
+
sprintf( '<a href="tools.php?page=head_footer_code">%s</a>', esc_html__( 'Tools / Head & Footer Code', 'head-footer-code' )
|
80 |
+
)
|
81 |
+
);
|
82 |
+
?></p>
|
83 |
+
<label><?php esc_html_e( 'Behavior', 'head-footer-code' ); ?></label><br />
|
84 |
<select name="auhfc[behavior]" id="auhfc_behavior_replace">
|
85 |
+
<option value="append" <?php echo ( 'append' === auhfc_get_meta( 'behavior' ) ) ? 'selected' : ''; ?>><?php esc_html_e( 'Append to the site-wide code', 'head-footer-code' ); ?></option>
|
86 |
+
<option value="replace" <?php echo ( 'replace' === auhfc_get_meta( 'behavior' ) ) ? 'selected' : ''; ?>><?php esc_html_e( 'Replace the site-wide code', 'head-footer-code' ); ?></option>
|
87 |
</select>
|
88 |
<br /><br />
|
89 |
+
<label for="auhfc_head"><?php esc_html_e( 'Head Code', 'head-footer-code' ); ?></label><br />
|
90 |
<textarea name="auhfc[head]" id="auhfc_head" class="widefat code" rows="5"><?php echo auhfc_get_meta( 'head' ); ?></textarea>
|
91 |
+
<p class="description"><?php esc_html_e( 'Example', 'head-footer-code'); ?>: <code><link rel="stylesheet" href="<?php echo get_stylesheet_directory_uri(); ?>/style.css" type="text/css" media="all"></code></p>
|
92 |
<br />
|
93 |
+
<label for="auhfc_body"><?php esc_html_e( 'Body Code', 'head-footer-code' ); ?></label><br />
|
94 |
<textarea name="auhfc[body]" id="auhfc_body" class="widefat code" rows="5"><?php echo auhfc_get_meta( 'body' ); ?></textarea>
|
95 |
+
<p class="description"><?php esc_html_e( 'Example', 'head-footer-code'); ?>: <code><script type="text/javascript" src="<?php echo get_stylesheet_directory_uri(); ?>/body-start.js" type="text/css" media="all"></script></code></p>
|
96 |
<br />
|
97 |
+
<label for="auhfc_footer"><?php esc_html_e( 'Footer Code', 'head-footer-code' ); ?></label><br />
|
98 |
<textarea name="auhfc[footer]" id="auhfc_footer" class="widefat code" rows="5"><?php echo auhfc_get_meta( 'footer' ); ?></textarea>
|
99 |
+
<p class="description"><?php esc_html_e( 'Example', 'head-footer-code'); ?>: <code><script type="text/javascript" src="<?php echo get_stylesheet_directory_uri(); ?>/script.js"></script></code></p>
|
100 |
<?php
|
101 |
} // END public static function html()
|
102 |
|
inc/front.php
CHANGED
@@ -7,28 +7,28 @@ if ( ! defined( 'WPINC' ) ) {
|
|
7 |
/**
|
8 |
* Inject site-wide code to head, body and footer with custom priorty.
|
9 |
*/
|
10 |
-
$
|
11 |
|
12 |
-
if ( empty( $
|
13 |
-
$
|
14 |
}
|
15 |
-
if ( empty( $
|
16 |
-
$
|
17 |
}
|
18 |
-
if ( empty( $
|
19 |
-
$
|
20 |
}
|
21 |
// Define actions for HEAD and FOOTER
|
22 |
-
add_action( 'wp_head', 'auhfc_wp_head', $
|
23 |
-
add_action( 'wp_body_open', 'auhfc_wp_body', $
|
24 |
-
add_action( 'wp_footer', 'auhfc_wp_footer', $
|
25 |
|
26 |
/**
|
27 |
-
* Inject site-wide and Article specific head code before </head>
|
28 |
*/
|
29 |
function auhfc_wp_head() {
|
30 |
|
31 |
-
// Get post type
|
32 |
if ( is_singular() ) {
|
33 |
global $wp_the_query;
|
34 |
$auhfc_post_type = $wp_the_query->get_queried_object()->post_type;
|
@@ -36,61 +36,64 @@ function auhfc_wp_head() {
|
|
36 |
$auhfc_post_type = 'not singular';
|
37 |
}
|
38 |
|
39 |
-
// Get variables to test
|
40 |
-
$auhfc_settings
|
|
|
|
|
|
|
41 |
|
42 |
-
// Get meta for post only if it's singular
|
43 |
-
if ( 'not singular' !== $auhfc_post_type && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) ) {
|
44 |
-
$
|
45 |
-
$
|
46 |
-
$dbg_set
|
47 |
} else {
|
48 |
-
$
|
49 |
-
$
|
50 |
-
$dbg_set
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
return;
|
56 |
}
|
57 |
|
58 |
// Prepare code output.
|
59 |
$out = '';
|
60 |
|
61 |
-
// Inject site-wide head code
|
62 |
if (
|
63 |
-
! empty( $auhfc_settings['head'] ) &&
|
64 |
-
(
|
65 |
-
'replace' !== $behavior ||
|
66 |
-
( 'replace' == $behavior && ! in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) ) ||
|
67 |
-
( 'replace' == $behavior && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) && empty( $auhfc_meta ) )
|
68 |
-
)
|
69 |
) {
|
70 |
-
$out .= auhfc_out( 's', 'h', $dbg_set, $auhfc_settings['head'] );
|
71 |
}
|
72 |
|
73 |
-
// Inject article specific
|
74 |
-
if (
|
75 |
-
$out .= auhfc_out( '
|
|
|
|
|
76 |
}
|
77 |
|
78 |
// Print prepared code.
|
79 |
echo $out;
|
80 |
-
// echo ( 'y' === $auhfc_settings['do_shortcode'] ) ? do_shortcode( $out ) : $out;
|
81 |
-
|
82 |
-
// Free some memory.
|
83 |
-
unset( $auhfc_post_type, $auhfc_settings, $auhfc_meta, $behavior, $out );
|
84 |
|
85 |
} // END function auhfc_wp_head()
|
86 |
|
87 |
-
|
88 |
/**
|
89 |
* Inject site-wide and Article specific body code right after opening <body>
|
90 |
*/
|
91 |
function auhfc_wp_body() {
|
92 |
|
93 |
-
// Get post type
|
94 |
if ( is_singular() ) {
|
95 |
global $wp_the_query;
|
96 |
$auhfc_post_type = $wp_the_query->get_queried_object()->post_type;
|
@@ -98,51 +101,55 @@ function auhfc_wp_body() {
|
|
98 |
$auhfc_post_type = 'not singular';
|
99 |
}
|
100 |
|
101 |
-
// Get variables to test
|
102 |
-
$auhfc_settings
|
|
|
|
|
|
|
103 |
|
104 |
-
// Get meta for post only if it's singular
|
105 |
-
if ( 'not singular' !== $auhfc_post_type && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) ) {
|
106 |
-
$
|
107 |
-
$
|
108 |
-
$dbg_set
|
109 |
} else {
|
110 |
-
$
|
111 |
-
$
|
112 |
-
$dbg_set
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
return;
|
118 |
}
|
119 |
|
120 |
// Prepare code output.
|
121 |
$out = '';
|
122 |
|
123 |
-
// Inject site-wide body code
|
124 |
if (
|
125 |
-
! empty( $auhfc_settings['body'] ) &&
|
126 |
-
(
|
127 |
-
'replace' !== $behavior ||
|
128 |
-
( 'replace' == $behavior && ! in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) ) ||
|
129 |
-
( 'replace' == $behavior && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) && empty( $auhfc_meta ) )
|
130 |
-
)
|
131 |
) {
|
132 |
-
$out .= auhfc_out( 's', 'b', $dbg_set, $auhfc_settings['body'] );
|
133 |
}
|
134 |
|
135 |
-
// Inject article specific
|
136 |
-
if (
|
137 |
-
$out .= auhfc_out( '
|
|
|
|
|
138 |
}
|
139 |
|
140 |
// Print prepared code.
|
141 |
echo $out;
|
142 |
-
// echo ( 'y' === $auhfc_settings['do_shortcode'] ) ? do_shortcode( $out ) : $out;
|
143 |
-
|
144 |
-
// Free some memory.
|
145 |
-
unset( $auhfc_post_type, $auhfc_settings, $auhfc_meta, $behavior, $out );
|
146 |
|
147 |
} // END function auhfc_wp_body()
|
148 |
|
@@ -151,7 +158,7 @@ function auhfc_wp_body() {
|
|
151 |
*/
|
152 |
function auhfc_wp_footer() {
|
153 |
|
154 |
-
// Get post type
|
155 |
if ( is_singular() ) {
|
156 |
global $wp_the_query;
|
157 |
$auhfc_post_type = $wp_the_query->get_queried_object()->post_type;
|
@@ -159,50 +166,54 @@ function auhfc_wp_footer() {
|
|
159 |
$auhfc_post_type = 'not singular';
|
160 |
}
|
161 |
|
162 |
-
// Get variables to test
|
163 |
-
$auhfc_settings
|
|
|
|
|
|
|
164 |
|
165 |
-
// Get meta for post only if it's singular
|
166 |
-
if ( 'not singular' !== $auhfc_post_type && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) ) {
|
167 |
-
$
|
168 |
-
$
|
169 |
-
$dbg_set
|
170 |
} else {
|
171 |
-
$
|
172 |
-
$
|
173 |
-
$dbg_set
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
return;
|
179 |
}
|
180 |
|
181 |
-
// Prepare code output
|
182 |
$out = '';
|
183 |
|
184 |
-
// Inject site-wide head code
|
185 |
if (
|
186 |
-
! empty( $auhfc_settings['footer'] ) &&
|
187 |
-
(
|
188 |
-
'replace' !== $behavior ||
|
189 |
-
( 'replace' == $behavior && ! in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) ) ||
|
190 |
-
( 'replace' == $behavior && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) && empty( $auhfc_meta ) )
|
191 |
-
)
|
192 |
) {
|
193 |
-
$out .= auhfc_out( 's', 'f', $dbg_set, $auhfc_settings['footer'] );
|
194 |
}
|
195 |
|
196 |
-
// Inject article specific
|
197 |
-
if (
|
198 |
-
$out .= auhfc_out( '
|
|
|
|
|
199 |
}
|
200 |
|
201 |
// Print prepared code.
|
202 |
-
echo ( 'y' === $auhfc_settings['do_shortcode'] ) ? do_shortcode( $out ) : $out;
|
203 |
-
|
204 |
-
// Free some memory.
|
205 |
-
unset( $auhfc_post_type, $auhfc_settings, $auhfc_meta, $behavior, $out );
|
206 |
|
207 |
} // END function auhfc_wp_footer()
|
208 |
|
7 |
/**
|
8 |
* Inject site-wide code to head, body and footer with custom priorty.
|
9 |
*/
|
10 |
+
$auhfc_settings = auhfc_settings();
|
11 |
|
12 |
+
if ( empty( $auhfc_settings['sitewide']['priority_h'] ) ) {
|
13 |
+
$auhfc_settings['sitewide']['priority_h'] = 10;
|
14 |
}
|
15 |
+
if ( empty( $auhfc_settings['sitewide']['priority_b'] ) ) {
|
16 |
+
$auhfc_settings['sitewide']['priority_b'] = 10;
|
17 |
}
|
18 |
+
if ( empty( $auhfc_settings['sitewide']['priority_f'] ) ) {
|
19 |
+
$auhfc_settings['sitewide']['priority_f'] = 10;
|
20 |
}
|
21 |
// Define actions for HEAD and FOOTER
|
22 |
+
add_action( 'wp_head', 'auhfc_wp_head', $auhfc_settings['sitewide']['priority_h'] );
|
23 |
+
add_action( 'wp_body_open', 'auhfc_wp_body', $auhfc_settings['sitewide']['priority_b'] );
|
24 |
+
add_action( 'wp_footer', 'auhfc_wp_footer', $auhfc_settings['sitewide']['priority_f'] );
|
25 |
|
26 |
/**
|
27 |
+
* Inject site-wide and Homepage or Article specific head code before </head>
|
28 |
*/
|
29 |
function auhfc_wp_head() {
|
30 |
|
31 |
+
// Get post type.
|
32 |
if ( is_singular() ) {
|
33 |
global $wp_the_query;
|
34 |
$auhfc_post_type = $wp_the_query->get_queried_object()->post_type;
|
36 |
$auhfc_post_type = 'not singular';
|
37 |
}
|
38 |
|
39 |
+
// Get variables to test.
|
40 |
+
$auhfc_settings = auhfc_settings();
|
41 |
+
$is_homepage_blog_posts = auhfc_is_homepage_blog_posts();
|
42 |
+
$homepage_behavior = false;
|
43 |
+
$homepage_code = '';
|
44 |
|
45 |
+
// Get meta for post only if it's singular.
|
46 |
+
if ( 'not singular' !== $auhfc_post_type && in_array( $auhfc_post_type, $auhfc_settings['article']['post_types'] ) ) {
|
47 |
+
$article_code = auhfc_get_meta( 'head' );
|
48 |
+
$article_behavior = auhfc_get_meta( 'behavior' );
|
49 |
+
$dbg_set = "type: {$auhfc_post_type}; bahavior: {$article_behavior}; priority: {$auhfc_settings['sitewide']['priority_h']}; do_shortcode: {$auhfc_settings['sitewide']['do_shortcode']}";
|
50 |
} else {
|
51 |
+
$article_code = '';
|
52 |
+
$article_behavior = '';
|
53 |
+
$dbg_set = $auhfc_post_type;
|
54 |
+
// Get meta for homepage.
|
55 |
+
if ( $is_homepage_blog_posts ) {
|
56 |
+
$homepage_code = $auhfc_settings['homepage']['head'];
|
57 |
+
$homepage_behavior = $auhfc_settings['homepage']['behavior'];
|
58 |
+
$dbg_set = "type: homepage; bahavior: {$homepage_behavior}; priority: {$auhfc_settings['sitewide']['priority_h']}; do_shortcode: {$auhfc_settings['sitewide']['do_shortcode']}";
|
59 |
+
}
|
60 |
+
}
|
61 |
+
|
62 |
+
// If no code to inject, simply exit.
|
63 |
+
if ( empty( $auhfc_settings['sitewide']['head'] ) && empty( $article_code ) && empty( $homepage_code ) ) {
|
64 |
return;
|
65 |
}
|
66 |
|
67 |
// Prepare code output.
|
68 |
$out = '';
|
69 |
|
70 |
+
// Inject site-wide head code.
|
71 |
if (
|
72 |
+
! empty( $auhfc_settings['sitewide']['head'] ) &&
|
73 |
+
auhfc_print_sitewide( $article_behavior, $auhfc_post_type, $auhfc_settings['article']['post_types'], $article_code, $homepage_behavior, $homepage_code )
|
|
|
|
|
|
|
|
|
74 |
) {
|
75 |
+
$out .= auhfc_out( 's', 'h', $dbg_set, $auhfc_settings['sitewide']['head'] );
|
76 |
}
|
77 |
|
78 |
+
// Inject head code for Homepage in Blog Posts omde OR article specific (for allowed post_type) head code.
|
79 |
+
if ( $is_homepage_blog_posts && ! empty( $homepage_code ) ) {
|
80 |
+
$out .= auhfc_out( 'h', 'h', $dbg_set, $homepage_code );
|
81 |
+
} else if ( ! empty( $article_code ) && in_array( $auhfc_post_type, $auhfc_settings['article']['post_types'] ) ) {
|
82 |
+
$out .= auhfc_out( 'a', 'h', $dbg_set, $article_code );
|
83 |
}
|
84 |
|
85 |
// Print prepared code.
|
86 |
echo $out;
|
87 |
+
// echo ( 'y' === $auhfc_settings['sitewide']['do_shortcode'] ) ? do_shortcode( $out ) : $out;
|
|
|
|
|
|
|
88 |
|
89 |
} // END function auhfc_wp_head()
|
90 |
|
|
|
91 |
/**
|
92 |
* Inject site-wide and Article specific body code right after opening <body>
|
93 |
*/
|
94 |
function auhfc_wp_body() {
|
95 |
|
96 |
+
// Get post type.
|
97 |
if ( is_singular() ) {
|
98 |
global $wp_the_query;
|
99 |
$auhfc_post_type = $wp_the_query->get_queried_object()->post_type;
|
101 |
$auhfc_post_type = 'not singular';
|
102 |
}
|
103 |
|
104 |
+
// Get variables to test.
|
105 |
+
$auhfc_settings = auhfc_settings();
|
106 |
+
$is_homepage_blog_posts = auhfc_is_homepage_blog_posts();
|
107 |
+
$homepage_behavior = false;
|
108 |
+
$homepage_code = '';
|
109 |
|
110 |
+
// Get meta for post only if it's singular.
|
111 |
+
if ( 'not singular' !== $auhfc_post_type && in_array( $auhfc_post_type, $auhfc_settings['article']['post_types'] ) ) {
|
112 |
+
$article_code = auhfc_get_meta( 'body' );
|
113 |
+
$article_behavior = auhfc_get_meta( 'behavior' );
|
114 |
+
$dbg_set = "type: {$auhfc_post_type}; bahavior: {$article_behavior}; priority: {$auhfc_settings['sitewide']['priority_b']}; do_shortcode: {$auhfc_settings['sitewide']['do_shortcode']}";
|
115 |
} else {
|
116 |
+
$article_code = '';
|
117 |
+
$article_behavior = '';
|
118 |
+
$dbg_set = $auhfc_post_type;
|
119 |
+
// Get meta for homepage.
|
120 |
+
if ( $is_homepage_blog_posts ) {
|
121 |
+
$homepage_code = $auhfc_settings['homepage']['body'];
|
122 |
+
$homepage_behavior = $auhfc_settings['homepage']['behavior'];
|
123 |
+
$dbg_set = "type: homepage; bahavior: {$homepage_behavior}; priority: {$auhfc_settings['sitewide']['priority_b']}; do_shortcode: {$auhfc_settings['sitewide']['do_shortcode']}";
|
124 |
+
}
|
125 |
+
}
|
126 |
+
|
127 |
+
// If no code to inject, simple exit.
|
128 |
+
if ( empty( $auhfc_settings['sitewide']['body'] ) && empty( $article_code ) && empty( $homepage_code ) ) {
|
129 |
return;
|
130 |
}
|
131 |
|
132 |
// Prepare code output.
|
133 |
$out = '';
|
134 |
|
135 |
+
// Inject site-wide body code.
|
136 |
if (
|
137 |
+
! empty( $auhfc_settings['sitewide']['body'] ) &&
|
138 |
+
auhfc_print_sitewide( $article_behavior, $auhfc_post_type, $auhfc_settings['article']['post_types'], $article_code, $homepage_behavior, $homepage_code )
|
|
|
|
|
|
|
|
|
139 |
) {
|
140 |
+
$out .= auhfc_out( 's', 'b', $dbg_set, $auhfc_settings['sitewide']['body'] );
|
141 |
}
|
142 |
|
143 |
+
// Inject head code for Homepage in Blog Posts omde OR article specific (for allowed post_type) body code.
|
144 |
+
if ( $is_homepage_blog_posts && ! empty( $homepage_code ) ) {
|
145 |
+
$out .= auhfc_out( 'h', 'b', $dbg_set, $homepage_code );
|
146 |
+
} else if ( ! empty( $article_code ) && in_array( $auhfc_post_type, $auhfc_settings['article']['post_types'] ) ) {
|
147 |
+
$out .= auhfc_out( 'a', 'b', $dbg_set, $article_code );
|
148 |
}
|
149 |
|
150 |
// Print prepared code.
|
151 |
echo $out;
|
152 |
+
// echo ( 'y' === $auhfc_settings['sitewide']['do_shortcode'] ) ? do_shortcode( $out ) : $out;
|
|
|
|
|
|
|
153 |
|
154 |
} // END function auhfc_wp_body()
|
155 |
|
158 |
*/
|
159 |
function auhfc_wp_footer() {
|
160 |
|
161 |
+
// Get post type.
|
162 |
if ( is_singular() ) {
|
163 |
global $wp_the_query;
|
164 |
$auhfc_post_type = $wp_the_query->get_queried_object()->post_type;
|
166 |
$auhfc_post_type = 'not singular';
|
167 |
}
|
168 |
|
169 |
+
// Get variables to test.
|
170 |
+
$auhfc_settings = auhfc_settings();
|
171 |
+
$is_homepage_blog_posts = auhfc_is_homepage_blog_posts();
|
172 |
+
$homepage_behavior = false;
|
173 |
+
$homepage_code = '';
|
174 |
|
175 |
+
// Get meta for post only if it's singular.
|
176 |
+
if ( 'not singular' !== $auhfc_post_type && in_array( $auhfc_post_type, $auhfc_settings['article']['post_types'] ) ) {
|
177 |
+
$article_code = auhfc_get_meta( 'footer' );
|
178 |
+
$article_behavior = auhfc_get_meta( 'behavior' );
|
179 |
+
$dbg_set = "type: {$auhfc_post_type}; bahavior: {$article_behavior}; priority: {$auhfc_settings['sitewide']['priority_f']}; do_shortcode: {$auhfc_settings['sitewide']['do_shortcode']}";
|
180 |
} else {
|
181 |
+
$article_code = '';
|
182 |
+
$article_behavior = '';
|
183 |
+
$dbg_set = $auhfc_post_type;
|
184 |
+
// Get meta for homepage.
|
185 |
+
if ( $is_homepage_blog_posts ) {
|
186 |
+
$homepage_code = $auhfc_settings['homepage']['footer'];
|
187 |
+
$homepage_behavior = $auhfc_settings['homepage']['behavior'];
|
188 |
+
$dbg_set = "type: homepage; bahavior: {$homepage_behavior}; priority: {$auhfc_settings['sitewide']['priority_f']}; do_shortcode: {$auhfc_settings['sitewide']['do_shortcode']}";
|
189 |
+
}
|
190 |
+
}
|
191 |
+
|
192 |
+
// If no code to inject, simple exit.
|
193 |
+
if ( empty( $auhfc_settings['sitewide']['footer'] ) && empty( $article_code ) && empty( $homepage_code ) ) {
|
194 |
return;
|
195 |
}
|
196 |
|
197 |
+
// Prepare code output.
|
198 |
$out = '';
|
199 |
|
200 |
+
// Inject site-wide head code.
|
201 |
if (
|
202 |
+
! empty( $auhfc_settings['sitewide']['footer'] ) &&
|
203 |
+
auhfc_print_sitewide( $article_behavior, $auhfc_post_type, $auhfc_settings['article']['post_types'], $article_code, $homepage_behavior, $homepage_code )
|
|
|
|
|
|
|
|
|
204 |
) {
|
205 |
+
$out .= auhfc_out( 's', 'f', $dbg_set, $auhfc_settings['sitewide']['footer'] );
|
206 |
}
|
207 |
|
208 |
+
// Inject head code for Homepage in Blog Posts omde OR article specific (for allowed post_type) footer code.
|
209 |
+
if ( $is_homepage_blog_posts && ! empty( $homepage_code ) ) {
|
210 |
+
$out .= auhfc_out( 'h', 'f', $dbg_set, $homepage_code );
|
211 |
+
} else if ( ! empty( $article_code ) && in_array( $auhfc_post_type, $auhfc_settings['article']['post_types'] ) ) {
|
212 |
+
$out .= auhfc_out( 'a', 'f', $dbg_set, $article_code );
|
213 |
}
|
214 |
|
215 |
// Print prepared code.
|
216 |
+
echo ( 'y' === $auhfc_settings['sitewide']['do_shortcode'] ) ? do_shortcode( $out ) : $out;
|
|
|
|
|
|
|
217 |
|
218 |
} // END function auhfc_wp_footer()
|
219 |
|
inc/helpers.php
CHANGED
@@ -30,9 +30,18 @@ function auhfc_activate() {
|
|
30 |
}
|
31 |
$version = 'PHP' == $flag ? $php_req : $wp_req;
|
32 |
deactivate_plugins( WPAU_HEAD_FOOTER_CODE_FILE );
|
|
|
33 |
wp_die(
|
34 |
-
'<p>
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
[
|
37 |
'response' => 200,
|
38 |
'back_link' => true,
|
@@ -66,28 +75,46 @@ function auhfc_codemirror_enqueue_scripts( $hook ) {
|
|
66 |
return;
|
67 |
}
|
68 |
$cm_settings['codeEditor'] = wp_enqueue_code_editor( [ 'type' => 'text/html' ] );
|
69 |
-
wp_localize_script( '
|
70 |
-
wp_enqueue_script( 'wp-codemirror' );
|
71 |
wp_enqueue_style( 'wp-codemirror' );
|
|
|
72 |
} // END function auhfc_codemirror_enqueue_scripts( $hook )
|
73 |
|
74 |
/**
|
75 |
* Provide global defaults
|
76 |
* @return array Arary of defined global values
|
77 |
*/
|
78 |
-
function
|
|
|
79 |
$defaults = [
|
80 |
-
'
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
];
|
87 |
-
$
|
88 |
-
$
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
/**
|
93 |
* Get values of metabox fields
|
@@ -135,7 +162,7 @@ function auhfc_get_meta( $field_name = '' ) {
|
|
135 |
|
136 |
/**
|
137 |
* Return debugging string if WP_DEBUG constant is true.
|
138 |
-
* @param string $scope Scope of output (s - SITE WIDE, a - ARTICLE SPECIFIC)
|
139 |
* @param string $location Location of output (h - HEAD, b - BODY, f - FOOTER)
|
140 |
* @param string $message Output message
|
141 |
* @param string $code Code for output
|
@@ -148,7 +175,19 @@ function auhfc_out( $scope = null, $location = null, $message = null, $code = nu
|
|
148 |
if ( null == $scope || null == $location || null == $message ) {
|
149 |
return;
|
150 |
}
|
151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
switch ( $location ) {
|
153 |
case 'h':
|
154 |
$location = 'HEAD';
|
@@ -173,6 +212,59 @@ function auhfc_out( $scope = null, $location = null, $message = null, $code = nu
|
|
173 |
);
|
174 |
} // END function auhfc_out( $scope = null, $location = null, $message = null, $code = null )
|
175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
function auhfc_body_note() {
|
177 |
-
return '<p class="notice"><strong>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
}
|
30 |
}
|
31 |
$version = 'PHP' == $flag ? $php_req : $wp_req;
|
32 |
deactivate_plugins( WPAU_HEAD_FOOTER_CODE_FILE );
|
33 |
+
|
34 |
wp_die(
|
35 |
+
'<p>' . sprintf(
|
36 |
+
/* translators: %1$s will be replaced with plugin name Head & Footer Code
|
37 |
+
%2$s will be replaced with related software name (PHP or WordPress)
|
38 |
+
%3$s will be replaved with minimal version of related software required to plugin work properly */
|
39 |
+
esc_html__( 'The %1$s plugin requires %2$s version %3$s or greater.', 'head-footer-code' ),
|
40 |
+
sprintf( '<strong>%s</strong>', esc_html__( 'Head & Footer Code', 'head-footer-code' ) ),
|
41 |
+
$flag,
|
42 |
+
$version
|
43 |
+
) . '</p>',
|
44 |
+
esc_html__( 'Plugin Activation Error', 'head-footer-code' ),
|
45 |
[
|
46 |
'response' => 200,
|
47 |
'back_link' => true,
|
75 |
return;
|
76 |
}
|
77 |
$cm_settings['codeEditor'] = wp_enqueue_code_editor( [ 'type' => 'text/html' ] );
|
78 |
+
wp_localize_script( 'code-editor', 'cm_settings', $cm_settings );
|
|
|
79 |
wp_enqueue_style( 'wp-codemirror' );
|
80 |
+
wp_enqueue_script( 'wp-codemirror' );
|
81 |
} // END function auhfc_codemirror_enqueue_scripts( $hook )
|
82 |
|
83 |
/**
|
84 |
* Provide global defaults
|
85 |
* @return array Arary of defined global values
|
86 |
*/
|
87 |
+
function auhfc_settings() {
|
88 |
+
|
89 |
$defaults = [
|
90 |
+
'sitewide' => [
|
91 |
+
'head' => '',
|
92 |
+
'body' => '',
|
93 |
+
'footer' => '',
|
94 |
+
'priority_h' => 10,
|
95 |
+
'priority_b' => 10,
|
96 |
+
'priority_f' => 10,
|
97 |
+
'do_shortcode' => 'n',
|
98 |
+
],
|
99 |
+
'homepage' => [
|
100 |
+
'head' => '',
|
101 |
+
'body' => '',
|
102 |
+
'footer' => '',
|
103 |
+
'behavior' => 'append',
|
104 |
+
],
|
105 |
+
'article' => [
|
106 |
+
'post_types' => [],
|
107 |
+
],
|
108 |
];
|
109 |
+
$auhfc_settings_sitewide = get_option( 'auhfc_settings_sitewide', $defaults['sitewide'] );
|
110 |
+
$defaults['sitewide'] = wp_parse_args( $auhfc_settings_sitewide, $defaults['sitewide'] );
|
111 |
+
$auhfc_settings_homepage = get_option( 'auhfc_settings_homepage', $defaults['homepage'] );
|
112 |
+
$defaults['homepage'] = wp_parse_args( $auhfc_settings_homepage, $defaults['homepage'] );
|
113 |
+
$auhfc_settings_article = get_option( 'auhfc_settings_article', $defaults['article'] );
|
114 |
+
$defaults['article'] = wp_parse_args( $auhfc_settings_article, $defaults['article'] );
|
115 |
+
|
116 |
+
return $defaults;
|
117 |
+
} // END function auhfc_settings()
|
118 |
|
119 |
/**
|
120 |
* Get values of metabox fields
|
162 |
|
163 |
/**
|
164 |
* Return debugging string if WP_DEBUG constant is true.
|
165 |
+
* @param string $scope Scope of output (s - SITE WIDE, a - ARTICLE SPECIFIC, h - HOMEPAGE)
|
166 |
* @param string $location Location of output (h - HEAD, b - BODY, f - FOOTER)
|
167 |
* @param string $message Output message
|
168 |
* @param string $code Code for output
|
175 |
if ( null == $scope || null == $location || null == $message ) {
|
176 |
return;
|
177 |
}
|
178 |
+
switch ( $scope ) {
|
179 |
+
case 'h':
|
180 |
+
$scope = 'Homepage';
|
181 |
+
break;
|
182 |
+
case 's':
|
183 |
+
$scope = 'Site-wide';
|
184 |
+
break;
|
185 |
+
case 'a':
|
186 |
+
$scope = 'Article specific';
|
187 |
+
break;
|
188 |
+
default:
|
189 |
+
$scope = 'Unknown';
|
190 |
+
}
|
191 |
switch ( $location ) {
|
192 |
case 'h':
|
193 |
$location = 'HEAD';
|
212 |
);
|
213 |
} // END function auhfc_out( $scope = null, $location = null, $message = null, $code = null )
|
214 |
|
215 |
+
function auhfc_is_homepage_blog_posts() {
|
216 |
+
if ( is_home() && 'posts' == get_option( 'show_on_front', false ) ) {
|
217 |
+
return true;
|
218 |
+
}
|
219 |
+
return false;
|
220 |
+
} // END function auhfc_is_homepage_blog_posts()
|
221 |
+
|
222 |
function auhfc_body_note() {
|
223 |
+
return '<p class="notice"><strong>' . esc_html__( 'Please note!', 'head-footer-code' ) . '</strong> ' . sprintf(
|
224 |
+
/* translators: %1$s will be replaced with translated 'unseen elements'
|
225 |
+
%2$s will be replaced with preformatted HTML tag <script>
|
226 |
+
%3$s will be replaced with translated sentence 'could break layouts or lead to unexpected situations'
|
227 |
+
%4$s will be replaced with a link to wp_body_open page on WordPress.org */
|
228 |
+
esc_html__( 'Usage of this hook should be reserved for output of %1$s like %2$s tags or additional metadata. It should not be used to add arbitrary HTML content to a page that %3$s. Make sure that your active theme support %4$s hook.', 'head-footer-code' ),
|
229 |
+
'<em>' . esc_html__( 'unseen elements', 'head-footer-code' ) . '</em>',
|
230 |
+
'<em>' . esc_html__( 'could break layouts or lead to unexpected situations', 'head-footer-code' ) . '</em>',
|
231 |
+
auhfc_html2code( '<script>' ),
|
232 |
+
'<a href="https://developer.wordpress.org/reference/hooks/wp_body_open/" target="_hook">wp_body_open</a>'
|
233 |
+
) . '</p>';
|
234 |
+
}
|
235 |
+
|
236 |
+
function auhfc_html2code( $text ) {
|
237 |
+
return '<code>' . htmlspecialchars( $text ) . '</code>';
|
238 |
+
} // END function auhfc_html2code( $text )
|
239 |
+
|
240 |
+
/**
|
241 |
+
* Determine should we print site-wide code
|
242 |
+
* or it should be replaced with homepage/article code.
|
243 |
+
* @param string $article_behavior Behavior for article specific code (replace/append)
|
244 |
+
* @param string $article_post_type Post type of current article
|
245 |
+
* @param array $article_post_types Array of post types where article specific code is enabled
|
246 |
+
* @param string $article_code Article specific custom code
|
247 |
+
* @param string $homepage_behavior Behavior for homepage code (replace/append)
|
248 |
+
* @param string $homepage_code Homepage specific custom code
|
249 |
+
* @return boolean Boolean that determine should site-wide code be printed (true) or not (false)
|
250 |
+
*/
|
251 |
+
function auhfc_print_sitewide(
|
252 |
+
$article_behavior = 'append',
|
253 |
+
$article_post_type = null,
|
254 |
+
$article_post_types = [],
|
255 |
+
$article_code = null,
|
256 |
+
$homepage_behavior = 'append',
|
257 |
+
$homepage_code = null
|
258 |
+
) {
|
259 |
+
$is_homepage_blog_posts = auhfc_is_homepage_blog_posts();
|
260 |
+
if (
|
261 |
+
( ! $is_homepage_blog_posts && 'replace' !== $article_behavior ) ||
|
262 |
+
( ! $is_homepage_blog_posts && 'replace' == $article_behavior && ! in_array( $article_post_type, $article_post_types ) ) ||
|
263 |
+
( ! $is_homepage_blog_posts && 'replace' == $article_behavior && in_array( $article_post_type, $article_post_types ) && empty( $article_code ) ) ||
|
264 |
+
( $is_homepage_blog_posts && 'replace' !== $homepage_behavior ) ||
|
265 |
+
( $is_homepage_blog_posts && 'replace' == $homepage_behavior && empty( $homepage_code ) )
|
266 |
+
) {
|
267 |
+
return true;
|
268 |
+
}
|
269 |
+
return false;
|
270 |
}
|
inc/settings.php
CHANGED
@@ -4,12 +4,12 @@ if ( ! defined( 'WPINC' ) ) {
|
|
4 |
die;
|
5 |
}
|
6 |
|
7 |
-
// Initiate settings section and fields.
|
8 |
-
add_action( 'admin_init', 'auhfc_settings_init' );
|
9 |
-
|
10 |
// Create menu item for settings page.
|
11 |
add_action( 'admin_menu', 'auhfc_add_admin_menu' );
|
12 |
|
|
|
|
|
|
|
13 |
// Add Settings page link to plugin actions cell.
|
14 |
add_filter( 'plugin_action_links_head-footer-code/head-footer-code.php', 'auhfc_plugin_settings_link' );
|
15 |
|
@@ -35,6 +35,8 @@ function auhfc_admin_enqueue_scripts( $hook ) {
|
|
35 |
|
36 |
function auhfc_add_admin_menu() {
|
37 |
|
|
|
|
|
38 |
add_submenu_page(
|
39 |
'tools.php',
|
40 |
'Head & Footer Code',
|
@@ -55,16 +57,10 @@ function auhfc_settings_init() {
|
|
55 |
/**
|
56 |
* Get settings from options table
|
57 |
*/
|
58 |
-
$auhfc_settings =
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
* This is part of the Settings API, which lets you automatically generate
|
63 |
-
* wp-admin settings pages by registering your settings and using a few
|
64 |
-
* callbacks to control the output.
|
65 |
-
*/
|
66 |
-
register_setting( 'head_footer_code_sitewide_settings', 'auhfc_settings' );
|
67 |
-
register_setting( 'head_footer_code_article_settings', 'auhfc_settings' );
|
68 |
|
69 |
/**
|
70 |
* Settings Sections are the groups of settings you see on WordPress settings pages
|
@@ -73,9 +69,10 @@ function auhfc_settings_init() {
|
|
73 |
* simpler to maintain and creates less new pages for users to learn.
|
74 |
* You just tell them to change your setting on the relevant existing page.
|
75 |
*/
|
|
|
76 |
add_settings_section(
|
77 |
-
'
|
78 |
-
|
79 |
'auhfc_sitewide_settings_section_description',
|
80 |
'head_footer_code'
|
81 |
);
|
@@ -86,17 +83,22 @@ function auhfc_settings_init() {
|
|
86 |
* wp-admin settings pages by registering your settings and using a few
|
87 |
* callbacks to control the output.
|
88 |
*/
|
|
|
89 |
add_settings_field(
|
90 |
'auhfc_head_code',
|
91 |
__( 'HEAD Code', 'head-footer-code' ),
|
92 |
'auhfc_textarea_field_render',
|
93 |
'head_footer_code',
|
94 |
-
'
|
95 |
[
|
96 |
-
'field' => '
|
97 |
-
'value' => $auhfc_settings['head'],
|
98 |
-
'description' =>
|
99 |
-
|
|
|
|
|
|
|
|
|
100 |
'rows' => 7,
|
101 |
]
|
102 |
);
|
@@ -106,11 +108,17 @@ function auhfc_settings_init() {
|
|
106 |
__( 'HEAD Priority', 'head-footer-code' ),
|
107 |
'auhfc_number_field_render',
|
108 |
'head_footer_code',
|
109 |
-
'
|
110 |
[
|
111 |
-
'field' => '
|
112 |
-
'value' => $auhfc_settings['priority_h'],
|
113 |
-
'description' =>
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
'class' => 'num',
|
115 |
'min' => 1,
|
116 |
'max' => 1000,
|
@@ -118,22 +126,21 @@ function auhfc_settings_init() {
|
|
118 |
]
|
119 |
);
|
120 |
|
121 |
-
$wp52note = version_compare( get_bloginfo( 'version' ),'5.2', '<') ? ' Require WordPress 5.2+' : '';
|
122 |
-
|
123 |
add_settings_field(
|
124 |
'auhfc_body_code',
|
125 |
__( 'BODY Code', 'head-footer-code' ),
|
126 |
'auhfc_textarea_field_render',
|
127 |
'head_footer_code',
|
128 |
-
'
|
129 |
[
|
130 |
-
'field' => '
|
131 |
-
'value' => $auhfc_settings['body'],
|
132 |
-
'description' =>
|
133 |
-
|
134 |
-
'
|
135 |
-
|
136 |
-
|
|
|
137 |
'rows' => 7,
|
138 |
]
|
139 |
);
|
@@ -143,13 +150,20 @@ function auhfc_settings_init() {
|
|
143 |
__( 'BODY Priority', 'head-footer-code' ),
|
144 |
'auhfc_number_field_render',
|
145 |
'head_footer_code',
|
146 |
-
'
|
147 |
[
|
148 |
-
'field' => '
|
149 |
-
'value' => $auhfc_settings['priority_b'],
|
150 |
-
'description' =>
|
151 |
-
|
152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
),
|
154 |
'class' => 'num',
|
155 |
'min' => 1,
|
@@ -163,15 +177,16 @@ function auhfc_settings_init() {
|
|
163 |
__( 'FOOTER Code', 'head-footer-code' ),
|
164 |
'auhfc_textarea_field_render',
|
165 |
'head_footer_code',
|
166 |
-
'
|
167 |
[
|
168 |
-
'field' => '
|
169 |
-
'value' => $auhfc_settings['footer'],
|
170 |
-
'description' =>
|
171 |
-
|
172 |
-
'head-footer-code'
|
|
|
173 |
),
|
174 |
-
'field_class' => 'widefat code',
|
175 |
'rows' => 7,
|
176 |
]
|
177 |
);
|
@@ -181,11 +196,17 @@ function auhfc_settings_init() {
|
|
181 |
__( 'FOOTER Priority', 'head-footer-code' ),
|
182 |
'auhfc_number_field_render',
|
183 |
'head_footer_code',
|
184 |
-
'
|
185 |
[
|
186 |
-
'field' => '
|
187 |
-
'value' => $auhfc_settings['priority_f'],
|
188 |
-
'description' =>
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
'class' => 'num',
|
190 |
'min' => 1,
|
191 |
'max' => 1000,
|
@@ -198,18 +219,141 @@ function auhfc_settings_init() {
|
|
198 |
__( 'Process Shortcodes', 'head-footer-code' ),
|
199 |
'auhfc_select_field_render',
|
200 |
'head_footer_code',
|
201 |
-
'
|
202 |
[
|
203 |
-
'field' => '
|
204 |
'items' => [
|
205 |
-
'y' => __( 'Enable' ),
|
206 |
-
'n' => __( 'Disable' ),
|
207 |
],
|
208 |
-
'value' => $auhfc_settings['do_shortcode'],
|
209 |
-
'description' => esc_html__( 'If you wish to process shortcodes in FOOTER section, enable this option.', 'head-footer-code' ),
|
210 |
-
'class' => '
|
211 |
]
|
212 |
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
/**
|
214 |
* Settings Sections are the groups of settings you see on WordPress settings pages
|
215 |
* with a shared heading. In your plugin you can add new sections to existing
|
@@ -217,9 +361,10 @@ function auhfc_settings_init() {
|
|
217 |
* simpler to maintain and creates less new pages for users to learn.
|
218 |
* You just tell them to change your setting on the relevant existing page.
|
219 |
*/
|
|
|
220 |
add_settings_section(
|
221 |
-
'
|
222 |
-
|
223 |
'auhfc_article_settings_section_description',
|
224 |
'head_footer_code'
|
225 |
);
|
@@ -233,23 +378,32 @@ function auhfc_settings_init() {
|
|
233 |
}
|
234 |
$clean_post_types[ $public_post_type ] = "{$public_post_object->label} ({$public_post_type})";
|
235 |
}
|
236 |
-
// unset( $clean_post_types['attachment'] );
|
237 |
|
|
|
238 |
add_settings_field(
|
239 |
'auhfc_post_types',
|
240 |
__( 'Post Types', 'head-footer-code' ),
|
241 |
'auhfc_checkbox_group_field_render',
|
242 |
'head_footer_code',
|
243 |
-
'
|
244 |
[
|
245 |
-
'field' => '
|
246 |
'items' => $clean_post_types,
|
247 |
-
'value' => $auhfc_settings['post_types'],
|
248 |
'description' => esc_html__( 'Select which post types will have Article specific section. Please note, even if you have Head/Footer Code set per article and then you disable that post type, article specific code will not be printed but only site-wide code.', 'head-footer-code' ),
|
249 |
'class' => 'checkbox',
|
250 |
]
|
251 |
);
|
252 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
} // END function auhfc_settings_init()
|
254 |
|
255 |
/**
|
@@ -270,7 +424,6 @@ function auhfc_textarea_field_render( $args ) {
|
|
270 |
);
|
271 |
} // END function auhfc_textarea_field_render( $args )
|
272 |
|
273 |
-
|
274 |
/**
|
275 |
* This function provides number input for settings fields
|
276 |
*/
|
@@ -285,7 +438,7 @@ function auhfc_number_field_render( $args ) {
|
|
285 |
$args['step'], // step
|
286 |
$args['description'] // description
|
287 |
);
|
288 |
-
} // END function auhfc_number_field_render($args)
|
289 |
|
290 |
/**
|
291 |
* This function provides checkbox group for settings fields
|
@@ -320,12 +473,10 @@ function auhfc_checkbox_group_field_render( $args ) {
|
|
320 |
}
|
321 |
|
322 |
$out .= '</fieldset>';
|
323 |
-
$out .= sprintf( '<p class="description">%s</p>', $description );
|
324 |
|
325 |
echo $out;
|
326 |
|
327 |
-
} //
|
328 |
-
|
329 |
|
330 |
/**
|
331 |
* This function provides select for settings fields
|
@@ -365,27 +516,36 @@ function auhfc_select_field_render( $args ) {
|
|
365 |
]
|
366 |
)
|
367 |
);
|
368 |
-
} // END
|
369 |
|
370 |
function auhfc_sitewide_settings_section_description() {
|
371 |
-
|
372 |
-
<p>
|
373 |
-
|
|
|
374 |
} // END function auhfc_sitewide_settings_section_description()
|
375 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
376 |
function auhfc_article_settings_section_description() {
|
377 |
-
|
378 |
-
<p
|
379 |
-
|
|
|
380 |
} // END function auhfc_article_settings_section_description()
|
381 |
|
382 |
function auhfc_options_page() {
|
383 |
if ( ! current_user_can( 'manage_options' ) ) {
|
384 |
-
wp_die(
|
385 |
}
|
386 |
// Render the settings template.
|
387 |
-
include(
|
388 |
-
}
|
389 |
|
390 |
/**
|
391 |
* Generate Settings link on Plugins page listing
|
@@ -393,10 +553,10 @@ function auhfc_options_page() {
|
|
393 |
* @return array Updated array of plugin row links with link to Settings page
|
394 |
*/
|
395 |
function auhfc_plugin_settings_link( $links ) {
|
396 |
-
$settings_link = '<a href="tools.php?page=head_footer_code"
|
397 |
array_unshift( $links, $settings_link );
|
398 |
return $links;
|
399 |
-
} // END
|
400 |
|
401 |
/**
|
402 |
* Add link to official plugin pages
|
@@ -411,11 +571,11 @@ function auhfc_add_plugin_meta_links( $links, $file ) {
|
|
411 |
[
|
412 |
sprintf(
|
413 |
'<a href="https://wordpress.org/support/plugin/head-footer-code" target="_blank">%s</a>',
|
414 |
-
__( 'Support' )
|
415 |
),
|
416 |
sprintf(
|
417 |
'<a href="https://urosevic.net/wordpress/donate/?donate_for=head-footer-code" target="_blank">%s</a>',
|
418 |
-
__( 'Donate' )
|
419 |
),
|
420 |
]
|
421 |
);
|
4 |
die;
|
5 |
}
|
6 |
|
|
|
|
|
|
|
7 |
// Create menu item for settings page.
|
8 |
add_action( 'admin_menu', 'auhfc_add_admin_menu' );
|
9 |
|
10 |
+
// Initiate settings section and fields.
|
11 |
+
add_action( 'admin_init', 'auhfc_settings_init' );
|
12 |
+
|
13 |
// Add Settings page link to plugin actions cell.
|
14 |
add_filter( 'plugin_action_links_head-footer-code/head-footer-code.php', 'auhfc_plugin_settings_link' );
|
15 |
|
35 |
|
36 |
function auhfc_add_admin_menu() {
|
37 |
|
38 |
+
// add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function )
|
39 |
+
// add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function, $position )
|
40 |
add_submenu_page(
|
41 |
'tools.php',
|
42 |
'Head & Footer Code',
|
57 |
/**
|
58 |
* Get settings from options table
|
59 |
*/
|
60 |
+
$auhfc_settings = auhfc_settings();
|
61 |
+
$auhfc_homepage_blog_posts = 'posts' == get_option( 'show_on_front', false ) ? true : false;
|
62 |
+
$wp52note = version_compare( get_bloginfo( 'version' ), '5.2', '<' ) ? ' ' . esc_html__( 'Requires WordPress 5.2 or later.', 'head-footer-code' ) : '';
|
63 |
+
$body_note = auhfc_body_note();
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
/**
|
66 |
* Settings Sections are the groups of settings you see on WordPress settings pages
|
69 |
* simpler to maintain and creates less new pages for users to learn.
|
70 |
* You just tell them to change your setting on the relevant existing page.
|
71 |
*/
|
72 |
+
// add_settings_section( $id, $title, $callback, $page )
|
73 |
add_settings_section(
|
74 |
+
'head_footer_code_settings_sitewide',
|
75 |
+
esc_html__( 'Site-wide head, body and footer code', 'head-footer-code' ),
|
76 |
'auhfc_sitewide_settings_section_description',
|
77 |
'head_footer_code'
|
78 |
);
|
83 |
* wp-admin settings pages by registering your settings and using a few
|
84 |
* callbacks to control the output.
|
85 |
*/
|
86 |
+
// add_settings_field( $id, $title, $callback, $page, $section, $args )
|
87 |
add_settings_field(
|
88 |
'auhfc_head_code',
|
89 |
__( 'HEAD Code', 'head-footer-code' ),
|
90 |
'auhfc_textarea_field_render',
|
91 |
'head_footer_code',
|
92 |
+
'head_footer_code_settings_sitewide',
|
93 |
[
|
94 |
+
'field' => 'auhfc_settings_sitewide[head]',
|
95 |
+
'value' => $auhfc_settings['sitewide']['head'],
|
96 |
+
'description' => sprintf(
|
97 |
+
/* translators: %s will be replaced with preformatted HTML tag </head> */
|
98 |
+
esc_html__( 'Code to enqueue in HEAD section (before the %s).', 'head-footer-code' ),
|
99 |
+
auhfc_html2code( '</head>' )
|
100 |
+
),
|
101 |
+
'field_class' => 'widefat code codeEditor',
|
102 |
'rows' => 7,
|
103 |
]
|
104 |
);
|
108 |
__( 'HEAD Priority', 'head-footer-code' ),
|
109 |
'auhfc_number_field_render',
|
110 |
'head_footer_code',
|
111 |
+
'head_footer_code_settings_sitewide',
|
112 |
[
|
113 |
+
'field' => 'auhfc_settings_sitewide[priority_h]',
|
114 |
+
'value' => $auhfc_settings['sitewide']['priority_h'],
|
115 |
+
'description' => sprintf(
|
116 |
+
/* translators: %1$d will be replaced with default HEAD priority
|
117 |
+
%2$s will be replaced with preformatted HTML tag </head> */
|
118 |
+
esc_html__( 'Priority for enqueued HEAD code. Default is %1$d. Larger number inject code closer to %2$s.', 'head-footer-code' ),
|
119 |
+
10,
|
120 |
+
auhfc_html2code( '</head>' )
|
121 |
+
),
|
122 |
'class' => 'num',
|
123 |
'min' => 1,
|
124 |
'max' => 1000,
|
126 |
]
|
127 |
);
|
128 |
|
|
|
|
|
129 |
add_settings_field(
|
130 |
'auhfc_body_code',
|
131 |
__( 'BODY Code', 'head-footer-code' ),
|
132 |
'auhfc_textarea_field_render',
|
133 |
'head_footer_code',
|
134 |
+
'head_footer_code_settings_sitewide',
|
135 |
[
|
136 |
+
'field' => 'auhfc_settings_sitewide[body]',
|
137 |
+
'value' => $auhfc_settings['sitewide']['body'],
|
138 |
+
'description' => $body_note . sprintf(
|
139 |
+
/* translators: %s will be replaced with preformatted HTML tag <body> */
|
140 |
+
esc_html__( 'Code to enqueue in BODY section (after the %s).', 'head-footer-code' ),
|
141 |
+
auhfc_html2code( '<body>' )
|
142 |
+
) . $wp52note,
|
143 |
+
'field_class' => 'widefat code codeEditor',
|
144 |
'rows' => 7,
|
145 |
]
|
146 |
);
|
150 |
__( 'BODY Priority', 'head-footer-code' ),
|
151 |
'auhfc_number_field_render',
|
152 |
'head_footer_code',
|
153 |
+
'head_footer_code_settings_sitewide',
|
154 |
[
|
155 |
+
'field' => 'auhfc_settings_sitewide[priority_b]',
|
156 |
+
'value' => $auhfc_settings['sitewide']['priority_b'],
|
157 |
+
'description' => sprintf(
|
158 |
+
/* translators: %1$d will be replaced with default BODY priority
|
159 |
+
%2$s will be replaced with preformatted HTML tag <body> */
|
160 |
+
esc_html__(
|
161 |
+
'Priority for enqueued BODY code. Default is %1$d. Smaller number inject code closer to %2$s.',
|
162 |
+
'head-footer-code'
|
163 |
+
) . $wp52note,
|
164 |
+
10,
|
165 |
+
auhfc_html2code( '<body>' ),
|
166 |
+
$wp52note
|
167 |
),
|
168 |
'class' => 'num',
|
169 |
'min' => 1,
|
177 |
__( 'FOOTER Code', 'head-footer-code' ),
|
178 |
'auhfc_textarea_field_render',
|
179 |
'head_footer_code',
|
180 |
+
'head_footer_code_settings_sitewide',
|
181 |
[
|
182 |
+
'field' => 'auhfc_settings_sitewide[footer]',
|
183 |
+
'value' => $auhfc_settings['sitewide']['footer'],
|
184 |
+
'description' => sprintf(
|
185 |
+
/* translators: %s will be replaced with preformatted HTML tag </body> */
|
186 |
+
esc_html__( 'Code to enqueue in footer section (before the %s).', 'head-footer-code' ),
|
187 |
+
auhfc_html2code( '</body>' )
|
188 |
),
|
189 |
+
'field_class' => 'widefat code codeEditor',
|
190 |
'rows' => 7,
|
191 |
]
|
192 |
);
|
196 |
__( 'FOOTER Priority', 'head-footer-code' ),
|
197 |
'auhfc_number_field_render',
|
198 |
'head_footer_code',
|
199 |
+
'head_footer_code_settings_sitewide',
|
200 |
[
|
201 |
+
'field' => 'auhfc_settings_sitewide[priority_f]',
|
202 |
+
'value' => $auhfc_settings['sitewide']['priority_f'],
|
203 |
+
'description' => sprintf(
|
204 |
+
/* translators: %1$d will be replaced with default FOOTER priority
|
205 |
+
%2$s will be replaced with preformatted HTML tag </body> */
|
206 |
+
esc_html__( 'Priority for enqueued FOOTER code. Default is %1$d. Larger number inject code closer to %2$s.', 'head-footer-code' ),
|
207 |
+
10,
|
208 |
+
auhfc_html2code( '</body>' )
|
209 |
+
),
|
210 |
'class' => 'num',
|
211 |
'min' => 1,
|
212 |
'max' => 1000,
|
219 |
__( 'Process Shortcodes', 'head-footer-code' ),
|
220 |
'auhfc_select_field_render',
|
221 |
'head_footer_code',
|
222 |
+
'head_footer_code_settings_sitewide',
|
223 |
[
|
224 |
+
'field' => 'auhfc_settings_sitewide[do_shortcode]',
|
225 |
'items' => [
|
226 |
+
'y' => __( 'Enable', 'head-footer-code' ),
|
227 |
+
'n' => __( 'Disable', 'head-footer-code' ),
|
228 |
],
|
229 |
+
'value' => $auhfc_settings['sitewide']['do_shortcode'],
|
230 |
+
'description' => esc_html__( 'If you wish to process shortcodes in FOOTER section, enable this option. Please note, shortcodes in HEAD and BODY sections are not processed!', 'head-footer-code' ),
|
231 |
+
'class' => 'regular-text',
|
232 |
]
|
233 |
);
|
234 |
+
|
235 |
+
/**
|
236 |
+
* Register a setting and its sanitization callback.
|
237 |
+
* This is part of the Settings API, which lets you automatically generate
|
238 |
+
* wp-admin settings pages by registering your settings and using a few
|
239 |
+
* callbacks to control the output.
|
240 |
+
*/
|
241 |
+
// register_setting( $option_group, $option_name, $sanitize_callback )
|
242 |
+
register_setting( 'head_footer_code_settings', 'auhfc_settings_sitewide' );
|
243 |
+
|
244 |
+
/**
|
245 |
+
* Add section for Homepage if show_on_front is set to Blog Posts
|
246 |
+
*/
|
247 |
+
if ( $auhfc_homepage_blog_posts ) {
|
248 |
+
|
249 |
+
/**
|
250 |
+
* Settings Sections are the groups of settings you see on WordPress settings pages
|
251 |
+
* with a shared heading. In your plugin you can add new sections to existing
|
252 |
+
* settings pages rather than creating a whole new page. This makes your plugin
|
253 |
+
* simpler to maintain and creates less new pages for users to learn.
|
254 |
+
* You just tell them to change your setting on the relevant existing page.
|
255 |
+
*/
|
256 |
+
// add_settings_section( $id, $title, $callback, $page )
|
257 |
+
add_settings_section(
|
258 |
+
'head_footer_code_settings_homepage',
|
259 |
+
esc_html__( 'Head, body and footer code on Homepage in Blog Posts mode', 'head-footer-code' ),
|
260 |
+
'auhfc_homepage_settings_section_description',
|
261 |
+
'head_footer_code'
|
262 |
+
);
|
263 |
+
|
264 |
+
/**
|
265 |
+
* Register a settings field to a settings page and section.
|
266 |
+
* This is part of the Settings API, which lets you automatically generate
|
267 |
+
* wp-admin settings pages by registering your settings and using a few
|
268 |
+
* callbacks to control the output.
|
269 |
+
*/
|
270 |
+
// add_settings_field( $id, $title, $callback, $page, $section, $args )
|
271 |
+
add_settings_field(
|
272 |
+
'auhfc_homepage_head_code',
|
273 |
+
__( 'Homepage HEAD Code', 'head-footer-code' ),
|
274 |
+
'auhfc_textarea_field_render',
|
275 |
+
'head_footer_code',
|
276 |
+
'head_footer_code_settings_homepage',
|
277 |
+
[
|
278 |
+
'field' => 'auhfc_settings_homepage[head]',
|
279 |
+
'value' => $auhfc_settings['homepage']['head'],
|
280 |
+
'description' => sprintf(
|
281 |
+
/* translators: %s will be replaced with preformatted HTML tag </head> */
|
282 |
+
esc_html__( 'Code to enqueue in HEAD section (before the %s) on Homepage.', 'head-footer-code' ),
|
283 |
+
auhfc_html2code( '</head>' )
|
284 |
+
),
|
285 |
+
'field_class' => 'widefat code codeEditor',
|
286 |
+
'rows' => 5,
|
287 |
+
]
|
288 |
+
);
|
289 |
+
|
290 |
+
add_settings_field(
|
291 |
+
'auhfc_homepage_body_code',
|
292 |
+
__( 'Homepage BODY Code', 'head-footer-code' ),
|
293 |
+
'auhfc_textarea_field_render',
|
294 |
+
'head_footer_code',
|
295 |
+
'head_footer_code_settings_homepage',
|
296 |
+
[
|
297 |
+
'field' => 'auhfc_settings_homepage[body]',
|
298 |
+
'value' => $auhfc_settings['homepage']['body'],
|
299 |
+
'description' => $body_note . sprintf(
|
300 |
+
/* translators: %s will be replaced with preformatted HTML tag <body> */
|
301 |
+
esc_html__( 'Code to enqueue in BODY section (after the %s) on Homepage.', 'head-footer-code' ),
|
302 |
+
auhfc_html2code( '<body>' )
|
303 |
+
) . $wp52note,
|
304 |
+
'field_class' => 'widefat code codeEditor',
|
305 |
+
'rows' => 5,
|
306 |
+
]
|
307 |
+
);
|
308 |
+
|
309 |
+
add_settings_field(
|
310 |
+
'auhfc_homepage_footer_code',
|
311 |
+
__( 'Homepage FOOTER Code', 'head-footer-code' ),
|
312 |
+
'auhfc_textarea_field_render',
|
313 |
+
'head_footer_code',
|
314 |
+
'head_footer_code_settings_homepage',
|
315 |
+
[
|
316 |
+
'field' => 'auhfc_settings_homepage[footer]',
|
317 |
+
'value' => $auhfc_settings['homepage']['footer'],
|
318 |
+
'description' => sprintf(
|
319 |
+
/* translators: %s will be replaced with preformatted HTML tag </body> */
|
320 |
+
esc_html__( 'Code to enqueue in footer section (before the %s) on Homepage.', 'head-footer-code' ),
|
321 |
+
auhfc_html2code( '</body>' )
|
322 |
+
),
|
323 |
+
'field_class' => 'widefat code codeEditor',
|
324 |
+
'rows' => 5,
|
325 |
+
]
|
326 |
+
);
|
327 |
+
|
328 |
+
add_settings_field(
|
329 |
+
'auhfc_homepage_behavior',
|
330 |
+
__( 'Behavior', 'head-footer-code' ),
|
331 |
+
'auhfc_select_field_render',
|
332 |
+
'head_footer_code',
|
333 |
+
'head_footer_code_settings_homepage',
|
334 |
+
[
|
335 |
+
'field' => 'auhfc_settings_homepage[behavior]',
|
336 |
+
'items' => [
|
337 |
+
'append' => esc_html__( 'Append to the site-wide code', 'head-footer-code' ),
|
338 |
+
'replace' => esc_html__( 'Replace the site-wide code', 'head-footer-code' ),
|
339 |
+
],
|
340 |
+
'value' => $auhfc_settings['homepage']['behavior'],
|
341 |
+
'description' => esc_html__( 'Chose how the Homepage specific code will be enqueued in relation to site-wide code.', 'head-footer-code' ),
|
342 |
+
'class' => 'regular-text',
|
343 |
+
]
|
344 |
+
);
|
345 |
+
|
346 |
+
/**
|
347 |
+
* Register a setting and its sanitization callback.
|
348 |
+
* This is part of the Settings API, which lets you automatically generate
|
349 |
+
* wp-admin settings pages by registering your settings and using a few
|
350 |
+
* callbacks to control the output.
|
351 |
+
*/
|
352 |
+
// register_setting( $option_group, $option_name, $sanitize_callback )
|
353 |
+
register_setting( 'head_footer_code_settings', 'auhfc_settings_homepage' );
|
354 |
+
|
355 |
+
} // END if ( $auhfc_homepage_blog_posts )
|
356 |
+
|
357 |
/**
|
358 |
* Settings Sections are the groups of settings you see on WordPress settings pages
|
359 |
* with a shared heading. In your plugin you can add new sections to existing
|
361 |
* simpler to maintain and creates less new pages for users to learn.
|
362 |
* You just tell them to change your setting on the relevant existing page.
|
363 |
*/
|
364 |
+
// add_settings_section( $id, $title, $callback, $page )
|
365 |
add_settings_section(
|
366 |
+
'head_footer_code_settings_article',
|
367 |
+
esc_html__( 'Article specific settings', 'head-footer-code' ),
|
368 |
'auhfc_article_settings_section_description',
|
369 |
'head_footer_code'
|
370 |
);
|
378 |
}
|
379 |
$clean_post_types[ $public_post_type ] = "{$public_post_object->label} ({$public_post_type})";
|
380 |
}
|
|
|
381 |
|
382 |
+
// add_settings_field( $id, $title, $callback, $page, $section, $args )
|
383 |
add_settings_field(
|
384 |
'auhfc_post_types',
|
385 |
__( 'Post Types', 'head-footer-code' ),
|
386 |
'auhfc_checkbox_group_field_render',
|
387 |
'head_footer_code',
|
388 |
+
'head_footer_code_settings_article',
|
389 |
[
|
390 |
+
'field' => 'auhfc_settings_article[post_types]',
|
391 |
'items' => $clean_post_types,
|
392 |
+
'value' => $auhfc_settings['article']['post_types'],
|
393 |
'description' => esc_html__( 'Select which post types will have Article specific section. Please note, even if you have Head/Footer Code set per article and then you disable that post type, article specific code will not be printed but only site-wide code.', 'head-footer-code' ),
|
394 |
'class' => 'checkbox',
|
395 |
]
|
396 |
);
|
397 |
|
398 |
+
/**
|
399 |
+
* Register a setting and its sanitization callback.
|
400 |
+
* This is part of the Settings API, which lets you automatically generate
|
401 |
+
* wp-admin settings pages by registering your settings and using a few
|
402 |
+
* callbacks to control the output.
|
403 |
+
*/
|
404 |
+
// register_setting( $option_group, $option_name, $sanitize_callback )
|
405 |
+
register_setting( 'head_footer_code_settings', 'auhfc_settings_article' );
|
406 |
+
|
407 |
} // END function auhfc_settings_init()
|
408 |
|
409 |
/**
|
424 |
);
|
425 |
} // END function auhfc_textarea_field_render( $args )
|
426 |
|
|
|
427 |
/**
|
428 |
* This function provides number input for settings fields
|
429 |
*/
|
438 |
$args['step'], // step
|
439 |
$args['description'] // description
|
440 |
);
|
441 |
+
} // END function auhfc_number_field_render( $args )
|
442 |
|
443 |
/**
|
444 |
* This function provides checkbox group for settings fields
|
473 |
}
|
474 |
|
475 |
$out .= '</fieldset>';
|
|
|
476 |
|
477 |
echo $out;
|
478 |
|
479 |
+
} // END function auhfc_checkbox_group_field_render( $args )
|
|
|
480 |
|
481 |
/**
|
482 |
* This function provides select for settings fields
|
516 |
]
|
517 |
)
|
518 |
);
|
519 |
+
} // END function auhfc_select_field_render( $args )
|
520 |
|
521 |
function auhfc_sitewide_settings_section_description() {
|
522 |
+
printf(
|
523 |
+
'<p>%s</p>',
|
524 |
+
esc_html__( 'Define site-wide code and behavior. You can Add custom content like JavaScript, CSS, HTML meta and link tags, Google Analytics, site verification, etc.', 'head-footer-code' )
|
525 |
+
);
|
526 |
} // END function auhfc_sitewide_settings_section_description()
|
527 |
|
528 |
+
function auhfc_homepage_settings_section_description() {
|
529 |
+
printf(
|
530 |
+
'<p>%s</p>',
|
531 |
+
esc_html__( 'Define code and behavior for the Homepage in Blog Posts mode.', 'head-footer-code' )
|
532 |
+
);
|
533 |
+
} // END function auhfc_homepage_settings_section_description()
|
534 |
+
|
535 |
function auhfc_article_settings_section_description() {
|
536 |
+
printf(
|
537 |
+
'<p>%s</p>',
|
538 |
+
esc_html__( 'Define what post types will support article specific features.', 'head-footer-code' )
|
539 |
+
);
|
540 |
} // END function auhfc_article_settings_section_description()
|
541 |
|
542 |
function auhfc_options_page() {
|
543 |
if ( ! current_user_can( 'manage_options' ) ) {
|
544 |
+
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'head-footer-code' ) );
|
545 |
}
|
546 |
// Render the settings template.
|
547 |
+
include( WPAU_HEAD_FOOTER_CODE_DIR . 'templates/settings.php' );
|
548 |
+
} // END function auhfc_options_page()
|
549 |
|
550 |
/**
|
551 |
* Generate Settings link on Plugins page listing
|
553 |
* @return array Updated array of plugin row links with link to Settings page
|
554 |
*/
|
555 |
function auhfc_plugin_settings_link( $links ) {
|
556 |
+
$settings_link = sprintf( '<a href="tools.php?page=head_footer_code">%s</a>', __( 'Settings', 'head-footer-code' ) );
|
557 |
array_unshift( $links, $settings_link );
|
558 |
return $links;
|
559 |
+
} // END function auhfc_plugin_settings_link( $links )
|
560 |
|
561 |
/**
|
562 |
* Add link to official plugin pages
|
571 |
[
|
572 |
sprintf(
|
573 |
'<a href="https://wordpress.org/support/plugin/head-footer-code" target="_blank">%s</a>',
|
574 |
+
__( 'Support', 'head-footer-code' )
|
575 |
),
|
576 |
sprintf(
|
577 |
'<a href="https://urosevic.net/wordpress/donate/?donate_for=head-footer-code" target="_blank">%s</a>',
|
578 |
+
__( 'Donate', 'head-footer-code' )
|
579 |
),
|
580 |
]
|
581 |
);
|
inc/update.php
CHANGED
@@ -108,3 +108,68 @@ function auhfc_update_3() {
|
|
108 |
update_option( 'auhfc_settings', $defaults );
|
109 |
|
110 |
} // END function auhfc_update_3()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
update_option( 'auhfc_settings', $defaults );
|
109 |
|
110 |
} // END function auhfc_update_3()
|
111 |
+
|
112 |
+
/**
|
113 |
+
* Add homepage blog posts code defaults
|
114 |
+
*/
|
115 |
+
function auhfc_update_4() {
|
116 |
+
|
117 |
+
// Get options from DB.
|
118 |
+
$defaults = get_option( 'auhfc_settings' );
|
119 |
+
|
120 |
+
// Add empty homepage_head field to options.
|
121 |
+
if ( ! isset( $defaults['homepage_head'] ) ) {
|
122 |
+
$defaults['homepage_head'] = '';
|
123 |
+
}
|
124 |
+
// Add empty homepage_body field to options.
|
125 |
+
if ( ! isset( $defaults['homepage_body'] ) ) {
|
126 |
+
$defaults['homepage_body'] = '';
|
127 |
+
}
|
128 |
+
// Add empty homepage_footer field to options.
|
129 |
+
if ( ! isset( $defaults['homepage_footer'] ) ) {
|
130 |
+
$defaults['homepage_footer'] = '';
|
131 |
+
}
|
132 |
+
// Add empty homepage_behavior field to options.
|
133 |
+
if ( ! isset( $defaults['homepage_behavior'] ) ) {
|
134 |
+
$defaults['homepage_behavior'] = 'append';
|
135 |
+
}
|
136 |
+
|
137 |
+
// Save settings to DB.
|
138 |
+
update_option( 'auhfc_settings', $defaults );
|
139 |
+
|
140 |
+
} // END function auhfc_update_4()
|
141 |
+
|
142 |
+
|
143 |
+
/**
|
144 |
+
* Split settings to 3 options (v1.2)
|
145 |
+
*/
|
146 |
+
function auhfc_update_5() {
|
147 |
+
|
148 |
+
// Get options from DB.
|
149 |
+
$defaults = get_option( 'auhfc_settings' );
|
150 |
+
|
151 |
+
$sitewide = [
|
152 |
+
'head' => $defaults['head'],
|
153 |
+
'body' => $defaults['body'],
|
154 |
+
'footer' => $defaults['footer'],
|
155 |
+
'do_shortcode' => $defaults['do_shortcode'],
|
156 |
+
];
|
157 |
+
update_option( 'auhfc_settings_sitewide', $sitewide );
|
158 |
+
|
159 |
+
$homepage = [
|
160 |
+
'head' => $defaults['homepage_head'],
|
161 |
+
'body' => $defaults['homepage_body'],
|
162 |
+
'footer' => $defaults['homepage_footer'],
|
163 |
+
'behavior' => $defaults['homepage_behavior'],
|
164 |
+
];
|
165 |
+
update_option( 'auhfc_settings_homepage', $homepage );
|
166 |
+
|
167 |
+
$article = [
|
168 |
+
'post_types' => $defaults['post_types'],
|
169 |
+
];
|
170 |
+
update_option( 'auhfc_settings_article', $article );
|
171 |
+
|
172 |
+
// Now delete old single option.
|
173 |
+
delete_option( 'auhfc_settings' );
|
174 |
+
|
175 |
+
} // END function auhfc_update_5()
|
readme.txt
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
=== Head & Footer Code ===
|
2 |
Contributors: urkekg
|
3 |
Donate link: https://urosevic.net/wordpress/donate/?donate_for=head-footer-code
|
4 |
-
Tags: wp_head, wp_footer, wp_body_open, head footer code, custom
|
5 |
Requires at least: 4.9
|
6 |
-
Tested up to: 5.
|
7 |
-
Stable tag: 1.
|
8 |
Requires PHP: 5.6
|
9 |
-
License: GPLv3
|
10 |
License URI: http://www.gnu.org/licenses/gpl-3.0.html
|
11 |
|
12 |
Easy add site-wide and/or article specific custom code before the </head> or </body> or opening <body> by hooking to wp_head, wp_footer and wp_body_open. Multisite is supported!
|
@@ -17,8 +17,12 @@ Let we say that you have been told to add some custom code (HTML, JavaScript or
|
|
17 |
|
18 |
Simply go to Tools → Head & Footer Code in your website admin dashboard, and insert custom code to HEAD, BODY or FOOTER section (depending what you have to do).
|
19 |
|
|
|
|
|
20 |
If you have to insert some custom code specific for individual article (post, page, custom post type), then you can use Article specific metabox while you editing post/page/custom post type (check out [Screenshots](https://wordpress.org/plugins/head-footer-code/#screenshots)). There you can also set should that specific code be appended to site-wide code defined on **Tools** → **Head & Footer Code**, or should be overwritten.
|
21 |
|
|
|
|
|
22 |
https://www.youtube.com/watch?v=Gd41Dv09UC4
|
23 |
|
24 |
**Works or broken?**
|
@@ -34,6 +38,7 @@ It is very hard to continue development and support for this and my other free p
|
|
34 |
* Set site-wide custom content for head page section (before the `</head>`)
|
35 |
* Set site-wide custom content for body section (after the `<body>`) - **Requires WordPress 5.2!**
|
36 |
* Set site-wide custom content for footer page section (before the `</body>`)
|
|
|
37 |
* Set article specific custom code for head page section (before the `</head>`)
|
38 |
* Set article specific custom code for body section (after the `<body>`) - **Requires WordPress 5.2!**
|
39 |
* Set article specific custom content for footer page section (before the `</body>`)
|
@@ -43,8 +48,8 @@ It is very hard to continue development and support for this and my other free p
|
|
43 |
* Site-wide section located under **Tools** > **Head & Footer Code**
|
44 |
* If you have set WP_DEBUG constant in `wp-config.php` to `true`, you'll see site-wide and article specific entries in page source code wrapped to comments.
|
45 |
|
46 |
-
General settings, including HEAD, BODY, FOOTER global code and priority, have been saved to WordPress option `auhfc_settings`.
|
47 |
-
Each post/page/custom post type specific HEAD, BODY and FOOTER code have been saved to post meta `_auhfc`.
|
48 |
On plugin uninstall these data is also deleted from database.
|
49 |
|
50 |
== Installation ==
|
@@ -100,8 +105,16 @@ Initial release of new plugin developed by Aleksandar Urosevic.
|
|
100 |
|
101 |
== Changelog ==
|
102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
= 1.1.1 =
|
104 |
-
* Tested: on WordPress 5.4.1
|
105 |
* Add: Video tutorial on how to install, configure and use Head & Footer Code plugin
|
106 |
|
107 |
= 1.1.0 =
|
1 |
=== Head & Footer Code ===
|
2 |
Contributors: urkekg
|
3 |
Donate link: https://urosevic.net/wordpress/donate/?donate_for=head-footer-code
|
4 |
+
Tags: wp_head, wp_footer, wp_body_open, head footer code, custom script
|
5 |
Requires at least: 4.9
|
6 |
+
Tested up to: 5.5
|
7 |
+
Stable tag: 1.2
|
8 |
Requires PHP: 5.6
|
9 |
+
License: GPLv3
|
10 |
License URI: http://www.gnu.org/licenses/gpl-3.0.html
|
11 |
|
12 |
Easy add site-wide and/or article specific custom code before the </head> or </body> or opening <body> by hooking to wp_head, wp_footer and wp_body_open. Multisite is supported!
|
17 |
|
18 |
Simply go to Tools → Head & Footer Code in your website admin dashboard, and insert custom code to HEAD, BODY or FOOTER section (depending what you have to do).
|
19 |
|
20 |
+
If your WordPress uses latest posts on homepage, you can also add specific code only for homepage on Tools → Head & Footer Code (there will be section **Head, body and footer code on Homepage in Blog Posts mode**)
|
21 |
+
|
22 |
If you have to insert some custom code specific for individual article (post, page, custom post type), then you can use Article specific metabox while you editing post/page/custom post type (check out [Screenshots](https://wordpress.org/plugins/head-footer-code/#screenshots)). There you can also set should that specific code be appended to site-wide code defined on **Tools** → **Head & Footer Code**, or should be overwritten.
|
23 |
|
24 |
+
Please note that taxonomies does not have own specific code but global code is added on those pages (category, tag and custom taxonomy listing, individual category, tags and custom taxonomies).
|
25 |
+
|
26 |
https://www.youtube.com/watch?v=Gd41Dv09UC4
|
27 |
|
28 |
**Works or broken?**
|
38 |
* Set site-wide custom content for head page section (before the `</head>`)
|
39 |
* Set site-wide custom content for body section (after the `<body>`) - **Requires WordPress 5.2!**
|
40 |
* Set site-wide custom content for footer page section (before the `</body>`)
|
41 |
+
* [NEW in 1.2] Set homepage specific custom code for head, body and/or footer if Homepage mode is se to Blog Posts
|
42 |
* Set article specific custom code for head page section (before the `</head>`)
|
43 |
* Set article specific custom code for body section (after the `<body>`) - **Requires WordPress 5.2!**
|
44 |
* Set article specific custom content for footer page section (before the `</body>`)
|
48 |
* Site-wide section located under **Tools** > **Head & Footer Code**
|
49 |
* If you have set WP_DEBUG constant in `wp-config.php` to `true`, you'll see site-wide and article specific entries in page source code wrapped to comments.
|
50 |
|
51 |
+
General settings, including HEAD, BODY, FOOTER global code and priority, and also homepage code and behavior have been saved to WordPress option `auhfc_settings`.
|
52 |
+
Each post/page/custom post type specific HEAD, BODY and FOOTER code and behavior have been saved to post meta `_auhfc`.
|
53 |
On plugin uninstall these data is also deleted from database.
|
54 |
|
55 |
== Installation ==
|
105 |
|
106 |
== Changelog ==
|
107 |
|
108 |
+
= 1.2 =
|
109 |
+
* Add: custom head, body and footer code for Homepage in Blog Posts mode.
|
110 |
+
* Fix: Code Editor broken in WordPress 5.5 release.
|
111 |
+
* Fix: Invalid requests while saving settings https://github.com/urosevic/head-footer-code/issues/1
|
112 |
+
* Improve: DRY for front-end conditions.
|
113 |
+
* Improve: translators tips for complex strings.
|
114 |
+
* Improve: all strings available to localize.
|
115 |
+
|
116 |
= 1.1.1 =
|
117 |
+
* Tested: on WordPress 5.4.1, 5.5-RC2-48768 with PHP 7.4.1
|
118 |
* Add: Video tutorial on how to install, configure and use Head & Footer Code plugin
|
119 |
|
120 |
= 1.1.0 =
|
templates/settings.php
CHANGED
@@ -16,35 +16,34 @@ if ( ! defined( 'WPINC' ) ) {
|
|
16 |
|
17 |
?>
|
18 |
<div class="wrap" id="head_footer_code_settings">
|
19 |
-
<
|
20 |
-
<em
|
21 |
<div class="head_footer_code_wrapper">
|
22 |
<div class="content_cell">
|
23 |
<form method="post" action="options.php">
|
24 |
<?php
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
@submit_button();
|
29 |
?>
|
30 |
</form>
|
31 |
</div><!-- .content_cell -->
|
32 |
|
33 |
<div class="sidebar_container">
|
34 |
-
<a href="https://urosevic.net/wordpress/donate/?donate_for=head-footer-code" class="auhfc-button paypal_donate" target="_blank"
|
35 |
<br />
|
36 |
-
<a href="https://wordpress.org/plugins/head-footer-code/#faq" class="auhfc-button" target="_blank"
|
37 |
<br />
|
38 |
-
<a href="https://wordpress.org/support/plugin/head-footer-code/" class="auhfc-button" target="_blank"
|
39 |
<br />
|
40 |
-
<a href="https://wordpress.org/support/plugin/head-footer-code/reviews/#new-post" class="auhfc-button" target="_blank"
|
41 |
</div><!-- .sidebar_container -->
|
42 |
</div><!-- .head_footer_code_wrapper -->
|
43 |
</div>
|
44 |
<script type="text/javascript">
|
45 |
jQuery(document).ready(function($) {
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
});
|
50 |
</script>
|
16 |
|
17 |
?>
|
18 |
<div class="wrap" id="head_footer_code_settings">
|
19 |
+
<h2><?php esc_html_e( 'Head & Footer Code', 'head-footer-code' ); ?></h2>
|
20 |
+
<em><?php esc_html_e( 'Plugin version', 'head-footer-code' ); ?>: <?php echo WPAU_HEAD_FOOTER_CODE_VER; ?></em>
|
21 |
<div class="head_footer_code_wrapper">
|
22 |
<div class="content_cell">
|
23 |
<form method="post" action="options.php">
|
24 |
<?php
|
25 |
+
settings_fields( 'head_footer_code_settings' );
|
26 |
+
do_settings_sections( 'head_footer_code' );
|
27 |
+
submit_button();
|
|
|
28 |
?>
|
29 |
</form>
|
30 |
</div><!-- .content_cell -->
|
31 |
|
32 |
<div class="sidebar_container">
|
33 |
+
<a href="https://urosevic.net/wordpress/donate/?donate_for=head-footer-code" class="auhfc-button paypal_donate" target="_blank"><?php _e( 'Donate', 'head-footer-code' ); ?></a>
|
34 |
<br />
|
35 |
+
<a href="https://wordpress.org/plugins/head-footer-code/#faq" class="auhfc-button" target="_blank"><?php _e( 'FAQ', 'head-footer-code' ); ?></a>
|
36 |
<br />
|
37 |
+
<a href="https://wordpress.org/support/plugin/head-footer-code/" class="auhfc-button" target="_blank"><?php _e( 'Community Support', 'head-footer-code' ); ?></a>
|
38 |
<br />
|
39 |
+
<a href="https://wordpress.org/support/plugin/head-footer-code/reviews/#new-post" class="auhfc-button" target="_blank"><?php _e( 'Review this plugin', 'head-footer-code' ); ?></a>
|
40 |
</div><!-- .sidebar_container -->
|
41 |
</div><!-- .head_footer_code_wrapper -->
|
42 |
</div>
|
43 |
<script type="text/javascript">
|
44 |
jQuery(document).ready(function($) {
|
45 |
+
$('#head_footer_code_settings .codeEditor').each( function(index, value) {
|
46 |
+
wp.codeEditor.initialize(this, cm_settings);
|
47 |
+
});
|
48 |
});
|
49 |
</script>
|
uninstall.php
CHANGED
@@ -15,7 +15,13 @@ if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
|
|
15 |
exit();
|
16 |
}
|
17 |
|
18 |
-
$auhfc_options =
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
foreach ( $auhfc_options as $option_name ) {
|
20 |
// Delete option on single site
|
21 |
delete_option( $option_name );
|
15 |
exit();
|
16 |
}
|
17 |
|
18 |
+
$auhfc_options = [
|
19 |
+
'auhfc_settings',
|
20 |
+
'auhfc_settings_sitewide',
|
21 |
+
'auhfc_settings_homepage',
|
22 |
+
'auhfc_settings_article',
|
23 |
+
'auhfc_db_ver',
|
24 |
+
];
|
25 |
foreach ( $auhfc_options as $option_name ) {
|
26 |
// Delete option on single site
|
27 |
delete_option( $option_name );
|