Skip to content

Instantly share code, notes, and snippets.

@daggerhart
daggerhart / wordpress-missing-functions.php
Created March 24, 2016 22:19
WordPress functions that I tend to write often
<?php
/**
* Get a WP_Post object by its slug ( post_name )
*
* @param $post_name
*
* @return WP_Post|null
*/
function get_post_by_slug( $post_name ) {
@daggerhart
daggerhart / simple-php-template.php
Last active June 30, 2021 14:44
Example of a template function in PHP
<?php
/**
* Simple PHP Templating function
*
* @param $names - string|array Template names
* @param $args - Associative array of variables to pass to the template file.
* @return string - Output of the template file. Likely HTML.
*/
function template( $names, $args ){
// allow for single file names
<?php
/*
* Note! This is no longer updated here in the Gist. It has been moved to a repo.
*
* @link https://github.com/daggerhart/wp-custom-menu-items
*/
/**
* Class custom_menu_items
@daggerhart
daggerhart / Simple-PHP-Website.md
Created October 10, 2015 18:23
A very simple php website and contact form tutorial.

Simple PHP Website and Contact Form

This is purely for example and educational purposes.

The resulting contact form you will create with this example is extremely insecure. Do not use it on a live website.

Setup Environment

  1. Create website directory on Desktop
  2. Open terminal and test PHP php --version
@daggerhart
daggerhart / 1.WordPress-Developer-Environment-Setup.md
Last active March 26, 2023 14:50
Modern WordPress Theme Development

Development Software

  • VirtualBox - Virtualization software for running local operating systems within your computer. This allows us have a full version of linux within our computers that better match how a live webserver works.
  • Vagrant - A tool for provisioning (creating) virtual machines.
  • VVV - A pre-built, community-supported Vagrant configuration for WordPress development.
  • Git - Version control system for managing code during development. Easily allows for tracking changes, and merging new code into an existing project.
  • SourceTree - A GUI available on Windows and Mac for managing Git projects. This makes Git much easier to use, as we won't have to learn the command line interface.
  • Github.com - A website that provides free Git repositories for both open source and private projects.
  • SASS - (SCSS) A CSS preprocessing implementation that allows us to write much less CSS for a project. This basically makes CSS into a simple programming language.
@daggerhart
daggerhart / wp-disable-login-form.php
Last active January 13, 2023 19:10
Completely disable the WordPress login form. You have your reasons.
<?php
// pick a hook from the wp-login.php file that best our needs. I chose the filter: wp_login_errors
add_filter( 'wp_login_errors', 'my_login_form_lock_down', 90, 2 );
/**
* Completely lock down the WordPress login form by hijacking the page
* and only executing the the login header, footer, and necessary
* closing tags.
*
* Provide a secret way to show the login form as a url variable in
@daggerhart
daggerhart / wp-allow-img-in-comments.php
Last active March 6, 2018 11:41
Allow HTML img tags in WordPress comments
<?php
add_filter( 'wp_kses_allowed_html', array( $this, 'my_kses_allowed_html_hook' ), 20, 2 );
function my_kses_allowed_html_hook( $tags, $context = null ){
if ( 'post' == $context && ! isset( $tags['img'] ) ) {
$tags['img'] = array(
'src' => 1,
'height' => 1,
'width' => 1,
'alt' => 1,
@daggerhart
daggerhart / simple-json-api.php
Last active October 21, 2018 16:15
Simple Read-Only JSON API example for WordPress
<?php
/**
* Class Simple_Json_Api
*/
class Simple_Json_Api {
/**
* The top level argument for the endpoint.
* ex http://example.com/myjson/post/1
@daggerhart
daggerhart / wordpress-cleanup-basic.php
Created July 27, 2015 20:36
WordPress post data cleanup. Very basic.
<?php
/**
* Example of using wp_kses() to clean up WordPress post data.
*/
function __cleanup_post_data(){
global $wpdb;
// default wordpress "allowed html" for posts
$allowed_tags = wp_kses_allowed_html( 'post' );
@daggerhart
daggerhart / wordpress-cleanup-content.php
Last active March 6, 2018 11:42
Wordpres: Clean up content
<?php
function __cleanup_post_data(){
global $wpdb;
// default wordpress "allowed html" for posts
$allowed_tags = wp_kses_allowed_html( 'post' );
// don't allow spans
unset($allowed_tags['span']);