Skip to content

Instantly share code, notes, and snippets.

Dave James Miller davejamesmiller

Block or report user

Report or block davejamesmiller

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@davejamesmiller
davejamesmiller / auto-activate-plugins.php
Last active Jan 8, 2019
Automatically enable plugins in new WordPress Multisite blogs
View auto-activate-plugins.php
<?php
// There are three options (that I know of) for automatically enabling a plugin
// in new sites.
// 1. Move the plugin from wp-content/plugins/ to wp-content/mu-plugins/ (MU =
// Must Use). But then it cannot be deactivated for any site.
// 2. Click "Network Activate" instead of "Activate" to enable it for all sites.
// I didn't want to use this though because I didn't want to affect existing
@davejamesmiller
davejamesmiller / blogs-menu.php
Last active Mar 14, 2019
List of all blogs in a WordPress Multisite network
View blogs-menu.php
<?php
// This code was used to generate a menu showing all blogs on a WordPress
// Multisite installation.
// projects_menu() was used in the template to show a dropdown menu on all the
// sites.
// The shortcode [bloglist] was used in the homepage of the main blog to link to
// all the other blogs.
@davejamesmiller
davejamesmiller / url-to-domain.php
Last active Mar 29, 2019
Get the domain name from a URL for display purposes in PHP
View url-to-domain.php
<?php
// This is PHP function to convert a user-supplied URL to just the domain name,
// which I use as the link text.
// Remember you still need to use htmlspecialchars() or similar to escape the
// result.
function url_to_domain($url)
{
@davejamesmiller
davejamesmiller / README.md
Last active Jul 31, 2019
HTML to PDF conversion in PHP using wkhtmltopdf (and optionally Smarty)
View README.md

Recently I was asked to generate PDF invoices for an online shop. I looked at various PHP PDF generators, but wasn't particularly impressed with any of them.

Then I found (via Stack Overflow) a command-line HTML-to-PDF convertor called wkhtmltopdf, which uses WebKit (the same layout engine as Safari and Google Chrome) and therefore is very accurate.

There is a class for PHP integration on the Wiki, but I found it overly complicated and it uses temp files which aren't necessary. This is the code I wrote instead.

I used Smarty for generating the HTML for the PDF, but you can use any template engine, or pure PHP if you prefer.

Note: I originally tried to install wkhtmltopdf from source, but it's much easier to use the static binary instead.

@davejamesmiller
davejamesmiller / run-program.ahk
Last active Aug 8, 2019
Run a program with a keyboard shortcut with AutoHotKey
View run-program.ahk
; This is a simple and pretty generic example of an AutoHotkey script to run a
; program when you press a keyboard shortcut. Add as many of these as you want
; to a .ahk file, and set that to be run at startup.
; See the Hotkeys reference [1] for details of the modifiers and keys available.
; [1]: http://www.autohotkey.com/docs/Hotkeys.htm
; Win+Alt+G - Open Gmail in Chrome
@davejamesmiller
davejamesmiller / monitor-off.ahk
Last active Aug 11, 2019
Turn monitor off with a keyboard shortcut with AutoHotKey
View monitor-off.ahk
; This is part of my AutoHotkey [1] script that turns off my monitor when I
; press Win+\.
; It also turns the screensaver on at the same time, so that Windows Live
; Messenger (and any other programs) know I am away.
; I don't have a password on my screensaver, so there is a second version
; (Win+Shift+\) that locks the PC as well.
; Note: Sometimes the monitor comes back on after a second and I have to
@davejamesmiller
davejamesmiller / sleep.ahk
Last active Aug 11, 2019
Sleep/suspend/hibernate computer with a keyboard shortcut using AutoHotKey
View sleep.ahk
; This is part of my AutoHotkey [1] script that puts my computer to sleep when I
; press Win+F12.
; I chose that keyboard shortcut because it's very similar to the Fn+F12
; keyboard shortcut on my laptop.
; I don't have my PC set to require a password to resume, so I also have a
; second version (Win+Shift+F12) in case I want to lock the PC first.
; [1]: http://www.autohotkey.com/
@davejamesmiller
davejamesmiller / paste-as-plain-text.ahk
Last active Jun 20, 2019
Paste clipboard contents as plain text with AutoHotKey
View paste-as-plain-text.ahk
; This is part of my AutoHotkey [1] script that pastes the contents of the
; clipboard as plain text when I press Ctrl+Alt+Shift+V.
; [1]: http://www.autohotkey.com/
; Ctrl+Alt+Shift+V
^!+v::
Clip0 = %ClipBoardAll%
ClipBoard = %ClipBoard% ; Convert to plain text
@davejamesmiller
davejamesmiller / paste-current-date.ahk
Last active Apr 3, 2019
Paste the current date into any program using AutoHotKey
View paste-current-date.ahk
; This is part of my AutoHotkey [1] script that pastes the current date into any
; program when I press Ctrl+Alt+Shift+D.
; See the FormatTime reference [2] for other date/time formats.
; [1]: http://www.autohotkey.com/
; [2]: http://www.autohotkey.com/docs/commands/FormatTime.htm
^!+d:: ; When you press Ctrl+Alt+Shift+D
Clip0 = %ClipBoardAll% ; Store current clipboard in Clip0
@davejamesmiller
davejamesmiller / wp-config.php
Last active Jul 31, 2019
Set WordPress site URL in the config file instead of the database
View wp-config.php
<?php
// WordPress stores the site URL in the database by default (which I have never
// understood), and it's a pain to have to type out the UPDATE SQL or search in
// phpMyAdmin to change it. This is a simple way to put the URL into
// wp-config.php instead.
// Note that you will still need to update any URLs that appear in the content,
// especially when you copy a database from a development site to production:
// https://gist.github.com/davejamesmiller/a8733a3fbb17e0ff0fb5
You can’t perform that action at this time.