Skip to content

Instantly share code, notes, and snippets.

View kodie's full-sized avatar
💻
#doworkson

Kodie Grantham kodie

💻
#doworkson
View GitHub Profile
@kodie
kodie / client-index.html
Created October 29, 2019 17:35
Meteor - Display inline SVG content
<template name="Home">
{{> Svg svg='my-svg'}}
</template>
@kodie
kodie / wp_hm_clear_transients.php
Created December 20, 2019 20:00
A function that allows you to clear all transients either by running the function, calling it via AJAX, or by calling it via CURL
<?php
// Clears all transients, can be ran via the function, via AJAX or by manually running:
// curl -d "action=hm_clear_transients" https://website.com/wordpress/wp-admin/admin-ajax.php
add_action('wp_ajax_hm_clear_transients', 'hm_clear_transients');
add_action('wp_ajax_nopriv_hm_clear_transients', 'hm_clear_transients');
function hm_clear_transients() {
global $wpdb;
$rows = $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '%_transient_%'");
if (wp_doing_ajax()) wp_send_json_success($rows);
return $rows;
@kodie
kodie / wp_add_admin_menu_separators.php
Created March 12, 2020 15:32
Adds additional separators to WordPress's admin dashboard menu
<?php
// Adds additional separators to WordPress's admin dashboard menu
add_action('admin_menu', 'add_admin_menu_separators');
function add_admin_menu_separators() {
function add_separator($position) {
global $menu;
$index = 0;
foreach($menu as $offset => $section) {
@kodie
kodie / wp_get_subpage_template.php
Created August 21, 2020 16:22
Allows for subpages to have their own unique templates in WordPress (ie. /my-page/edit/ uses page-my-page-edit.php)
<?php
// Allows for subpages to have their own unique templates (ie. /my-page/edit/ uses page-my-page-edit.php)
add_filter('template_include', 'get_subpage_template');
function get_subpage_template($template, $page_id = null) {
if ($page_id === null) {
global $wp_query;
$page = $wp_query->queried_object;
} else {
$page = get_post($page_id);
}
@kodie
kodie / wp_custom_author_url_rewrite.php
Last active August 21, 2020 16:36
Enables https://mywebsite.com/@username for author URLs in WordPress
<?php
// Enables https://mywebsite.com/@username for author URLs
add_action('init', 'custom_author_url_rewrite');
function custom_author_url_rewrite() {
add_rewrite_rule('^@([a-zA-Z0-9.-_]+)/?', 'index.php?author_name=$matches[1]', 'top');
}
// Changes get_author_posts_url() response to https://mywebsite.com/@username author URLs
add_filter('author_link', 'custom_author_url_filter', 10, 3);
function custom_author_url_filter($link, $author_id, $author_nicename) {
@kodie
kodie / wp_profile_redirect.php
Created August 21, 2020 16:37
Redirects https://mywebsite.com/profile to current user's author URL
<?php
// Redirects https://mywebsite.com/profile to current user's author URL
add_action('wp', 'profile_redirect');
function profile_redirect() {
global $wp;
$page = trim(strtolower((string) $wp->request), '/');
if ($page === 'profile') {
wp_safe_redirect(get_author_posts_url(get_current_user_id()));
}
}
@kodie
kodie / nav_add_page_slug_class.php
Created August 21, 2020 16:38
Adds the page's slug to page nav items classes when using wp_nav_menu() in WordPress
<?php
// Adds the page's slug to page nav items classes when using wp_nav_menu()
add_filter('nav_menu_css_class', 'nav_add_page_slug_class', 10, 2);
function nav_add_page_slug_class($classes, $item) {
if ($item->object === 'page') {
$slug = get_post_field('post_name', $item->object_id);
$classes[] = "page-$slug";
}
return $classes;
}
@kodie
kodie / wp_nav_add_icons.php
Created August 21, 2020 16:39
Adds an SVG inside of page nav items when using wp_nav_menu() in WordPress
<?php
// Adds an SVG inside of page nav items when using wp_nav_menu()
add_filter('wp_nav_menu_objects', 'nav_add_icons', 10, 2);
function nav_add_icons($items, $args) {
if ($args->menu->slug === 'header-navigation') {
foreach ($items as $item) {
if ($item->object === 'page') {
$slug = get_post_field('post_name', $item->object_id);
$icon = get_stylesheet_directory_uri() . "/img/svg/nav-$slug.svg";
$item->title = "<img src='$icon' class='style-svg' /><span>{$item->title}</span>";
@kodie
kodie / wp_add_pagination_to_pages.php
Created August 21, 2020 16:59
This makes it possible to use `/page/` URLs with non-archive pages such as single pages in WordPress
<?php
// This makes it possible to use `/page/` URLs with non-archive pages such as single pages
add_action('init', 'add_pagination_to_pages');
function add_pagination_to_pages() {
$pages = array(
'flex/assessments' => 220460,
'flex/collections' => 215682,
'flex/lesson-plans' => 215685,
'flex/my-standards' => 220768,
'flex/resources' => 220383,
@kodie
kodie / kill_all_processes.sql
Created August 21, 2020 17:51
A MySQL command to kill all processes
# Run this on a MySQL server and it will echo out a command for you to copy, paste, and run to kill all currently running MySQL processes
SELECT GROUP_CONCAT(CONCAT('KILL ',id,';') SEPARATOR ' ') 'Paste the following query to kill all processes' FROM information_schema.processlist WHERE user<>'system user'\G