-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
123 lines (109 loc) · 4.27 KB
/
plugin.php
File metadata and controls
123 lines (109 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* @package Infinus
* @version 1.1.0
*/
/*
Plugin Name: Consent Popup
Plugin URI: https://infinus.ca
Description: Custom Consent Popup with admin settings
Author: Brendan Carr
Version: 1.1.0
Author URI: https://brendancarr.ca
*/
$plugin = WP_PLUGIN_DIR . '/consent';
function js_script() {
wp_enqueue_script( 'theme-script', plugins_url( 'script.js' , __FILE__ ), array( 'jquery' ), '', true );
}
function css_styles() {
wp_enqueue_style('default-style', plugins_url( 'style.css' , __FILE__ ) );
}
if ( is_dir( $plugin ) && !is_admin() ) {
add_action('wp_enqueue_scripts', 'js_script');
add_action('wp_enqueue_scripts', 'css_styles');
}
// Add admin menu
function consent_popup_menu() {
add_options_page('Consent Popup Settings', 'Consent Popup', 'manage_options', 'consent-popup-settings', 'consent_popup_settings_page');
}
add_action('admin_menu', 'consent_popup_menu');
// Register settings
function consent_popup_register_settings() {
register_setting('consent_popup_options', 'consent_popup_bg_color');
register_setting('consent_popup_options', 'consent_popup_custom_code');
}
add_action('admin_init', 'consent_popup_register_settings');
// Settings page
function consent_popup_settings_page() {
?>
<div class="wrap">
<h1>Consent Popup Settings</h1>
<form method="post" action="options.php">
<?php settings_fields('consent_popup_options'); ?>
<?php do_settings_sections('consent_popup_options'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Background Color</th>
<td><input type="text" name="consent_popup_bg_color" value="<?php echo esc_attr(get_option('consent_popup_bg_color')); ?>" class="color-picker" /></td>
</tr>
<tr valign="top">
<th scope="row">Tracking Code (gTag, Pixel)</th>
<td><textarea name="consent_popup_custom_code" rows="10" cols="50"><?php echo esc_textarea(get_option('consent_popup_custom_code')); ?></textarea></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Enqueue color picker
function consent_popup_admin_scripts($hook) {
if ($hook != 'settings_page_consent-popup-settings') {
return;
}
wp_enqueue_style('wp-color-picker');
wp_enqueue_script('wp-color-picker');
wp_enqueue_script('consent-popup-admin', plugins_url('admin-script.js', __FILE__), array('wp-color-picker'), false, true);
}
add_action('admin_enqueue_scripts', 'consent_popup_admin_scripts');
// Enqueue custom code in header
function consent_popup_custom_code() {
$custom_code = get_option('consent_popup_custom_code');
if (!empty($custom_code)) {
echo $custom_code;
}
}
add_action('wp_head', 'consent_popup_custom_code');
// Update popup background color
function consent_popup_update_bg_color() {
$bg_color = get_option('consent_popup_bg_color');
if (!empty($bg_color)) {
echo '<style>.cookie-prompt { background-color: ' . esc_attr($bg_color) . ' !important; }</style>';
}
}
add_action('wp_head', 'consent_popup_update_bg_color');
function get_consent_popup() {
ob_start();
?>
<div id="cookieConsentPrompt" class="cookie-prompt">
<p>This website uses cookies to provide you with a better browsing experience. By clicking "Accept," you consent to the use of cookies.</p>
<div class="button-group">
<button onclick="setCookieConsent(true)">Accept</button>
<button onclick="setCookieConsent(false)">Decline</button>
</div>
</div>
<?php
echo ob_get_clean();
wp_die();
}
add_action('wp_ajax_get_consent_popup', 'get_consent_popup');
add_action('wp_ajax_nopriv_get_consent_popup', 'get_consent_popup');
function consent_popup_enqueue_scripts() {
wp_enqueue_script('theme-script', plugins_url('consent/script.js', __FILE__), array('jquery'), '', true);
wp_localize_script('theme-script', 'consentPopupAjax', array('ajaxurl' => admin_url('admin-ajax.php')));
}
// Replace the existing js_script function call with this:
if (is_dir($plugin) && !is_admin()) {
add_action('wp_enqueue_scripts', 'consent_popup_enqueue_scripts');
add_action('wp_enqueue_scripts', 'css_styles');
}