Skip to content

Instantly share code, notes, and snippets.

View terrytsang's full-sized avatar

Terry Tsang terrytsang

View GitHub Profile
@terrytsang
terrytsang / wp-codesnippet-hide-wordpress-admin-bar.php
Created September 9, 2022 13:17
How to hide WordPress Admin bar for all/non-admin users
<?php
//below code snippet is to be added to WordPress theme functions.php file
//option 1: remove the WordPress admin bar for all users
add_filter('show_admin_bar', '__return_false');
//option 2: hide the WordPress admin for everyone except users with administrator privileges
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
To access the functions.php file through your WordPress Admin interface, follow these steps:
Step 1: Log in to the WordPress Admin interface.
Step 2: In the left sidebar, hover over "Appearances", then click "Theme Editor" (recommended to use child theme).
Step 3: In the right sidebar, click functions.php and you can the code snippet at the bottom of the file.
Step 4: Click "Save" to apply the changes.
<?php
//add this code snippet to the wp-config.php file to increase your memory limit to 100MB
define('WP_MEMORY_LIMIT', '100M');
@terrytsang
terrytsang / wp-codesnippet-empty-trash-automatically-x-days.php
Last active September 9, 2022 14:07
To configure how regularly the WordPress trash is emptied (within x days)
<?php
//add below code into wp-config.php
define('EMPTY_TRASH_DAYS', 1 ); //Integer is the amount of days
@terrytsang
terrytsang / wp-codesnippet-enable-svg-upload.php
Last active September 9, 2022 14:33
Allow WordPress admin to upload SVG media file
<?php
//below code snippet is to be added to WordPress theme functions.php file
//enable SVG upload
function tt_enable_svg_upload( $mimes ) {
//Only admin can upload SVG file
if ( !current_user_can( 'administrator' ) ) {
return $mimes;
}
@terrytsang
terrytsang / wp-codesnippet-cofigure-autosave-timing.php
Created September 9, 2022 14:18
Configure autosave timing for WordPress
<?php
//below code should be added into your wp-config.php file
//Example 1: autosave every 30 seconds
define('AUTOSAVE_INTERVAL', 60); //autosave every 60 seconds
//Example 2: autosave every 5 minutes
define('AUTOSAVE_INTERVAL', 300);
@terrytsang
terrytsang / wp-codesnippet-disable-wordpress-search.php
Created September 11, 2022 07:09
Disable WordPress search function
<?php
//add this code into functions.php to disable search
function tt_filter_query( $query, $error = true ) {
if ( is_search() & !is_admin()) {
$query->is_search = false;
$query->query_vars['s'] = false;
$query->query['s'] = false;
// to error
@terrytsang
terrytsang / wp-codesnippet-disable-wordpress-search.php
Created September 11, 2022 07:09
Disable WordPress search function
<?php
//add this code into functions.php to disable search
function tt_filter_query( $query, $error = true ) {
if ( is_search() & !is_admin()) {
$query->is_search = false;
$query->query_vars['s'] = false;
$query->query['s'] = false;
// to error
@terrytsang
terrytsang / wp-codesnippet-change-post-excerpt-length.php
Last active September 11, 2022 09:58
change the default length of your excerpts in your WordPress website
<?php
//add this code into your theme functions.php
add_filter( 'excerpt_length', function($length) {
return 20; //edit the number to the amount of words you want to show in your excerpts
}, PHP_INT_MAX );
<?php
//add this code into your theme functions.php file
//step 1: change the admin login logo to the uploaded media image file
if ( !function_exists('tt_wp_admin_login_logo') ) :
function tt_wp_admin_login_logo() { ?>
<style type="text/css">
body.login div#login h1 a {
background-image: url('https://www.yoursite.com/wp-content/uploads/2022/09/your-logo.jpg');