Skip to content

Instantly share code, notes, and snippets.

/**
* Get information about available image sizes
*/
function get_image_sizes($size = '')
{
global $_wp_additional_image_sizes;
$sizes = array();
$get_intermediate_image_sizes = get_intermediate_image_sizes();
@dannyconnolly
dannyconnolly / modal.js
Last active February 4, 2021 19:19
Vanilla JS Modal
(function() {
// Define constructor
this.Modal = function() {
// Create global element references
this.closeButton = null;
this.modal = null;
this.overlay = null;
// Determine proper prefix
@dannyconnolly
dannyconnolly / parse.php
Created September 19, 2016 23:06
parse wp feed
<?php
$article_string = file_get_contents( $url );
$article_string = preg_replace_callback('/<!\[CDATA\[(.*)\]\]>/', 'filter_xml', $article_string);
$article_xml = simplexml_load_string($article_string);
$namespaces = $article_xml->getNamespaces(true); // get namespaces
function filter_xml($matches) {
return trim(htmlspecialchars($matches[1]));
}
//
// foreach ($article_xml->channel->item as $item) {
@dannyconnolly
dannyconnolly / translate_woocommerce.php
Created December 18, 2015 13:22
Change SKU text label in woocommerce to Product Code
function translate_woocommerce($translation, $text, $domain) {
if ($domain == 'woocommerce') {
switch ($text) {
case 'SKU':
$translation = 'Product Code';
break;
case 'SKU:':
$translation = 'Product Code:';
break;
@dannyconnolly
dannyconnolly / media-taxonomies.php
Created November 2, 2015 08:55
Add taxonomies to worpress media attachments
/**
* add categories for attachments
*/
function add_categories_for_attachments() {
register_taxonomy_for_object_type('category', 'attachment');
}
add_action('init', 'add_categories_for_attachments');
@dannyconnolly
dannyconnolly / Add custom field to woocommerce checkout.
Created June 17, 2015 09:52
Add custom field to woocommerce checkout.
/**
* Add vehicle registration field to the checkout page
*/
add_action( 'woocommerce_after_order_notes', 'car_registration_checkout_field' );
function car_registration_checkout_field( $checkout ) {
echo '<div id="car_registration_checkout_field"><h2>' . __('Car Registration') . '</h2>';
@dannyconnolly
dannyconnolly / Add css class to top selling products
Created April 13, 2015 13:08
Add css class to top selling products
/**
* Add topseller class to product
**/
$query = "SELECT ID FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} pm ON ( pm.post_id = p.ID AND pm.meta_key='total_sales' )
WHERE p.post_type = 'product'
AND p.post_status = 'publish'
ORDER BY pm.meta_value+0 DESC
LIMIT 10";
@dannyconnolly
dannyconnolly / Import an sql dump file with PHP
Last active August 29, 2015 14:10
Import an sql dump file with PHP
// Name of the file
$filename = 'DUMPFILEHERE.sql';
// MySQL host
$mysql_host = 'DB_HOST';
// MySQL username
$mysql_username = 'DB_USER';
// MySQL password
$mysql_password = 'DB_PASSWORD';
// Database name
$mysql_database = 'DB_NAME';
//* Add walker class that displays menu item descriptions
class Menu_With_Description extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
function get_product_by_sku( $sku ) {
global $wpdb;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
if ( $product_id ) return new WC_Product( $product_id );
return null;
}