function() { return current_user_can('manage_options'); }, 'show_in_rest' => false, )); register_meta('post', 'aFhfc_body_code', array( 'auth_callback' => function() { return current_user_can('manage_options'); }, 'show_in_rest' => false, )); register_meta('post', 'aFhfc_footer_code', array( 'auth_callback' => function() { return current_user_can('manage_options'); }, 'show_in_rest' => false, )); } public static function menu() { add_options_page(AFHDFTRCD_NICK.' Plugin Options', AFHDFTRCD_NICK, 'manage_options', AFHDFTRCD_ID.'_options', array('aFHFCClass', 'options_page')); } public static function options_page() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.', 'addfunc-head-footer-code')); } $plugin_id = AFHDFTRCD_ID; include(self::file_path('options.php')); } public static function output_head_code() { $site_head_code = get_option('aFhfc_site_wide_head_code'); $meta_head_code = ((is_archive()) || (is_author()) || (is_category()) || (is_tag()) || (is_home()) || (is_search()) || (is_404())) ? '' : get_post_meta(get_the_ID(),'aFhfc_head_code',true); $head_replace = get_post_meta(get_the_ID(),'aFhfc_head_replace',true); if(!empty($head_replace)) { echo $meta_head_code."\n"; }else{ echo $site_head_code."\n".$meta_head_code."\n"; } } public static function output_body_code() { $site_body_code = get_option('aFhfc_site_wide_body_code'); $meta_body_code = ((is_archive()) || (is_author()) || (is_category()) || (is_tag()) || (is_home()) || (is_search()) || (is_404())) ? '' : get_post_meta(get_the_ID(),'aFhfc_body_code',true); $body_replace = get_post_meta(get_the_ID(),'aFhfc_body_replace',true); if(!empty($body_replace)) { return $meta_body_code."\n"; }else{ return $site_body_code."\n".$meta_body_code."\n"; } } public static function output_footer_code() { $site_footer_code = get_option('aFhfc_site_wide_footer_code'); $meta_footer_code = ((is_archive()) || (is_author()) || (is_category()) || (is_tag()) || (is_home()) || (is_search()) || (is_404())) ? '' : get_post_meta(get_the_ID(),'aFhfc_footer_code',true); $footer_replace = get_post_meta(get_the_ID(),'aFhfc_footer_replace',true); if(!empty($footer_replace)) { echo $meta_footer_code."\n"; }else{ echo $site_footer_code."\n".$meta_footer_code."\n"; } } } add_action('init', array('aFHFCClass','register_meta_keys')); if (is_admin()) { add_action('admin_init', array('aFHFCClass','register')); add_action('admin_menu', array('aFHFCClass','menu')); } $head_code_prior = get_option('aFhfc_head_code_priority'); if(!empty($head_code_prior)) { add_action('wp_head', array('aFHFCClass','output_head_code'),$head_code_prior); } else { add_action('wp_head', array('aFHFCClass','output_head_code')); } function aFHFCBuffRec() { ob_start(); } add_action('wp_head','aFHFCBuffRec'); function aFHFCBuffPlay() { $body_code = new aFHFCClass; $pattern = '/<[bB][oO][dD][yY]\s[A-Za-z]{2,5}[A-Za-z0-9 "_,=%*\'\/():;\[\]\-\.]+>|/'; $queue = array(); $tape = ob_get_clean(); preg_match($pattern,$tape,$queue); $q = empty($queue[0]) ? '' : $queue[0].$body_code->output_body_code(); echo preg_replace($pattern,$q,$tape); } add_action('wp_print_footer_scripts','aFHFCBuffPlay'); $footer_code_prior = get_option('aFhfc_footer_code_priority'); if(!empty($footer_code_prior)) { add_action('wp_footer', array('aFHFCClass','output_footer_code'),$footer_code_prior); } else { add_action('wp_footer', array('aFHFCClass','output_footer_code')); } endif; /* M E T A B O X F O R P O S T S ================================= Metabox w/head & footer fields for all post types (including custom) */ add_action('add_meta_boxes','aFhfc_add'); function aFhfc_add() { if(current_user_can('manage_options')) { $args = array('public'=>true); $post_types = get_post_types($args); add_meta_box('aFhfcMetaBox','Head & Footer Code','aFhfc_mtbx',$post_types,'normal','low'); } } function aFhfc_mtbx($post) { $values = get_post_custom($post->ID); $head_text = isset($values['aFhfc_head_code']) ? esc_attr($values['aFhfc_head_code'][0]) : ''; $head_replace = isset($values['aFhfc_head_replace']) ? esc_attr($values['aFhfc_head_replace'][0]) : ''; $body_text = isset($values['aFhfc_body_code']) ? esc_attr($values['aFhfc_body_code'][0]) : ''; $body_replace = isset($values['aFhfc_body_replace']) ? esc_attr($values['aFhfc_body_replace'][0]) : ''; $footer_text = isset($values['aFhfc_footer_code']) ? esc_attr($values['aFhfc_footer_code'][0]) : ''; $footer_replace = isset($values['aFhfc_footer_replace']) ? esc_attr($values['aFhfc_footer_replace'][0]) : ''; wp_nonce_field('aFhfc_nonce', 'aFhfc_mb_nonce'); ?>

WARNING
Mandatory reading:

Security Risk

Overview: Values entered here are stored and output raw into site pages without sanitization or escaping. Any HTML or JavaScript will execute in visitors' browsers with the same privileges as the page.

Potential impacts: Cross-site scripting (XSS), session or credential theft, data exfiltration, third-party script compromises that serve malware or tracking, broken layout or runtime errors, and degraded performance or outage if scripts are faulty or load slow resources.

High-risk patterns to avoid: eval(), document.write(), inline event handlers, inserting unreviewed third-party snippets, code that reads/writes cookies or localStorage, and scripts that load remote resources from untrusted domains.

Who should edit: Only fully trusted administrators (capability: manage_options) should add or modify these fields. Do not grant this capability to untrusted users.

Mitigations: Prefer server-side integrations or registering scripts via wp_enqueue_script(), host assets over HTTPS, enable Content Security Policy (CSP) and Subresource Integrity (SRI) when possible, review external sources before adding, and keep WordPress, themes, and plugins updated.

Guidelines

Follow these pragmatic steps to reduce risk and keep changes reversible:

  • Test changes in a staging or development environment first.
  • Create a full backup (files + database) before adding or modifying code.
  • Add only the minimal snippet required; prefer vendor libraries from a single, trusted source.
  • Prefer using wp_enqueue_script() in a theme or plugin instead of injecting code here when feasible.
  • For external scripts, use async/defer attributes and SRI (if available) and load from HTTPS endpoints only.
  • Document the change with a date, reason, and author so it can be audited and removed if needed.
  • Monitor the site after deployment for console errors, slow requests, security alerts, and unexpected network calls.
  • Remove or expire temporary marketing or tracking scripts promptly—do not leave unused code in place.

If you are not confident making these changes safely, consult a developer for assistance.

/>

/>

/>