PluginProbe
Add Custom Codes – Insert Header, Footer, Custom PHP Snippets, CSS, Javascript / 4.71
Add Custom Codes – Insert Header, Footer, Custom PHP Snippets, CSS, Javascript v4.71
trunk 1.0 2.0 3.0 3.5 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.71 4.80 5.0
add-custom-codes / add-custom-codes.php

add-custom-codes.php in Add Custom Codes – Insert Header, Footer, Custom PHP Snippets, CSS, Javascript 4.71, at add-custom-codes.php

543 lines 17.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Add Custom Codes - Insert Header, Footer, Custom Code Snippets
4 * Description: Light-weight plugin to add Custom CSS, Javascript, Google Analytics, Search console verification tags and other custom code snippets to your Wordpress website. Go to <em>Appearance -> Add Custom Codes</em> after installing the plugin.
5 * Version: 4.71
6 * Author: Saifudheen Mak
7 * Author URI: https://maktalseo.com
8 * License: GPL2
9 * Text Domain: add-custom-codes
10 */
11
12 // If this file was called directly, abort.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 add_action( 'init', 'accodes_load_textdomain' );
18 function accodes_load_textdomain() {
19 load_plugin_textdomain( 'add-custom-codes', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
20 }
21
22 /*----------------
23 plugin links 'Plugins' page
24 ------------------*/
25 add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'accodes_action_links' );
26
27 function accodes_action_links ( $actions ) {
28 $mylinks = array(
29 '<a href="' . admin_url( 'themes.php?page=add-custom-codes' ) . '">Settings</a>',
30 );
31 $actions = array_merge( $actions, $mylinks );
32 return $actions;
33 }
34
35 add_filter( 'plugin_row_meta', 'accodes_row_meta', 10, 2 );
36
37 function accodes_row_meta( $links, $file ) {
38 if ( plugin_basename( __FILE__ ) == $file ) {
39 $row_meta = array(
40 'wrt-review' => '<a href="' . esc_url( 'https://wordpress.org/support/plugin/add-custom-codes/reviews/#new-post' ) . '" target="_blank" style="">' . esc_html__( 'Rate this plugin', 'add-custom-codes' ) . '</a>',
41 'acc-buy-coffee' => '<a href="' . esc_url( 'https://maktalseo.com' ) . '" target="_blank" style="color:green;">' . esc_html__( 'Hire us!', 'add-custom-codes' ) . '</a>'
42 );
43 return array_merge( $links, $row_meta );
44 }
45 return (array) $links;
46 }
47
48 /*---------------------------------
49 styles and scripts for plugin page
50 -----------------------------------------*/
51
52 add_action('admin_enqueue_scripts', 'accodes_codemirror_scripts');
53
54 function accodes_codemirror_scripts($hook) {
55 $cm_settings['codeEditor'] = wp_enqueue_code_editor(array('type' => 'htmlmixed'));
56 wp_localize_script('jquery', 'cm_settings', $cm_settings);
57
58 wp_enqueue_style('wp-codemirror');
59
60 wp_register_style( 'accodes-css', plugins_url( 'add-custom-codes/css/style43.css' ), '', '4.193' );
61 wp_enqueue_style( 'accodes-css' );
62
63 wp_register_script( 'accodes-js', plugins_url( 'add-custom-codes/js/scripts.js' ),array(),'4.32', true);
64 wp_enqueue_script( 'accodes-js' );
65
66 }
67
68 /*------------------------------
69 add menu link for plugin settings page
70 ------------------------*/
71 add_action('admin_menu', 'accodes_show_menu');
72
73 function accodes_show_menu() {
74 add_theme_page('Add Custom Codes', 'Add Custom Codes', 'administrator', 'add-custom-codes', 'accodes_settings_page');
75 }
76
77 function accodes_settings_page() {
78 if (!current_user_can('administrator')) {
79 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'add-custom-codes'));
80 }
81
82 include('global-form.php');
83 }
84
85 /*---------------------------
86 define items in settings page
87 -------------------------------*/
88
89 function accodes_sanitize_raw_input($input) {
90 // Return raw input safely — meant for HTML, CSS, JS
91 //return is_string($input) ? wp_kses_post($input) : '';
92 return $input;
93 }
94
95 // Register individual settings with static callbacks
96 add_action('admin_init', 'accodes_settings');
97
98 function accodes_settings() {
99 // Custom CSS field
100 register_setting(
101 'accodes-settings-group',
102 'custom-css-codes-input',
103 [
104 'type' => 'string',
105 'sanitize_callback' => 'accodes_sanitize_raw_input',
106 'default' => ''
107 ]
108 );
109
110 // Custom footer codes field
111 register_setting(
112 'accodes-settings-group',
113 'custom-footer-codes-input',
114 [
115 'type' => 'string',
116 'sanitize_callback' => 'accodes_sanitize_raw_input',
117 'default' => ''
118 ]
119 );
120
121 // Custom header codes field
122 register_setting(
123 'accodes-settings-group',
124 'custom-header-codes-input',
125 [
126 'type' => 'string',
127 'sanitize_callback' => 'accodes_sanitize_raw_input',
128 'default' => ''
129 ]
130 );
131
132 // Boolean option for global CSS in footer
133 register_setting(
134 'accodes-settings-group',
135 'accodes_global_css_on_footer',
136 [
137 'type' => 'boolean',
138 'sanitize_callback' => 'rest_sanitize_boolean',
139 'default' => false
140 ]
141 );
142 }
143
144
145 /*----------------
146 functions
147 ---------------*/
148 function get_current_page_id()
149 {
150 global $post;
151 $current_page_id = false;
152 if ( ! isset( $post ) ) {
153 return false;
154 }
155 // if woocommerce product, get id
156 if ( class_exists( 'WooCommerce' ) && is_shop() ) {
157 $current_page_id = wc_get_page_id( 'shop' );
158 }
159 else {
160 // get page id of individual only
161 if ( is_singular() ) {
162 $current_page_id = $post->ID;
163 }
164 }
165 return $current_page_id;
166 }
167
168 function get_current_taxonomy_id() {
169 if (is_tax() || is_category() || is_tag()) {
170 $term = get_queried_object();
171 if (!empty($term) && !is_wp_error($term)) {
172 return $term->term_id;
173 }
174 }
175 return false;
176 }
177
178 /*---------------------------------
179 output Global CSS
180 ----------------*/
181 function get_global_custom_css()
182 {
183 $accodes_global_css = '';
184 $options = get_option( 'custom-css-codes-input' );
185 if($options!='')
186 {
187 $accodes_global_css = $options;
188 }
189 return $accodes_global_css;
190 }
191
192
193 add_action( 'wp_head', 'accodes_css_output_header' );
194 function accodes_css_output_header() {
195 //check if global css to be added before footer
196 $css_on_footer = (bool) get_option('accodes_global_css_on_footer', false);
197 //put css in footer not ticked
198 if(!$css_on_footer)
199 {
200 $accodes_global_css = get_global_custom_css();
201 //escape
202 echo '<!-- Global CSS by Add Custom Codes --> <style type="text/css"> '.wp_strip_all_tags($accodes_global_css).' </style> <!-- End - Global CSS by Add Custom Codes -->';
203 }
204 }
205
206 add_action( 'wp_footer', 'accodes_css_output_footer' );
207 function accodes_css_output_footer() {
208 //check if global css to be added before footer
209 $css_on_footer = (bool) get_option('accodes_global_css_on_footer', false);
210 //put css in footer ticked
211 if($css_on_footer)
212 {
213 $accodes_global_css = get_global_custom_css();
214 //escape
215 echo '<!-- Global CSS by Add Custom Codes --> <style type="text/css"> '.wp_strip_all_tags($accodes_global_css).' </style> <!-- End - Global CSS by Add Custom Codes -->';
216 }
217 }
218 /*---------------------------------
219 output Global Header Codes
220 ----------------*/
221 add_action( 'wp_head', 'accodes_header_output' );
222 function accodes_header_output() {
223
224 $hide_global_header = '';
225 $current_page_id = get_current_page_id();
226 $current_taxonomy_id = get_current_taxonomy_id();
227 $global_header_codes ='';
228 $get_single_header_codes = '';
229 $single_header_codes = '';
230 $get_taxonomy_header_codes = '';
231 $taxonomy_header_codes = '';
232 $output = '';
233
234 //check if single page
235 if ( $current_page_id ) {
236 //value is 'on' if checked
237 $hide_global_header = get_post_meta( $current_page_id, 'accodes_hide_header', true );
238 //get Single Header Codes
239 $get_single_header_codes = get_post_meta( $current_page_id , '_accodes_header_metabox', true );
240 if($get_single_header_codes !='')
241 {
242 $single_header_codes = PHP_EOL.'<!-- Single header Scripts by Add Custom Codes -->'. PHP_EOL;
243 $single_header_codes .= $get_single_header_codes;
244 $single_header_codes .= PHP_EOL.'<!-- End of Single header Scripts by Add Custom Codes -->'. PHP_EOL;
245 }
246 }
247
248 //check if taxonomy page
249 if ( $current_taxonomy_id ) {
250 //value is 'on' if checked
251 $hide_global_header = get_term_meta( $current_taxonomy_id, 'accodes_hide_header', true );
252 //get Single Header Codes
253 $get_taxonomy_header_codes = get_term_meta( $current_taxonomy_id , '_accodes_header_metabox', true );
254 if($get_taxonomy_header_codes !='')
255 {
256 $taxonomy_header_codes = PHP_EOL.'<!-- Taxonomy header Scripts by Add Custom Codes -->'. PHP_EOL;
257 $taxonomy_header_codes .= $get_taxonomy_header_codes;
258 $taxonomy_header_codes .= PHP_EOL.'<!-- End of Taxonomy header Scripts by Add Custom Codes -->'. PHP_EOL;
259 }
260 }
261
262 //get global header - if not set to hide
263 if($hide_global_header != 'on')
264 {
265 //get Global Header Codes
266 $get_global_header_codes = get_option( 'custom-header-codes-input' );
267 if($get_global_header_codes!='')
268 {
269 $global_header_codes = ' <!-- Global Header Scripts by Add Custom Codes --> ';
270 $global_header_codes .= $get_global_header_codes;
271 $global_header_codes .= ' <!-- End - Global Header Scripts by Add Custom Codes --> ';
272 }
273 }
274 $output .= $global_header_codes;
275 $output .= $single_header_codes;
276 $output .= $taxonomy_header_codes;
277
278 if($output !='')
279 {
280 //escape
281 echo $output;
282 }
283
284 }
285
286
287
288 /*---------------------------------
289 output Global Footer Codes
290 ----------------*/
291 add_action( 'wp_footer', 'accodes_footer_output' );
292 function accodes_footer_output() {
293
294 $hide_global_footer = '';
295 $current_page_id = get_current_page_id();
296 $current_taxonomy_id = get_current_taxonomy_id();
297 $global_footer_codes ='';
298 $single_footer_codes = '';
299 $taxonomy_footer_codes = '';
300 $output = '';
301
302 if ( $current_page_id ) {
303 //value is 'on' if checked
304 $hide_global_footer = get_post_meta( $current_page_id, 'accodes_hide_footer', true );
305 //get Footer Codes for Single
306 $get_single_footer_codes = get_post_meta( $current_page_id , '_accodes_footer_metabox', true );
307 if($get_single_footer_codes !='')
308 {
309 $single_footer_codes = PHP_EOL.'<!-- Single Footer Scripts by Add Custom Codes -->'. PHP_EOL;
310 $single_footer_codes .= $get_single_footer_codes;
311 $single_footer_codes .= PHP_EOL.'<!-- End - Single Footer Scripts by Add Custom Codes -->'. PHP_EOL;
312 }
313 }
314
315 //check if taxonomy page
316 if ( $current_taxonomy_id ) {
317 //value is 'on' if checked
318 $hide_global_footer = get_term_meta( $current_taxonomy_id, 'accodes_hide_footer', true );
319 //get Single Header Codes
320 $get_taxonomy_footer_codes = get_term_meta( $current_taxonomy_id , '_accodes_footer_metabox', true );
321 if($get_taxonomy_footer_codes !='')
322 {
323 $taxonomy_footer_codes = PHP_EOL.'<!-- Taxonomy footer Scripts by Add Custom Codes -->'. PHP_EOL;
324 $taxonomy_footer_codes .= $get_taxonomy_footer_codes;
325 $taxonomy_footer_codes .= PHP_EOL.'<!-- End of Taxonomy footer Scripts by Add Custom Codes -->'. PHP_EOL;
326 }
327 }
328
329 //get global footer - if not set to hide
330 if($hide_global_footer != 'on')
331 {
332 //get Global Footer codes
333 $get_global_footer_codes = get_option( 'custom-footer-codes-input' );
334 if($get_global_footer_codes!='')
335 {
336 $global_footer_codes = PHP_EOL.'<!-- Global Footer Scripts by Add Custom Codes -->'. PHP_EOL;
337 $global_footer_codes .= $get_global_footer_codes;
338 $global_footer_codes .= PHP_EOL.'<!-- End - Global Footer Scripts by Add Custom Codes -->'. PHP_EOL;
339 }
340 }
341 $output .= $global_footer_codes;
342 $output .= $single_footer_codes;
343 $output .= $taxonomy_footer_codes;
344
345 if($output !='')
346 {
347 //escape
348 echo $output;
349 }
350
351 }
352
353
354
355 /*----------------
356 * Individual pages
357 * -----------------------*/
358
359 /**-----------------------------
360 * Create the meta boxes for Single post, page, product any other custom post type
361 ------------------------------------*/
362 function _accodes_create_metabox_single() {
363
364 $post_types = get_post_types( '', 'names' );
365 $post_types = array_merge( $post_types, array( 'post', 'page' ) );
366
367 foreach ( $post_types as $post_type ) {
368 add_meta_box(
369 '_accodes_metabox',
370 'Add Custom Codes by Mak',
371 '_accodes_render_metabox',
372 $post_type,
373 'normal',
374 'default'
375 );
376 }
377
378 }
379 add_action( 'add_meta_boxes', '_accodes_create_metabox_single' );
380
381
382
383
384 /*-------------------------------
385 * Display Meta Boxes for Single
386 * ----------------------------*/
387 function _accodes_render_metabox() {
388 // Variables
389 global $post; // Get the current post data
390 $header_script = get_post_meta( $post->ID, '_accodes_header_metabox', true ); // Get the saved values
391 $footer_script = get_post_meta( $post->ID, '_accodes_footer_metabox', true ); // Get the saved values
392
393 $hide_header = get_post_meta( $post->ID, 'accodes_hide_header', true );
394 $hide_footer = get_post_meta( $post->ID, 'accodes_hide_footer', true );
395
396 include('single-meta.php');
397
398 }
399
400
401 /*-------------------------
402 * update data on post save - Single
403 ---------------------------*/
404 function _accodes_save_metabox_single( $post_id, $post ) {
405
406
407 // Verify nonce for security
408 if ( !isset($_POST['accodes_meta_nonce']) || !wp_verify_nonce($_POST['accodes_meta_nonce'], 'accodes_save_meta') ) {
409 return $post_id;
410 }
411
412 // Verify user has permission to edit post
413 if ( !current_user_can( 'edit_post', $post->ID )) {
414 return $post->ID;
415 }
416
417 // Check if we're in the WordPress REST API request (used by Gutenberg)
418 if (defined('REST_REQUEST') && REST_REQUEST) {
419 return $post_id;
420 }
421
422 // Check if it's an autosave or a revision. If so, return early.
423 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
424 return $post_id;
425 }
426
427
428 $allowed_html = [
429 'script' => ['type' => [], 'src' => [], 'async' => [], 'defer' => []],
430 'style' => ['type' => []],
431 'meta' => ['name' => [], 'content' => [], 'charset' => []],
432 'link' => ['rel' => [], 'href' => [], 'type' => []],
433 'div' => ['class' => [], 'id' => [], 'style' => []],
434 'span' => ['class' => [], 'style' => []],
435 'p' => ['class' => [], 'style' => []],
436 'a' => ['href' => [], 'title' => [], 'target' => []],
437 'img' => ['src' => [], 'alt' => [], 'width' => [], 'height' => []],
438 'iframe' => ['src' => [], 'width' => [], 'height' => [], 'frameborder' => [], 'allowfullscreen' => []],
439 ];
440
441
442 $accodes_header_metabox = isset($_POST['_accodes_header_metabox'])
443 ? wp_kses(wp_unslash($_POST['_accodes_header_metabox']), $allowed_html)
444 : '';
445
446 $accodes_footer_metabox = isset($_POST['_accodes_footer_metabox'])
447 ? wp_kses(wp_unslash($_POST['_accodes_footer_metabox']), $allowed_html)
448 : '';
449
450 // Get values of checkbox
451 $hide_header = isset( $_POST['accodes_hide_header'] ) ? sanitize_text_field(wp_unslash($_POST['accodes_hide_header']) ): '';
452 $hide_footer = isset( $_POST['accodes_hide_footer'] ) ? sanitize_text_field(wp_unslash($_POST['accodes_hide_footer']) ): '';
453
454 //Update values of meta boxes
455 update_post_meta( $post->ID, '_accodes_header_metabox', $accodes_header_metabox );
456 update_post_meta( $post->ID, '_accodes_footer_metabox', $accodes_footer_metabox );
457
458 //Update values of check boxes
459 update_post_meta( $post->ID, 'accodes_hide_header', $hide_header );
460 update_post_meta( $post->ID, 'accodes_hide_footer', $hide_footer );
461
462 }
463 add_action( 'save_post', '_accodes_save_metabox_single', 1, 2 );
464
465
466 /*
467 * single meta end
468 * -----------------------------------------------*/
469
470 function get_current_term_id() {
471 if (isset($_GET['tag_ID'])) {
472 return intval($_GET['tag_ID']);
473 }
474 return 0;
475 }
476
477 // Register meta box
478 function accodes_render_taxonomy_meta_box() {
479 $taxonomies = get_taxonomies();
480 foreach ($taxonomies as $taxonomy) {
481 add_action("{$taxonomy}_edit_form", function($tag) use ($taxonomy) {
482 $term_id = get_current_term_id();
483 $header_script = get_term_meta( $term_id, '_accodes_header_metabox', true );
484 $footer_script = get_term_meta( $term_id, '_accodes_footer_metabox', true );
485 $hide_header = get_term_meta( $term_id, 'accodes_hide_header', true );
486 $hide_footer = get_term_meta( $term_id, 'accodes_hide_footer', true );
487 include('taxonomy-meta.php');
488 });
489 }
490
491 }
492 add_action('admin_init', 'accodes_render_taxonomy_meta_box');
493
494 // Save meta box data of taxonomies
495 function accodes_save_taxonomy_meta_data($term_id) {
496
497 // Verify nonce for security
498 if (!isset($_POST['accodes_tax_meta_nonce']) || !wp_verify_nonce($_POST['accodes_tax_meta_nonce'], 'accodes_save_tax_meta')) {
499 return $term_id;
500 }
501
502 $allowed_html = [
503 'script' => ['type' => [], 'src' => [], 'async' => [], 'defer' => []],
504 'style' => ['type' => []],
505 'meta' => ['name' => [], 'content' => [], 'charset' => []],
506 'link' => ['rel' => [], 'href' => [], 'type' => []],
507 'div' => ['class' => [], 'id' => [], 'style' => []],
508 'span' => ['class' => [], 'style' => []],
509 'p' => ['class' => [], 'style' => []],
510 'a' => ['href' => [], 'title' => [], 'target' => []],
511 'img' => ['src' => [], 'alt' => [], 'width' => [], 'height' => []],
512 'iframe' => ['src' => [], 'width' => [], 'height' => [], 'frameborder' => [], 'allowfullscreen' => []],
513 ];
514
515
516 //get value of meta boxes
517 $accodes_header_metabox = isset($_POST['_accodes_header_metabox'])
518 ? wp_kses(wp_unslash($_POST['_accodes_header_metabox']), $allowed_html)
519 : '';
520
521 $accodes_footer_metabox = isset($_POST['_accodes_footer_metabox'])
522 ? wp_kses(wp_unslash($_POST['_accodes_footer_metabox']), $allowed_html)
523 : '';
524
525 // Get values of checkbox
526 $hide_header = isset( $_POST['accodes_hide_header'] ) ? sanitize_text_field(wp_unslash($_POST['accodes_hide_header'])) : '';
527 $hide_footer = isset( $_POST['accodes_hide_footer'] ) ? sanitize_text_field(wp_unslash($_POST['accodes_hide_footer']) ): '';
528
529 //Update values of meta boxes
530 update_term_meta( $term_id, '_accodes_header_metabox', $accodes_header_metabox );
531 update_term_meta( $term_id, '_accodes_footer_metabox', $accodes_footer_metabox );
532
533 //Update values of check boxes
534 update_term_meta( $term_id, 'accodes_hide_header', $hide_header );
535 update_term_meta( $term_id, 'accodes_hide_footer', $hide_footer );
536 }
537 add_action('edited_term', 'accodes_save_taxonomy_meta_data');
538 add_action('create_term', 'accodes_save_taxonomy_meta_data');
539
540
541
542 ?>
543