Skip to content

Instantly share code, notes, and snippets.

@Giannisduke
Giannisduke / theme-widget-categories.php
Created June 28, 2019 23:15 — forked from ruturajpatki/theme-widget-categories.php
Custom Widget to list WordPress categories along with the Post count. Requires styling with CSS to blend it well with theme.
<?php
// Adds widget: Categories
class RutuCategories_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'rutu_categories_widget',
esc_html__( 'Rutu Categories', 'mfl' ),
array( 'description' => esc_html__( 'Custom Categories widget.', 'mfl' ), ) // Args
@Giannisduke
Giannisduke / wordpress-functions.php
Created June 28, 2019 20:55 — forked from deardorffdev/wordpress-functions.php
Useful Wordpress Functions 1
<!-- What is Functions File in WordPress? -->
<!-- Functions file commonly known as functions.php file is a WordPress theme file.
It comes with all free and premium WordPress themes.
The purpose of this file is to allow theme developers to define theme features and functions. This file acts just like a WordPress plugin and can be used to add your own custom code snippets in WordPress.
You would find many of these code snippets on websites like https://deardorffassociatesweb.wordpress.com/ with instructions telling you to add this code in your theme’s functions.php file or a site-specific WordPress plugin.
Now you may be thinking what’s the difference between a site-specific WordPress plugin and functions.php file? Which one is better?
@Giannisduke
Giannisduke / woocommerce.php
Created June 3, 2019 14:21 — forked from marcosnakamine/woocommerce.php
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>
@Giannisduke
Giannisduke / mycode_import_gender_attribute-1.php
Created June 2, 2019 18:30 — forked from EricBusch/mycode_import_gender_attribute-1.php
Import Attribute from Multiple Fields [dfrpswc][datafeedr]
<?php
/**
* Add the product's gender as an attribute.
*
* The attribute "Gender" with a slug of "gender" must already exist here: WordPress Admin Area > Products > Attributes
*
* @param array|string $value The current value of the $attribute for this $post.
* @param string $attribute The slug of the attribute. Examples: pa_color or pa_shoe-size
* @param array $post An array of post data including ID, post_title, post_status, etc...
@Giannisduke
Giannisduke / import.php
Created June 2, 2019 18:30
Import script for Woocommerce and variable products.
<?php require __DIR__ . '/../htdocs/wp-load.php';
// Decode products.json into an array of products.
$products = json_decode( file_get_contents( __DIR__ . '/products.json' ), true );
// Now we want to loop through and create the top-level product.
foreach ( $products as $product ) {
/**
* A product is just a Wordpress post with a different 'post_type',
<?php
// Change the add to cart text on product archives
add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' ); // < 2.1
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 +
function woo_custom_cart_button_text() {
return __( 'My Button Text', 'woocommerce' );
}
/**
* Restore CSV upload functionality for WordPress 4.9.9 and up
*/
add_filter('wp_check_filetype_and_ext', function($values, $file, $filename, $mimes) {
if ( extension_loaded( 'fileinfo' ) ) {
// with the php-extension, a CSV file is issues type text/plain so we fix that back to
// text/csv by trusting the file extension.
$finfo = finfo_open( FILEINFO_MIME_TYPE );
$real_mime = finfo_file( $finfo, $file );
finfo_close( $finfo );
// Disable scrollmagic controller on mobile
controller.scrollPos(function () {
if(window.innerWidth >= 576){
return window.pageYOffset;
} else {
return 0;
}
});
@Giannisduke
Giannisduke / ajax.js
Created April 23, 2019 07:47 — forked from anthonydelgado/ajax.js
load page with ajax
window.onload = function(){
var aboutdata = null;
$.get( "/about-us/?ajax=true", function( data ) {
aboutdata = data;
$( "#menu-item-23" ).click(function() {
event.preventDefault();
$( "body" ).html(aboutdata);
});
@Giannisduke
Giannisduke / isotope.php
Created April 15, 2019 15:08 — forked from wpsmith/isotope.php
PHP: Properly Load Isotope
<?php
add_action( 'wp_enqueue_scripts', 'wps_load_scripts' );
/**
* Enqueue Isotope
* For commercially developed child themes, you must obtain a license
* from isotope.metafizzy.co for approx. $25.
*
* @link http://isotope.metafizzy.co/
* @link https://github.com/desandro/isotope
* @link http://codex.wordpress.org/Function_Reference/wp_enqueue_script