Skip to content

Instantly share code, notes, and snippets.

View SalmanRavoof's full-sized avatar
🎯
Focusing

Salman Ravoof SalmanRavoof

🎯
Focusing
View GitHub Profile
@SalmanRavoof
SalmanRavoof / AJAX-Action-Hook-Plugin-Functions.php
Last active January 1, 2021 16:11
Code to address the Ajax action without JavaScript enabled on the client side. Should be added to your plugin file.
<?php // used here only for enabling syntax highlighting. Leave this out if it's already included in your plugin file.
// define the actions for the two hooks created, first for logged in users and the next for logged out users
add_action("wp_ajax_my_user_like", "my_user_like");
add_action("wp_ajax_nopriv_my_user_like", "please_login");
// define the function to be fired for logged in users
function my_user_like() {
// nonce check for an extra layer of security, the function will exit if it fails
@SalmanRavoof
SalmanRavoof / Ready-AJAX-Call.php
Created August 9, 2019 03:23
Add this code to your theme's post template to have it prepared for making an Ajax call, with or without JavaScript enabled on the client side.
// The 'likes' meta key value will store the total like count for a specific post, it'll show 0 if it's an empty string
<?php
$likes = get_post_meta($post->ID, "likes", true);
$likes = ($likes == "") ? 0 : $likes;
?>
This post has <span id='like_counter'><?php echo $likes ?></span> likes<br>
// Linking to the admin-ajax.php file. Nonce check included for extra security. Note the "user_like" class for JS enabled clients.
<?php
@SalmanRavoof
SalmanRavoof / Add-WordPress-Dashboard-Widget.php
Created July 31, 2019 23:35
Add a custom widget to your WordPress dashboard.
// Add a new widget to the dashboard using a custom function
function wpmudev_add_dashboard_widgets() {
wp_add_dashboard_widget(
'wpmudev_dashboard_widget', // Widget slug
'My Custom Dashboard Widget', // Widget title
'wpmudev_new_dashboard_widget_function' // Function name to display the widget
);
}
// Register the new dashboard widget with the 'wp_dashboard_setup' action
add_action( 'wp_dashboard_setup', 'wpmudev_add_dashboard_widgets' );
@SalmanRavoof
SalmanRavoof / Remove-WordPress-Dashboard-Widgets.php
Last active September 18, 2020 17:16
Removes all the widgets from the WordPress dashboard screen for all non-admin users.
// function to remove the dashboard widgets, but only for non-admin users
// if you want to remove the widgets for admin(s) too, remove the 'if' statement within the function
function remove_dashboard_widgets() {
if ( ! current_user_can( 'manage_options' ) ) {
remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );