allow-admin-svg-uploads.php

Allow admins to upload SVGs

function allow_admin_svg_uploads($file_types)
{
	if (!current_user_can('administrator'))
	{
		return $file_types;
	}
	$file_types['svg'] = 'image/svg+xml';
	return $file_types;
}
add_filter('upload_mimes', 'allow_admin_svg_uploads');
current-year-shortcode.php

Display the current year (2023) by typing [current_year]

function mytheme_handle_current_year_shortcode()
{
	$year = date('Y');
	return $year;
}

add_shortcode('mytheme_current_year', 'mytheme_handle_current_year_shortcode');
debug-wordpress.php

Add these lines to wp-config.php to enable WordPress's debug log file and disable front-end error display

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
disable-wordpress-update-emails.php

Disable the emails that WordPress sends out after automatic updates.

add_filter('auto_core_update_send_email', '__return_false');
add_filter('auto_theme_update_send_email', '__return_false');
add_filter('auto_plugin_update_send_email', '__return_false');
extend-wordpress-login-duration.php

Increase the amount of time that users stay logged in.

//Increase the login session expiration time (by default, it's 2 days, or 14 days if you checked "Remember me")
function extend_wordpress_login_sessions($seconds, $user_id, $remember)
{
	if ($remember)
	{
		$expiration = 90 * 24 * 60 * 60; //Extend remembered sessions to 90 days
	}
	
	else
	{
		$expiration = 15 * 24 * 60 * 60; //Extend non-remembered sessions to 15 days
	}

	return $expiration;
}

add_filter('auth_cookie_expiration', 'extend_wordpress_login_sessions', 99, 3);
get-sorted-categories.php

Return a flat array of sorted categories with their basic info and indent level.

function get_sorted_categories($taxonomy, $meta_key_to_sort_by, $parent_category_id = 0, $indent_level = 0, &$nested_categories_array = null)
{
    if ($nested_categories_array === null)
    {
        $nested_categories_array = [];
    }

    $categories = get_categories
    (
        [
            'parent' => $parent_category_id,
            'hide_empty' => false,
            'taxonomy' => $taxonomy,
        ]
    );

    usort($categories, function($a, $b) use ($meta_key_to_sort_by)
    {
        $order_a = intval(get_term_meta($a->term_id, $meta_key_to_sort_by, true));
        $order_b = intval(get_term_meta($b->term_id, $meta_key_to_sort_by, true));
        return $order_a - $order_b;
    });

    foreach ($categories as $category)
    {
        $category_id = $category ?-> term_id;
        $category_name = $category ?-> name;
        $category_slug = $category ?-> slug;
        $category_count = $category ?-> count;
        $category_permalink = get_category_link($category_id);

        $nested_categories_array[] =
        [
            'category_id' => $category_id,
            'category_name' => $category_name,
            'category_slug' => $category_slug,
            'category_permalink' => $category_permalink,
            'category_count' => $category_count,
            'indent_level' => $indent_level,
        ];

        get_sorted_categories($taxonomy, $meta_key_to_sort_by, $category_id, $indent_level + 1, $nested_categories_array);
    }

    return $nested_categories_array;
}

$nested_audio_categories = get_sorted_categories('food_category', 'food_order');


Example of a returned array:

array (
  0 => 
  array (
    'category_id' => 183,
    'category_name' => 'Foods',
    'category_slug' => 'foods',
    'category_permalink' => 'https://example.com/food-category/foods/',
    'category_count' => 2,
    'indent_level' => 0,
  ),
  1 => 
  array (
    'category_id' => 15,
    'category_name' => 'Italian',
    'category_slug' => 'italian',
    'category_permalink' => 'https://example.com/food-category/italian/',
    'category_count' => 698,
    'indent_level' => 1,
  ),
  2 => 
  array (
    'category_id' => 28,
    'category_name' => 'Pizza',
    'category_slug' => 'pizza',
    'category_permalink' => 'https://example.com/food-category/pizza/',
    'category_count' => 477,
    'indent_level' => 2,
  ),
  3 => 
  array (
    'category_id' => 29,
    'category_name' => 'Japanese',
    'category_slug' => 'japanese',
    'category_permalink' => 'https://example.com/food-category/japanese/',
    'category_count' => 165,
    'indent_level' => 1,
  ),
wordpress-ajax-call.php

A simple example of a WordPress AJAX call.

function mysite_get_foobar_ajax()
{
	$response =	[
					'status'		=> 'success',
					'text_message'	=> 'The foobar was successful.',
					'html_message'	=> '<p class="success-message">The foobar was successful.</p>',
				];
	echo json_encode($response);
	wp_die();
}

add_action('wp_ajax_mysite_get_foobar_ajax', 'mysite_get_foobar_ajax');

?>


<script>
let form_data = new FormData();
form_data.append('action', 'mysite_get_foobar_ajax');

fetch('/wp-admin/admin-ajax.php', {method: 'POST', headers: {}, body: form_data})
.then
(
	response => response.json()
)
.then
(
	data =>
	{
		console.log(data);
		
		if (data.status === 'success')
		{
			alert(data.text_message);
		}
		
		else if (data.status === 'error')
		{
			alert(data.text_message);
		}
		
		else
		{
			throw 'Unexpected response status';
		}
	}
)
.catch(error =>
{
	console.log(error);
	alert('An unknown error occured.');
});
</script>
wordpress-search-bar.php

Create a simple WordPress search bar.

<form action="/" style="margin-bottom: 80px;">
	<input type="text" class="homepage-search" name="s" placeholder="Search the website...">
</form>
wordpress-shortcode.php

Create a simple WordPress shortcode.

/*
* Plugin Name: Custom Foobar Plugin
* Description: Our custom foobar plugin
* Version: 1.0
* Author: Author
* Author URI: https://www.example.com
*/

function handle_foobar_shortcode($original_shortcode_attributes)
{
	$attributes = shortcode_atts([
									'lorem'	=> null,
								],
								$original_shortcode_attributes);
	
	$lorem = $attributes['lorem'] ?? null;
	
	if ($attributes['lorem'] !== 'ipsum')
	{
		return '<p>Error: Unexpected value for "lorem" attribute (expected "ipsum").</p>';
	}
	
	$output_html = '<p>The shortcode works!</p>';
	
	return $output_html;
}

add_shortcode('foobar', 'handle_foobar_shortcode');
wordpress-show-all-hooks.php

Output the hook name of every WordPress filter/action being used.

$debug_tags = array();

add_action('all', function ($tag)
{
    global $debug_tags;
    if (in_array($tag, $debug_tags))
	{
        return;
    }
    echo "<pre>" . $tag . "</pre>";
    $debug_tags[] = $tag;
});

This website is owned and operated by SiteBolts. All the code snippets found here are intended to be a starting point and should not be treated as a perfect solution to every problem. We do not guarantee the performance, reliability, or security of the code found on this website. As always, you should ensure that you have adequate backups before adding, changing, or executing any code.