Skip to content

Instantly share code, notes, and snippets.

View get_wordpress_image_sizes.php
/**
* 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 Feb 4, 2021
Vanilla JS Modal
View modal.js
(function() {
// Define constructor
this.Modal = function() {
// Create global element references
this.closeButton = null;
this.modal = null;
this.overlay = null;
// Determine proper prefix
View parse.php
<?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 Dec 18, 2015
Change SKU text label in woocommerce to Product Code
View translate_woocommerce.php
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 Nov 2, 2015
Add taxonomies to worpress media attachments
View media-taxonomies.php
/**
* 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 Jun 17, 2015
Add custom field to woocommerce checkout.
View 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 Apr 13, 2015
Add css class to top selling products
View 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 Aug 29, 2015
Import an sql dump file with PHP
View 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';
View Wordpress Menu with Description
//* 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;
View How to Find Product by SKU in WooCommerce
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;
}