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 / 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' );
@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 / 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 / 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 / Add-Javascript-AJAX-Support.php
Last active January 1, 2021 16:12
Enqueue jQuery library as well as your plugin’s custom JavaScript file by adding it to your plugin.
<?php // used here only for enabling syntax highlighting. Leave this out if it's already included in your plugin file.
// Fires after WordPress has finished loading, but before any headers are sent.
add_action( 'init', 'script_enqueuer' );
function script_enqueuer() {
// Register the JS file with a unique handle, file location, and an array of dependencies
wp_register_script( "liker_script", plugin_dir_url(__FILE__).'liker_script.js', array('jquery') );
@SalmanRavoof
SalmanRavoof / liker_script.js
Created August 9, 2019 04:37
JavaScript file to add AJAX functionality to your plugin. The 'myAjax.ajaxurl' value is the admin-ajax.php file for your website.
jQuery(document).ready( function() {
jQuery(".user_like").click( function(e) {
e.preventDefault();
post_id = jQuery(this).attr("data-post_id");
nonce = jQuery(this).attr("data-nonce");
jQuery.ajax({
type : "post",
dataType : "json",
url : myAjax.ajaxurl,
data : {action: "my_user_like", post_id : post_id, nonce: nonce},
@SalmanRavoof
SalmanRavoof / test-highlight.php
Created August 11, 2019 15:45 — forked from polybuildr/test-highlight.php
Testing GitHub Gists syntax highlighting for PHP without an opening <?php tag.
$foo = "bar";
echo $foo;
@SalmanRavoof
SalmanRavoof / test.php
Created August 20, 2019 07:38
A simple test PHP file.
<html>
<head>
<title>PHP-Test</title>
</head>
<body>
<?php echo '<h1>Hello World!</h1><h3>Welcome to WPMU DEV</h3>'; ?>
</body>
</html>
@SalmanRavoof
SalmanRavoof / wp-config.php
Created October 9, 2019 20:30
Force your WordPress site to use SSL on both frontend and backend.
define('FORCE_SSL', true);
define('FORCE_SSL_ADMIN',true);
@SalmanRavoof
SalmanRavoof / .htaccess
Created October 9, 2019 20:36
Force any HTTP request to redirect to HTTPS by adding the first three lines to your WordPress .htaccess file. Never put it in between the WordPress tags.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# BEGIN WordPress
# END WordPress