Skip to content

Instantly share code, notes, and snippets.

@leepowers
leepowers / wpseo-yoast-sitemap-custom.php
Last active March 28, 2024 05:19
WordPress Yoast SEO: Create a custom sitemap with data not sourced from a custom post type.
<?php
/**
* USAGE:
* - Search and replace the `CUSTOM_SITEMAP` string with a lowercase identifier name: e.g., "vehicles", "customer_profiles"
* - The `your_alternate_data_source()` function does not exist; it's simply a placeholder for your own function that returns some sort of data array.
* - Uses heredocs for inline XML: https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
*/
/**
@leepowers
leepowers / wpseo-yoast-sitemap-post-types.php
Created February 12, 2020 05:52
WordPress Yoast SEO: Custom sitemap with data sourced from one or more post types
<?php
/**
* USAGE:
* - Configure the return value of the `CUSTOM_SITEMAP_post_types` to required post type(s) - otherwise populates sitemap with all posts and pages
* - Search and replace the `CUSTOM_SITEMAP` string with a lowercase identifier name: e.g., "myseo", "vehicles", "customer_profiles", "postspage", etc.
* - Uses heredocs for inline XML: https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
*/
/**
@leepowers
leepowers / cors-in-wp.php
Created March 5, 2022 00:48
WordPress CORS plugin
<?php
/**
* Plugin Name: CORSWP - CORS in WordPress
* Plugin URI: http://leepowers.co/
* Description: Enable CORSWP in WordPress
* Version: 0.0.1
* Author: Lee Powers
* Author URI: http://leepowers.co/
* Text Domain: cors-in-wp
@leepowers
leepowers / gist:9862956
Last active February 12, 2020 04:35
Fix `clean_url` entities
<?php
/**
* Fix esc_url/clean_url. Instead of escaping `&` to numeric character entities, this filter encodes them as `&amp;`
* Some browsers (such as Android Chrome 33.0.1750.170) don't parse `&#038;` correctly inside `<script src="..."></script>` tags.
* Such browsers leave out or otherwise mangle query string parameters.
* For instance, for a URL like `//maps.googleapis.com/maps/api/js?key=xxxx&#038;sensor=false` the `sensor` parameter is not being sent to the server correctly.
*/
function lpowers_fix_url_entities($url, $original_url, $_context) {
$bad_entity = "&#038;";
@leepowers
leepowers / gist:5267871
Created March 29, 2013 00:17
Wordpress `paginate_links_array` - get an array of objects that can be used to generate a custom HTML structure for pagination. Use this if you want the functionality of `paginate_links` without HTML being generated.
<?php
/**
* Modification of `paginate_links()` Wordpress built-in function.
* Returns an array of objects describing links instead of generating HTML for links.
* @param array $args Same as `paginate_links()` arguments array. Exceptions: `classes` mapping for CSS classes; `type` is ignored
* @return array Returns an array of objects representing links that can be used to generate HTML
*/
function paginate_links_array($args = '') {
$defaults = array(
@leepowers
leepowers / gist:5529546
Last active December 17, 2015 01:38
Auto-link URLs in an HTML document that haven't already been linked.
<?php
/**
* Safely auto-link HTML content.
* Doesn't double-link existing content.
* Plus logging features
* @param string $html
* @return string
*/
function safe_autolink($html) {
# Ignore everything in the root except the "wp-content" directory.
/*
!.gitignore
!wp-content/
# Ignore everything in the "wp-content" directory, except the "plugins" and "themes" directories.
wp-content/*
!wp-content/plugins/
!wp-content/themes/
@leepowers
leepowers / spawn-request.php
Created July 30, 2015 22:26
PHP Spawn HTTP Request Fork HTTP Request
/**
* Launch a new PHP process at the given URL
* @param string $url
*/
public function spawn_request($url) {
$url_a = parse_url($url);
$port = isset($url_a["port"]) ? $url_a["port"] : 80;
$fp = fsockopen($url_a["host"], $port, $errno, $errstr, 30);
$output = "GET " . $url_a["path"] . "?" . $url_a["query"] . " HTTP/1.0\r\n";
$output .= "Host: " . $url_a["host"] . "\r\n";
@leepowers
leepowers / wordpress_settings.php
Created September 27, 2014 23:52
Quick-and-dirty class for managing settings for a WordPress plugin.
<?php
/**
* Quick-and-dirty class for managing WordPress options for a plugin.
* Include this class in your plugin. Create an instance and use as follows:
*
* Required Usage:
*
* // Create new instance
* $settings = new wordpress_settings;