Skip to content

Instantly share code, notes, and snippets.

View petersplugins's full-sized avatar

Peter Raschendorfer petersplugins

View GitHub Profile
<?php
// This code snippet removes the wrong password login screen shake
// Add a custom function to the login head
add_action( 'login_head', 'remove_login_shake' );
function remove_login_shake() {
// remove the wp_shake JavaScript
remove_action( 'login_head', 'wp_shake_js', 12 );
}
<?php
// This code snippet replaces the link to the locally hosted jQuery core with the link to the jQuery core hosted on jQuery CDN
// This reduces server load and page load time
// We do not use wp_deregister_script and wp_register_script but we directly access the global variable $wp_scripts
// because we want to replace the script with the correct version number
add_action( 'wp_print_scripts', 'replace_jquery' );
<?php
// This code snippet completely removes the admin bar from the front end
add_filter( 'show_admin_bar', '__return_false' );
?>
<?php
// This code snippet stops WordPress from redirecting a variety of URLs to the admin area
remove_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );
?>
<?php
// This code snippet blocks WordPress dashboard access for non admin users
// Add a custom function to check if the user is allowed to access the admin area
add_action( 'init', 'block_admin_access' );
function block_admin_access() {
if ( is_admin() && ! current_user_can( 'administrator' ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
// Redirect to home page if a non admin user tries to access admin area
wp_redirect( home_url() );
<?php
// This code snippet hides exclusive content if the user is not logged in
// To hide content you have to create a category with the slug 'only-for-members'
// All posts assigned to this category will be hidden from not logged in users
// Add a custom function to filter the content
add_filter( 'the_content', 'exclusive_content', 99 );
// If the user is not logged in, the excerpt will be displayed plus a message that the content is avialable only for registered users
<?php
// This code snippet allows you to use shortcodes inside text widgets
// Some themes enable this feature, so maybe there's no need to do this yourself
// If your theme does not, this tiny code can do it
add_filter( 'widget_text', 'do_shortcode' );
?>
<?php
// This code snippet adds a meta box in the right sidebar of the post screen that lists all available shortcodes
// As we use a default WordPress admin meta box the box can be closed to save space and opened when needed
// Add a custom function to create the meta box
add_action( 'add_meta_boxes', 'add_shortcodes_metabox' );
// This function adds the meta box
function add_shortcodes_metabox() {
<?php
// This code snippet automatically deletes the thumbnail if a post is deleted
// Add a custom function that runs before a post is deleted
add_action( 'before_delete_post', 'remove_post_thumbnail', 10 );
// This function deletes the image assigned as thumbnail from the media library
function remove_post_thumbnail( $post_id ) {
// First we have to check, of there's a thumbnail assigned to the post