Skip to content

Instantly share code, notes, and snippets.

View mcascardi's full-sized avatar
🚙

Matt Cascardi mcascardi

🚙
View GitHub Profile
@mcascardi
mcascardi / functions.php
Last active November 24, 2025 03:28
Wordpress wp-json API example using OOP
<?php
include_once 'news-api.class.php';
// Implementation Steps:
// Step 1: Call init() in your custom theme or custom plugin code.
$news_api = new News_API();
$news_api->init();
// Step 2: Use the static News_API::get_news_api_posts() method to retrieve the posts in your template file.
$query = new \WP_Query( ['post_type' => 'news', 'posts_per_page' => 5] );
$fetched_news_posts = News_API::get_news_api_posts();
<?php
/**
* Show Video
*
* Creates a video element that plays in modal window.
*/
function show_video($type = 'default', $thumb = 0, $post_id = null) {
// Assume this function returns the html for a video player
}
@mcascardi
mcascardi / wp-security.php
Last active November 21, 2025 20:02
Security configurations for WordPress
<?php
// Remove links for blog clients, shortlink, and generator info from header
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'wp_generator');
// Remove Yoast SEO next/prev links
add_filter('wpseo_prev_rel_link', '__return_empty_string' );
@mcascardi
mcascardi / convert-short-open-tags.bash
Last active November 24, 2025 03:26 — forked from jankkhvej/shellcommand.sh
Descend into a directory, parse code for short open tags and convert them to normal
#!/bin/bash
find $1 -type f -iname "*.php" -exec php -d short_open_tag=On ./convert.php {} \;
@mcascardi
mcascardi / remove_submodule.sh
Created September 29, 2020 00:49
Process for removing a git submodule and merging the history back into the parent repo
#!/bin/bash
# Gratitude: https://stackoverflow.com/questions/1759587/how-to-un-submodule-a-git-submodule
origin=submodule_origin
url=git://url/to/submodule/origin
path=submodule_path
echo "git remote add $origin $url"
echo "git fetch $origin"
@mcascardi
mcascardi / apache-wp-uploads-config.conf
Created September 21, 2020 23:19
A configuration snippet for spoofing the uploads directory in a WP local development environment.
# Apache example configuration, added inside the <VirtualHost> directive, or root server block if VirtualHost is not being used.
<Directory "${SRVROOT}/htdocs/www.example.local/wp-content/uploads">
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) https://www.example.com/wp-content/uploads/$1 [L,R,NE]
</IfModule>
</Directory>
# Nginx example configuration, added inside the server {} block
# Uploads redirected to the hostname of your production server
location /wp-content/uploads/ {
try_files $uri @uploads-redirect;
}
location @uploads-redirect {
expires 30s;
return 301 https://{HOSTNAME}/$request_uri;
@mcascardi
mcascardi / client.class.php
Last active March 9, 2023 23:07
Customized version of cf_deploy_client class
<?php
class cf_deploy_client extends cfd_common {
protected $use_compression = CF_DEPLOY_USE_COMPRESSION;
public function __construct() {
$this->options = maybe_unserialize(get_option(CF_DEPLOY_SETTINGS, array()));
}
/**
@mcascardi
mcascardi / ajax.php
Created October 31, 2018 17:33
Fast AJAX handler for wp
<?php
/**
* Hello, this is a minimal ajax handler for WP.
* Based off of: https://coderwall.com/p/of7y2q/faster-ajax-for-wordpress which didn't work for me out of the box.
* Note: replace tld with your own prefix.
* Handles get and post type requests.
* Assumes you put this file in the root directory of your theme folder.
*/
$_tld_ajax = [];
<?php
/**
* Programmatically logs a user in
*
* @param string $username
* @return bool True if the login was successful; false if it wasn't
*/
function programmatic_login( $username ) {