Skip to content

Instantly share code, notes, and snippets.

@avenirer
avenirer / featured_images.php
Created April 29, 2022 06:20
Get featured images for multiple posts at one time (in one query)
@avenirer
avenirer / wordpress_remove_whitespaces.sql
Created August 26, 2019 09:48
Wordpress mysql - remove whitespaces from post_excerpt and post_content
UPDATE wp_posts SET post_excerpt = REPLACE(post_excerpt, ' ', '');
UPDATE wp_posts SET post_excerpt = REPLACE(post_excerpt, '\t', '' );
UPDATE wp_posts SET post_excerpt = REPLACE(post_excerpt, '\n', '');
UPDATE wp_posts SET post_content = TRIM(Replace(Replace(post_content,'\t',''));
@avenirer
avenirer / functions.php
Last active June 27, 2019 08:03
Wordpress - get hierarchical parents of a taxonomy term
<?php
/**
* Retrieve an array of taxonomy terms that represent the parents of the given term
*
* @param $term_obj_or_id
* @param $taxonomy
* @param array $hierarchy
*
* @return array
*/
@avenirer
avenirer / functions.php
Last active March 14, 2019 11:56
Wordpress - allow any extension
<?php
// Solution started from: https://wordpress.stackexchange.com/questions/231448/how-to-add-dot-in-post-slug
// This will allow the "." character inside urls
function allow_htm_extensions( $title, $raw_title = '', $context = 'display' ) {
// IN HERE WE WRITE THE ACCEPTED EXTENSIONS
$allowed_extensions = array('.htm','.html');
$title = strip_tags($title);
// Preserve escaped octets.
$title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
@avenirer
avenirer / utils.php
Last active February 14, 2019 12:58
WordPress - get most used taxonomies
<?php
function getMostUsedTaxonomies($taxonomy, $fields = 'all', $orderBy = 'count', $orderDirection = 'desc', $limit = '30') {
$transientName = 'mostUsedTaxonomies_'.$taxonomy.'_'.$fields.'_'.$orderBy.'_'.$orderDirection.'_'.$limit;
$mainTechnologies = get_transient( $transientName );
if ( $mainTechnologies === false ) {
$params = array(
'taxonomy' => $taxonomy,
'hide_empty' => true,
'orderby' => $orderBy,
'order' => $orderDirection,
@avenirer
avenirer / functions.php
Last active November 19, 2018 15:05
Debug WordPress rewrite rules
<?php
function debug_rewrite_rules() {
global $wp, $template, $wp_rewrite;
echo '<pre>';
echo 'Request: ';
echo empty($wp->request) ? "None" : esc_html($wp->request) . PHP_EOL;
echo 'Matched Rewrite Rule: ';
echo empty($wp->matched_rule) ? None : esc_html($wp->matched_rule) . PHP_EOL;
echo 'Matched Rewrite Query: ';
@avenirer
avenirer / customExcerpt.php
Last active August 21, 2018 15:46
WordPress - This snippet, added in functions or where-ever, allows you to select the length and the link title of the excerpt
<?php
// CUSTOMIZE EXCERPT LENGTH
function get_the_excerpt_limit($charlength = 200, $post = null, $readMoreStr = '[...]')
{
$newExcerpt = '';
if(isset($post)) {
$excerpt = strlen($post->post_excerpt) > 0 ? $post->post_excerpt : wp_strip_all_tags($post->post_content);
}
else {
$excerpt = get_the_excerpt();