Skip to content

Instantly share code, notes, and snippets.

@Nerd-Rush
Last active January 29, 2024 00:15
Show Gist options
  • Save Nerd-Rush/294f491cc1211903b40f7370ad6081aa to your computer and use it in GitHub Desktop.
Save Nerd-Rush/294f491cc1211903b40f7370ad6081aa to your computer and use it in GitHub Desktop.
Dismissable WordPress message to your clients.
// Client editing disclaimer
function custom_admin_notice_with_logo() {
$user_id = get_current_user_id();
if (get_user_meta($user_id, 'custom_notice_dismissed', true) !== 'yes') {
if (current_user_can('administrator') || current_user_can('editor') || current_user_can('author')) {
$nonce = wp_create_nonce('custom_notice_dismiss_nonce');
$image_url = 'https://nerdrush.com/wp-content/uploads/2023/08/Nerd-Rush-Logo-Blue-2023.svg'; // External image URL
?>
<div class="notice notice-warning custom-notice" style="padding-top: 20px;">
<img src="<?php echo esc_url( $image_url ); ?>" alt="Logo" style="width: 120px; display: block; margin-bottom: 10px;">
<p><strong>Error Resolution Notice:</strong> Please keep in mind that any unintended changes you make to the website (breakage, errors, etc.) that will need to be fixed by us, your website administrator, are considered billable. For more questions, please contact us at <a href="mailto:[email protected]">[email protected]</a></p>
<button class="button button-primary" id="custom-notice-button" data-nonce="<?php echo esc_attr($nonce); ?>" style="margin-bottom: 10px;">I Understand</button>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#custom-notice-button').click(function() {
var nonce = $(this).data('nonce');
$(this).closest('.custom-notice').fadeOut();
$.post(ajaxurl, {
action: 'dismiss_custom_notice',
nonce: nonce
});
});
});
</script>
<?php
}
}
}
add_action('admin_notices', 'custom_admin_notice_with_logo');
// AJAX action to set user meta with error handling
function dismiss_custom_notice() {
check_ajax_referer('custom_notice_dismiss_nonce', 'nonce');
$user_id = get_current_user_id();
if (current_user_can('administrator') || current_user_can('editor')) {
update_user_meta($user_id, 'custom_notice_dismissed', 'yes');
update_user_meta($user_id, 'custom_notice_dismissed_on', current_time('mysql')); // Save the current date and time
} else {
wp_send_json_error('Insufficient permissions', 403);
}
wp_die();
}
add_action('wp_ajax_dismiss_custom_notice', 'dismiss_custom_notice');
// Add custom field in user profile
function custom_user_profile_fields($user) {
if (!current_user_can('edit_user', $user->ID)) {
return;
}
$dismissed_on = get_user_meta($user->ID, 'custom_notice_dismissed_on', true);
$checked = get_user_meta($user->ID, 'custom_notice_dismissed', true) === 'yes';
$disabled = !current_user_can('administrator') ? 'disabled' : ''; // Check if the user is not an admin
?>
<h3>Developer Error Resolution Notice</h3>
<style type="text/css">
/* Style for disabled checkbox on hover */
input[type=checkbox]:disabled:hover {
cursor: not-allowed;
opacity: 0.5; /* Example style, adjust as needed */
}
</style>
<table class="form-table">
<tr>
<th><label for="custom_notice_dismissed">Disclaimer</label></th>
<td>
<input type="checkbox" name="custom_notice_dismissed" id="custom_notice_dismissed" <?php echo $checked ? 'checked' : ''; ?> <?php echo $disabled; ?>/>
<span>I understand that unintended changes made to the website by me, that will need to be fixed by the website administrator, are considered billable.</span>
<?php if ($checked): ?>
<br><span style="color:green">Accepted on: <?php echo date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($dismissed_on)); ?></span>
<?php endif; ?>
</td>
</tr>
</table>
<?php
}
add_action('show_user_profile', 'custom_user_profile_fields');
add_action('edit_user_profile', 'custom_user_profile_fields');
// Save the value of the custom field with sanitization
function save_custom_user_profile_fields($user_id) {
if (!current_user_can('edit_user', $user_id)) {
return false;
}
$value = isset($_POST['custom_notice_dismissed']) ? sanitize_text_field($_POST['custom_notice_dismissed']) : 'no';
$value = $value === 'on' ? 'yes' : 'no'; // Additional validation
update_user_meta($user_id, 'custom_notice_dismissed', $value);
}
add_action('personal_options_update', 'save_custom_user_profile_fields');
add_action('edit_user_profile_update', 'save_custom_user_profile_fields');
@Nerd-Rush
Copy link
Author

Edit: Added - Only admins can check/uncheck the disclaimer checkbox on the profile page. Added - Shows for authors as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment