Skip to content

Instantly share code, notes, and snippets.

View monecchi's full-sized avatar

Adriano Monecchi monecchi

View GitHub Profile
@monecchi
monecchi / woocommmerce-product-variations.php
Created May 19, 2016 14:13 — forked from RadGH/woocommmerce-product-variations.php
For the WooCommerce plugin for WordPress, this function returns all variations of a product by referencing the product ID.
<?php /*
Takes a product ID and returns an array that includes all types of variations of the product, and the attributes of that variation.
Variations are normally returned as a term object. They belong to the original product ID and the taxonomy name is equal to the attribute name.
If a custom variation is provided instead of a term object, the variation will simply be a string of the option's name.
Example return result is given below. This is a single product with one attribute and two different variations.
Array (
[pa_oregon-training-classes] => Array (
[attribute] => Array (
@monecchi
monecchi / functions.php
Created May 22, 2016 07:25 — forked from jchristopher/functions.php
Automatically link to WooCommerce Product Variation detail when searching for a Product Variation SKU
<?php
function my_maybe_woocommerce_variation_permalink( $permalink ) {
if ( ! is_search() ) {
return $permalink;
}
// check to see if the search was for a product variation SKU
$sku = get_search_query();
$args = array(
@monecchi
monecchi / ajax-team.twig
Created May 22, 2016 22:50
Craft - load more with AJAX
{% set teamIdsToExclude = ['and'] %}
{% for excerpt in craft.entries.section('team').limit(12) %}
{% set teamIdsToExclude = teamIdsToExclude|merge([excerpt.id]) %}
{% endfor %}
{# Convert the teamIdsToExclude array into a comma separated list #}
{% set teamIdsToExcludeString = teamIdsToExclude|join(', not ') %}
{% for excerpt in craft.entries.section('team').id(teamIdsToExcludeString) %}
<?php
// Example meta box field
$featured_categories->add_field( array(
'name' => __( 'Featured categories', 'iweb' ),
'id' => 'featured_categories',
'type' => 'pw_multiselect',
'options' => iweb_get_cmb_options_array_tax( 'category' ),
) );
/**
@monecchi
monecchi / add-to-cart.php
Created May 28, 2016 23:02 — forked from claudiosanches/add-to-cart.php
WooCommerce - Template add-to-cart.php with quantity and Ajax!
<?php
/**
* Custom Loop Add to Cart.
*
* Template with quantity and ajax.
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
global $product;
@monecchi
monecchi / gist:d4cd69e3806a4c1a2183293c8334576b
Created June 4, 2016 21:19 — forked from brunoalves/gist:7683005
Custom post type feed rewrite
<?php
function artigo_feed_rewrite( $wp_rewrite ) {
$feed_rules = array(
'feed/artigos' => 'index.php?post_type=listing&feed=rss2'
);
$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
// regerar regras de permalinks depois de ativar / implementar esse código
@monecchi
monecchi / wp_rss_feed.php
Created June 4, 2016 21:22 — forked from nickeforsberg/wp_rss_feed.php
Multiple Custom Post Types to Wordpress main RSS Feed.
// Add custom post types main RSS feed.
function wp_rss_feed( $query ) {
if ( $query->is_feed() )
$query->set( 'post_type', array( 'post', 'events', 'books' ) );
return $query;
}
add_filter( 'pre_get_posts', 'wp_rss_feed' );
@monecchi
monecchi / array-filter-iteration-limit-count.php
Last active June 7, 2016 02:54
Receive one array as input, filter values from it, and output as a new array.
// Receive one array as input, filter values from it, and output as another array.
// The function should loop through up to x iterations.
// Credits go to the author of the answer here: http://stackoverflow.com/a/7274450/1152876
*/
* Exit the foreach loop when count is reached.
* Use a foreach to iterate over the full array, and if the stated maximum count isn't reached, process the whole array.
* Otherwise, if the maximum iteration is reached, jump out of the loop.
*/
@monecchi
monecchi / array-easily-exit-foreach-loop-after-x-number-of-iterations.php
Last active June 7, 2016 03:22
Array easily exit the foreach loop after x number of iterations
// Credit goes to authors of answers here: http://stackoverflow.com/a/2865393/1152876
<?php
/*
* Example array, the values are grabbed from a custom field on WordPress.
* Works well if you need a solution that can accept any array.
*/
$array = get_post_meta( get_the_ID(), '_prefix_my_custom_field', true );
@monecchi
monecchi / wp-json-api-v2-custom-post-types.php
Last active June 7, 2016 17:42
Get Custom Post Types Using the WP API V2
<?php
/*
* Easy! Using the example of a custom post type called ‘the_event’,
* you’d just put the following into your functions.php file (or plugin).
* Credits go to author: https://bay-a.co.uk/wordpress-tips/wp-api-v2-tips/
*/
add_action( 'init', 'add_events_to_json_api', 30 );
function add_events_to_json_api(){