PluginProbe
Add Custom Codes – Insert Header, Footer, Custom PHP Snippets, CSS, Javascript / 4.5
Add Custom Codes – Insert Header, Footer, Custom PHP Snippets, CSS, Javascript v4.5
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.5, at add-custom-codes.php

337 lines 10.5 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.5
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__), 'add_action_links_accodes' );
26
27 function add_action_links_accodes ( $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', 'plugin_row_meta_accodes', 10, 2 );
36
37 function plugin_row_meta_accodes( $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
44 );
45
46 return array_merge( $links, $row_meta );
47 }
48 return (array) $links;
49 }
50
51 /*---------------------------------
52 styles and scripts for plugin page
53 -----------------------------------------*/
54
55 add_action('admin_enqueue_scripts', 'codemirror_enqueue_scripts');
56
57 function codemirror_enqueue_scripts($hook) {
58 $cm_settings['codeEditor'] = wp_enqueue_code_editor(array('type' => 'htmlmixed'));
59 wp_localize_script('jquery', 'cm_settings', $cm_settings);
60
61 wp_enqueue_style('wp-codemirror');
62
63 wp_register_style( 'accodes-css', plugins_url( 'add-custom-codes/css/style41.css' ), '', '4.192' );
64 wp_enqueue_style( 'accodes-css' );
65
66 wp_register_script( 'accodes-js', plugins_url( 'add-custom-codes/js/script.js' ), '', '4.1' );
67 wp_enqueue_script( 'accodes-js' );
68
69 }
70
71 /*------------------------------
72 add menu link for plugin settings page
73 ------------------------*/
74 add_action('admin_menu', 'accodes_menu');
75
76 function accodes_menu() {
77 add_theme_page('Add Custom Codes', 'Add Custom Codes', 'administrator', 'add-custom-codes', 'accodes_settings_page');
78 }
79
80 function accodes_settings_page() {
81 include('global-form.php');
82 }
83
84 /*---------------------------
85 define items in settings page
86 -------------------------------*/
87 add_action( 'admin_init', 'accodes_settings' );
88
89 function accodes_settings() {
90 register_setting( 'accodes-settings-group', 'custom-css-codes-input' );
91 register_setting( 'accodes-settings-group', 'custom-footer-codes-input' );
92 register_setting( 'accodes-settings-group', 'custom-header-codes-input' );
93 register_setting( 'accodes-settings-group', 'accodes_global_css_on_footer' );
94 }
95
96
97 /*----------------
98 functions
99 ---------------*/
100 function get_current_page_id()
101 {
102 global $post;
103 $current_page_id = false;
104 if ( ! isset( $post ) ) {
105 return false;
106 }
107 // if woocommerce product, get id
108 if ( class_exists( 'WooCommerce' ) && is_shop() ) {
109 $current_page_id = wc_get_page_id( 'shop' );
110 }
111 else {
112 // get page id of individual only
113 if ( is_singular() ) {
114 $current_page_id = $post->ID;
115 }
116 }
117 return $current_page_id;
118 }
119
120 /*---------------------------------
121 output Global CSS
122 ----------------*/
123 function get_global_custom_css()
124 {
125 $accodes_global_css = '';
126 $options = get_option( 'custom-css-codes-input' );
127 if($options!='')
128 {
129 $accodes_global_css = '<!-- Global CSS by Add Custom Codes -->'. PHP_EOL;
130 $accodes_global_css .= '<style type="text/css">';
131 $accodes_global_css .= ''. PHP_EOL;
132 $accodes_global_css .= $options;
133 $accodes_global_css .= ''. PHP_EOL.'</style>'.PHP_EOL;
134 $accodes_global_css .= '<!-- End - Global CSS by Add Custom Codes -->';
135 }
136 return $accodes_global_css;
137 }
138
139
140 add_action( 'wp_head', 'accodes_css_output_header' );
141 function accodes_css_output_header() {
142 //check if global css to be added before footer
143 $css_on_footer = esc_attr( get_option('accodes_global_css_on_footer') );
144 //put css in footer not ticked
145 if($css_on_footer != 'on')
146 {
147 $accodes_global_css = get_global_custom_css();
148 echo $accodes_global_css;
149 }
150 }
151
152 add_action( 'wp_footer', 'accodes_css_output_footer' );
153 function accodes_css_output_footer() {
154 //check if global css to be added before footer
155 $css_on_footer = esc_attr( get_option('accodes_global_css_on_footer') );
156 //put css in footer ticked
157 if($css_on_footer == 'on')
158 {
159 $accodes_global_css = get_global_custom_css();
160 echo $accodes_global_css;
161 }
162 }
163 /*---------------------------------
164 output Global Header Codes
165 ----------------*/
166 add_action( 'wp_head', 'accodes_header_output' );
167 function accodes_header_output() {
168
169 $hide_global_header = '';
170 $current_page_id = get_current_page_id();
171 $global_header_codes ='';
172 $single_header_codes = '';
173 $output = '';
174
175 if ( $current_page_id ) {
176 //value is 'on' if checked
177 $hide_global_header = get_post_meta( $current_page_id, 'accodes_hide_header', true );
178 //get Single Header Codes
179 $get_single_header_codes = get_post_meta( $current_page_id , '_accodes_header_metabox', true );
180 if($get_single_header_codes !='')
181 {
182 $single_header_codes = PHP_EOL.'<!-- Single header Scripts by Add Custom Codes -->'. PHP_EOL;
183 $single_header_codes .= $get_single_header_codes;
184 $single_header_codes .= PHP_EOL.'<!-- End of Single header Scripts by Add Custom Codes -->'. PHP_EOL;
185 }
186 }
187
188 //get global header - if not set to hide
189 if($hide_global_header != 'on')
190 {
191 //get Global Header Codes
192 $get_global_header_codes = get_option( 'custom-header-codes-input' );
193 if($get_global_header_codes!='')
194 {
195 $global_header_codes = PHP_EOL.'<!-- Global Header Scripts by Add Custom Codes -->'. PHP_EOL;
196 $global_header_codes .= $get_global_header_codes;
197 $global_header_codes .= PHP_EOL.'<!-- End - Global Header Scripts by Add Custom Codes -->'. PHP_EOL;
198 }
199 }
200 $output .= $global_header_codes;
201 $output .= $single_header_codes;
202
203 if($output !='')
204 {
205 echo $output;
206 }
207
208 }
209
210
211
212 /*---------------------------------
213 output Global Footer Codes
214 ----------------*/
215 add_action( 'wp_footer', 'accodes_footer_output' );
216 function accodes_footer_output() {
217
218 $hide_global_footer = '';
219 $current_page_id = get_current_page_id();
220 $global_footer_codes ='';
221 $single_footer_codes = '';
222 $output = '';
223
224 if ( $current_page_id ) {
225 //value is 'on' if checked
226 $hide_global_footer = get_post_meta( $current_page_id, 'accodes_hide_footer', true );
227 //get Footer Codes for Single
228 $get_single_footer_codes = get_post_meta( $current_page_id , '_accodes_footer_metabox', true );
229 if($get_single_footer_codes !='')
230 {
231 $single_footer_codes = PHP_EOL.'<!-- Single Footer Scripts by Add Custom Codes -->'. PHP_EOL;
232 $single_footer_codes .= $get_single_footer_codes;
233 $single_footer_codes .= PHP_EOL.'<!-- End - Single Footer Scripts by Add Custom Codes -->'. PHP_EOL;
234 }
235 }
236
237 //get global footer - if not set to hide
238 if($hide_global_footer != 'on')
239 {
240 //get Global Footer codes
241 $get_global_footer_codes = get_option( 'custom-footer-codes-input' );
242 if($get_global_footer_codes!='')
243 {
244 $global_footer_codes = PHP_EOL.'<!-- Global Footer Scripts by Add Custom Codes -->'. PHP_EOL;
245 $global_footer_codes .= $get_global_footer_codes;
246 $global_footer_codes .= PHP_EOL.'<!-- End - Global Footer Scripts by Add Custom Codes -->'. PHP_EOL;
247 }
248 }
249 $output .= $global_footer_codes;
250 $output .= $single_footer_codes;
251
252 if($output !='')
253 {
254 echo $output;
255 }
256
257 }
258
259
260
261 /*----------------
262 * Individual pages
263 * -----------------------*/
264
265 /**
266 * Create the meta boxes for Single post, page, product any other custom post type
267 */
268 function _accodes_create_metabox_single() {
269
270 $post_types = get_post_types( '', 'names' );
271 $post_types = array_merge( $post_types, array( 'post', 'page' ) );
272
273 foreach ( $post_types as $post_type ) {
274 add_meta_box( '_accodes_metabox', 'Add Custom Codes by Mak', '_accodes_render_metabox', $post_type,
275 'normal', 'default');
276 }
277
278 }
279 add_action( 'add_meta_boxes', '_accodes_create_metabox_single' );
280
281
282
283
284 /*-------------------------------
285 * Display Meta Boxes for Single
286 * ----------------------------*/
287 function _accodes_render_metabox() {
288 // Variables
289 global $post; // Get the current post data
290 $header_script = get_post_meta( $post->ID, '_accodes_header_metabox', true ); // Get the saved values
291 $footer_script = get_post_meta( $post->ID, '_accodes_footer_metabox', true ); // Get the saved values
292
293 $hide_header = get_post_meta( $post->ID, 'accodes_hide_header', true );
294 $hide_footer = get_post_meta( $post->ID, 'accodes_hide_footer', true );
295
296 include('single-meta.php');
297
298 /* check if the submission come from actual server dashboard */
299 wp_nonce_field( '_accodes_form_metabox_nonce', '_accodes_form_metabox_process' );
300
301 }
302
303
304 /*-------------------------
305 * update data on post save - Single
306 ---------------------------*/
307 function _accodes_save_metabox_single( $post_id, $post ) {
308
309 // Verify data came from edit/dashboard screen
310 if ( !wp_verify_nonce( $_POST['_accodes_form_metabox_process'], '_accodes_form_metabox_nonce' ) ) {
311 return $post->ID;
312 }
313
314 // Verify user has permission to edit post
315 if ( !current_user_can( 'edit_post', $post->ID )) {
316 return $post->ID;
317 }
318
319 //Get values of meta boxes
320 $accodes_header_metabox = isset( $_POST['_accodes_header_metabox'] ) ? $_POST['_accodes_header_metabox'] : '';
321 $accodes_footer_metabox = isset( $_POST['_accodes_footer_metabox'] ) ? $_POST['_accodes_footer_metabox'] : '';
322
323 // Get values of checkbox
324 $hide_header = isset( $_POST['accodes_hide_header'] ) ? $_POST['accodes_hide_header'] : '';
325 $hide_footer = isset( $_POST['accodes_hide_footer'] ) ? $_POST['accodes_hide_footer'] : '';
326
327 //Update values of meta boxes
328 update_post_meta( $post->ID, '_accodes_header_metabox', $accodes_header_metabox );
329 update_post_meta( $post->ID, '_accodes_footer_metabox', $accodes_footer_metabox );
330
331 //Update values of check boxes
332 update_post_meta( $post->ID, 'accodes_hide_header', $hide_header );
333 update_post_meta( $post->ID, 'accodes_hide_footer', $hide_footer );
334
335 }
336 add_action( 'save_post', '_accodes_save_metabox_single', 1, 2 );
337 ?>