Last active
September 26, 2019 19:39
-
-
Save grola/12be4de5082e9869bb39ac21bd2d2b19 to your computer and use it in GitHub Desktop.
Dismissible notice with AJAX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function wpdesk_permanent_dismisible_info_notice_for_administrator() { | |
$notice name = 'admin_notice'; | |
if ( intval( get_option( 'wpdesk_notice_dismiss_' . $notice_name, 0 ) ) !== 1 ) { | |
$user = wp_get current_user(); | |
if ( in_array( 'administrator', $user->roles ) ) { | |
?> | |
<div class="notice notice-info is-dismissible" data-notice-name="<?php echo $notice_name; ?>"> | |
<p>Info notice for administrator.</p> | |
</div> | |
<?php | |
} | |
} | |
} | |
add_action( 'admin_notices', 'wpdesk_permanent_dismisible_info_notice_for_administrator' ); | |
function wpdesk_ajax_notice_dismiss() { | |
if (isset($_POST['notice_name'])) { | |
$notice name = $_POST['notice_name']; | |
update_option( 'wpdesk_notice_dismiss_' . $notice_name, 1 ); | |
} | |
} | |
add_action( 'wp_ajax_wpdesk_notice_dismiss', 'wpdesk_ajax_notice_dismiss' ); | |
function wpdesk_admin_head_sctipt() { | |
?> | |
<script type="text/javascript"> | |
jQuery( document ).on( 'click', '.notice-dismiss', function() { | |
var notice_name = jQuery(this).closest('div.notice').data('notice-name'); | |
if ('' !== notice_name) { | |
jQuery.ajax({ | |
url: ajaxurl, | |
type: 'post', | |
data: { | |
action: 'wpdesk_notice_dismiss', | |
notice_name: notice_name | |
}, | |
success: function (response) { | |
} | |
}); | |
} | |
}); | |
</script> | |
<?php | |
} | |
add_action( 'admin_head', 'wpdesk_admin_head_sctipt' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment