SNHPS
Quick and easy code snippets
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);
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-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.