Skip to content

Instantly share code, notes, and snippets.

View marcosnakamine's full-sized avatar

Marcos Nakamine marcosnakamine

View GitHub Profile
@marcosnakamine
marcosnakamine / index.php
Last active March 10, 2017 13:20
WordPress - Get all child of parent page
<?php
$page_parent = get_page_by_path( 'page-parent-slug' ); // GET PAGE BY SLUG
$page_childs = get_pages( array(
'parent' => $page_parent->ID //GET CHILDS
) );
print_r( $page_childs );
/*
Array
(
@marcosnakamine
marcosnakamine / index.php
Created February 3, 2017 12:28
PHP - Google Recaptcha
<?php
if ( isset( $_POST['g-recaptcha-response'] ) ) {
// ENVIA INFORMAÇÕES POR POST PARA TESTAR O CAPTCHA
$result = file_get_contents( 'https://www.google.com/recaptcha/api/siteverify', false, stream_context_create( array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query( array(
@marcosnakamine
marcosnakamine / wetransfer.sh
Created February 3, 2017 11:07
Shell - Download with command line
#!/bin/bash
wget --user-agent Mozilla/4.0 'wetransferlink' -O file.zip
@marcosnakamine
marcosnakamine / woocommerce.php
Last active March 10, 2017 13:21
WooCommerce - Get all variations of product
<?php
$product = wc_get_product( get_the_ID() );
$variations = $product->get_available_variations();
print_r( $variations );
/*
Array
(
[0] => Array
(
[variation_id] => 109
@marcosnakamine
marcosnakamine / woocommerce.php
Last active October 25, 2020 17:21
WooCommerce - Get variable product attributes
<?php $attributes = $product->get_attributes() // GET ALL ATRIBUTES ?>
<?php foreach( $attributes as $key => $value ): ?>
<?php $attribute_name = preg_replace( '/pa_/', '', $key ) // GET ATTRIBUTE NAME ?>
<label>
<select name="attribute_pa_<?php echo $attribute_name ?>" id="attribute_pa_<?php echo $attribute_name ?>">
<option value=""><?php echo $attribute_name ?></option>
<?php $attribute_name = wc_get_product_terms( get_the_ID(), $key ) // GET ATTRIBUTE NAME ?>
<?php $attribute_slug = wc_get_product_terms( get_the_ID(), $key, array( 'fields' => 'slugs' ) ) // GET ATTRIBUTE SLUG ?>
<?php for ( $i=0; $i<count( $attribute_name ); $i++ ): // array_slice BECAUSE ARRAY INDEX IS NOT SEQUENCIAL ?>
<option value="<?php $slug = array_slice( $attribute_slug, $i, 1 ); echo $slug[0]; ?>"><?php $name = array_slice( $attribute_name, $i, 1 ); echo $name[0]; ?></option>
@marcosnakamine
marcosnakamine / woocommerce.php
Last active March 21, 2017 20:04
WooCommerce - Get all featured products
<?php
$featured_products = new WP_Query( array(
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
'posts_per_page' => -1
) );
@marcosnakamine
marcosnakamine / woocommerce.php
Last active March 21, 2017 20:03
WooCommerce - Get current category
<?php
$wp_query->get_queried_object();
/*
WP_Term Object
(
[term_id] => 10
[name] => Hoodies
[slug] => hoodies
[term_group] => 0
[term_taxonomy_id] => 10
@marcosnakamine
marcosnakamine / woocommerce.php
Last active March 21, 2017 20:04
WooCommerce - Basic form for variable product
<?php $product = wc_get_product( get_the_ID() ) ?>
<form class="txt" method="post" action="<?php the_permalink() ?>?add=<?php get_the_ID() ?>">
<span class="ref">Ref. <?php echo $product->get_sku() ?></span>
<div class="price">
<p><?php echo woocommerce_price( $product->get_price_including_tax() ) ?></p>
<p>
</div>
<div class="attributes">
@marcosnakamine
marcosnakamine / index.php
Last active March 21, 2017 20:03
WordPress - Add custom pagination with arrows
<?php
global $wp_query;
echo get_previous_posts_link('<span class="ion-chevron-left"></span>');
echo get_query_var('paged') ? get_query_var('paged') : 1 ?> / <?php echo $wp_query->max_num_pages;
echo get_next_posts_link('<span class="ion-chevron-right"></span>');
// Use http://ionicons.com/
@marcosnakamine
marcosnakamine / index.php
Last active March 21, 2017 20:04
WooCommerce - Add breadcrumb with custom delimiter
<?php
woocommerce_breadcrumb( array('delimiter' => ' >> ') );