Skip to content

Instantly share code, notes, and snippets.

@eduardoleao
eduardoleao / uninstall-terms-taxonomy-1.php
Created March 23, 2023 18:36 — forked from wpsmith/uninstall-terms-taxonomy-1.php
PHP: How to delete terms and taxonomies in uninstall.php
<?php
/** Delete All the Taxonomies */
foreach ( array( 'my_first_custom_tax', 'my_second_custom_tax', 'my_third_custom_tax', ) as $taxonomy ) {
// Prepare & excecute SQL, Delete Terms
$wpdb->get_results( $wpdb->prepare( "DELETE t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('%s')", $taxonomy ) );
// Delete Taxonomy
$wpdb->delete( $wpdb->term_taxonomy, array( 'taxonomy' => $taxonomy ), array( '%s' ) );
}
@eduardoleao
eduardoleao / better-author-urls.php
Created March 4, 2023 18:39 — forked from mjangda/better-author-urls.php
Better author URLs for WordPress. This enables author URLs like /author/firstname-lastname/ or /author/display_name/ instead of the usual /author/user_login/ but comes at a performance cost.
<?php
/**
* Handle author request to change query string as username
*
* This enables author URLs like /author/firstname-lastname/ or /author/display_name/ instead of the usual /author/user_login/
*/
function tc_handle_author_request( $query_vars ) {
if ( ! empty( $query_vars['author_name'] ) ) {
<?php
$iter = 500000;
$start = microtime(true);
$n = 0;
for ($c=0; $c<7*$iter; $c++) {
$i = $c % 16;
if ($i===0) $n += 0;
@eduardoleao
eduardoleao / in_array_vs_isset_vs_array_key_exists.php
Created May 13, 2021 17:56 — forked from alcaeus/in_array_vs_isset_vs_array_key_exists.php
Performance comparision: in-array vs. isset vs. array_key_exists
<?php declare(strict_types = 1);
function testPerformance($name, Closure $closure, $runs = 1000000)
{
$start = microtime(true);
for (; $runs > 0; $runs--)
{
$closure();
}
$end = microtime(true);
@eduardoleao
eduardoleao / get_posts_between.php
Created May 8, 2021 12:06 — forked from danielmartins/get_posts_between.php
Wordpress: Get posts between dates based on specific post meta field
<?php
/**
* Get posts in range date by a specific custom field(post meta)
* But, the date needs to be in the format YYYYMMDD
*
* @param string $start start date
* @param string $end end date
* @param string $field specific custom field
* @param string $ctype
<?php if (($wp_query->current_post +1) == ($wp_query->post_count)) {
echo 'This is the last post';
} ?>
<?php if (($wp_query->current_post +1) != ($wp_query->post_count)) {
echo 'This is the not the last post';
} ?>
@eduardoleao
eduardoleao / PHP Title Case
Created April 8, 2021 08:49 — forked from JonnyNineToes/PHP Title Case
PHP Title Case function
function titleCase($string) {
//reference http://grammar.about.com/od/tz/g/Title-Case.htm
// The below array contains the most commonly non-capitalized words in title casing - I'm not so sure about the commented ones that follow it...
$minorWords = array('a','an','and','as','at','but','by','for','in','nor','of','on','or','per','the','to','with'); // but, is, if, then, else, when, from, off, out, over, into,
// take the input string, trim whitespace from the ends, single out all repeating whitespace
$string = preg_replace('/[ ]+/', ' ', trim($string));
// explode string into array of words
$pieces = explode(' ', $string);
// for each element in array...
for($p = 0; $p <= (count($pieces) - 1); $p++){
<?php
# SHORTS
# DIRECTORY SEPARATOR
define( 'DS', DIRECTORY_SEPARATOR );
# PATH SEPARATOR
define( 'PS', PATH_SEPARATOR );
# Absolute path to the WordPress directory.
! defined( 'ABSPATH' )
AND define( 'ABSPATH', dirname( __FILE__ ).DS );
@eduardoleao
eduardoleao / viewport-units-ios.scss
Created August 26, 2020 09:07 — forked from BenMorel/viewport-units-ios.scss
SCSS mixin to support vh and vw units on all iOS Safari versions. Based on an idea by Patrick Burtchaell's: https://gist.github.com/pburtchaell/e702f441ba9b3f76f587
/**
* Fix for vw, vh, vmin, vmax on iOS 7.
* http://caniuse.com/#feat=viewport-units
*
* This fix works by replacing viewport units with px values on known screen sizes.
*
* iPhone 6 and 6 Plus cannot run iOS 7, so are not targeted by this fix.
* Target devices running iOS 8+ will incidentally execute the media query,
* but this will still produce the expected result; so this is not a problem.
@eduardoleao
eduardoleao / AssetRegister.php
Created August 6, 2020 15:37 — forked from dobbyloo/AssetRegister.php
Wordpress: wrapper class to register/enqueue styles and scripts in a theme.
<?php namespace PaintedCloud\WP\Classes;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class AssetRegister {
public $scripts_base_uri;
public $styles_base_uri;
protected $scripts;
protected $stylesheets;