Code Snippets

React

useDebounce Hook

Debouncing is crucial for optimizing performance in scenarios like search inputs or resizing events, where you want to delay execution until the user stops typing or interacting.


import { useState, useEffect } from 'react';

function useDebounce(value, delay) {
  const [debouncedValue, setDebouncedValue] = useState(value);

  useEffect(() => {
    const handler = setTimeout(() => {
      setDebouncedValue(value);
    }, delay);

    return () => {
      clearTimeout(handler);
    };
  }, [value, delay]);

  return debouncedValue;
}

// Usage example:
// const debouncedSearch = useDebounce(searchTerm, 500);
// useEffect(() => { fetchResults(debouncedSearch); }, [debouncedSearch]);
7/25/2025
5 min read
View Full Snippet

Recent Snippets

React

useLocalStorage Hook

This hook synchronizes a piece of state with the browser’s local storage, allowing data to persist across page reloads. It’s essential for features like saving user preferences or form drafts.


import { useState, useEffect } from 'react';

function useLocalStorage(key, initialValue) {
  const [storedValue, setStoredValue] = useState(() => {
    try {
      const item = window.localStorage.getItem(key);
      return item ? JSON.parse(item) : initialValue;
    } catch (error) {
      console.error(error);
      return initialValue;
    }
  });

  useEffect(() => {
    try {
      window.localStorage.setItem(key, JSON.stringify(storedValue));
    } catch (error) {
      console.error(error);
    }
  }, [key, storedValue]);

  return [storedValue, setStoredValue];
}

// Usage example:
// const [theme, setTheme] = useLocalStorage('theme', 'light');
7/25/20255 min read
WordPress

Hide WordPress Version from Source Code

Prevent attackers from knowing your WordPress version, which can reduce targeted exploits.


remove_action('wp_head', 'wp_generator');
7/25/20255 min read
php

Disable XML-RPC in WordPress

XML-RPC is rarely used in modern WordPress setups and is a common attack vector for brute force and DDoS. Disable it to improve security.


add_filter('xmlrpc_enabled', '__return_false');
7/25/20255 min read

More Snippets

.htaccess

Default .htaccess Configuration for WordPress Sites

Default WordPress .htaccess rules used for handling pretty permalinks and request routing.
If your WordPress site is showing HTTP 404 errors on posts or pages, this code is essential to restore proper URL rewriting.
Place it in the .htaccess file located in the root WordPress directory (usually public_html).


# BEGIN WordPress

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress
7/25/2025
5 min
php

Manually Add a WordPress Admin User via FTP & Functions.php

Creates a new WordPress admin user manually via functions.php. Useful when locked out of the dashboard. Remove the code after login to prevent unauthorized access.

Tips:

  • Replace 'Username', 'Password', and 'email@domain.com' with your desired credentials.

  • After the admin is created, remove this code from functions.php to avoid security risks.


function wpb_admin_account() {
    $user  = 'Username';
    $pass  = 'Password';
    $email = 'email@domain.com';

    if ( !username_exists( $user ) && !email_exists( $email ) ) {
        $user_id = wp_create_user( $user, $pass, $email );
        $user    = new WP_User( $user_id );
        $user->set_role( 'administrator' );
    }
}
add_action( 'init', 'wpb_admin_account' );
7/25/2025
5 min