Skip to content

Instantly share code, notes, and snippets.

View huanyichuang's full-sized avatar

Huanyi Chuang (Eric) huanyichuang

View GitHub Profile
@huanyichuang
huanyichuang / language-detector.php
Created July 31, 2020 05:02
Because the pages in the new version doesn't need to be edited (hard-coded), it seems better to use PO/MO directly for the multi-lingual website.
<?php
/**
* Language detector
* This global variable is used to detect the language that the user's browser is using.
* Should be passed to step to get the specified language's MO file.
*/
function applemint_locale( $locale ) {
//Create a global variable so that other functions and hooks can use the variable to determine the language.
global $am_lang;
<?php
/**
* 使用方式:在頁面中建立 "退貨政策" 後,從 [自訂] 中選取退貨政策的頁面,便會出現在結帳頁中。
* 在 WooCommerce 結帳頁顯示內容。
*/
if ( ! function_exists( 'hyc_refund' ) ) {
function hyc_refund() {
$pid = get_theme_mod( 'hyc_refund_policy' );
if ( $pid ) {
?>
<?php
function reservation_register_meta() {
global $post;
if ( 'page-shopreservations.php' != get_post_meta( $post->ID, '_wp_page_template', true ) ) {
//忘記 $post 是一個物件 XD 要再把 ID 指定起來
return;
} else {
add_meta_box(
'reservation-form-id',
'MW WP Form ID',
@huanyichuang
huanyichuang / lazyload.php
Last active May 11, 2020 02:25
Prepare WordPress post for Image LazyLoading by replacing the src Attribute
<?php
/**
* Lozad
* Attribute replacement refernce from: https://gist.github.com/ahmadassaf/9332846
*/
function faster_replace_img_src( $content ){
if ( function_exists( 'is_amp_endpoint' ) && ! is_amp_endpoint() ) {
$html = preg_replace_callback( '#(<img\s[^>]*src)="([^"]+)"#' , 'callback_img' , $content );
$html = preg_replace( '/(<img\s[^>]*)class="(.*)"/', '$1 class="lozad $2"' , $html );
$html = preg_replace( '/(<img\s[^>]*)srcset="(.*)"/', '$1 data-srcset="$2"' , $html );

#lidemy API Challenge

@huanyichuang
huanyichuang / infinite-scroll.php
Last active March 17, 2019 03:51
Infinite Scroll 筆記
<?php
<?php
function mxp_ajax_get_next_page_data() {
$max_num_pages = $_POST['max_num_pages'];
$current_page = $_POST['current_page'];
$found_posts = $_POST['found_posts'];
$post_type = $_POST['post_type'];
$taxonomy = $_POST['taxonomy'];
$term = $_POST['term'];
$nonce = $_POST['nonce'];
#!/bin/bash
# Program:
# Check if mysql is down, if down, turn it on.
# History:
# 2019/02/25 First Release by Eric
# 2019/03/05 Echo date time in logs for better readability.
UP=$(service mysql status|grep 'running' | wc -l);
now=$(date);
if [ "$UP" -ne 1 ];
@huanyichuang
huanyichuang / add_image_sizes.php
Created February 22, 2019 06:09
WordPress add new image size
<?php
@huanyichuang
huanyichuang / cpt.php
Last active February 21, 2019 14:00
Register Custom Post Type
<?php
$args = array(
'template' => array(
array( 'core/heading', array(
'placeholder' => 'Payment Options',
'content' => 'Payment Options',
) ),
array( 'acf/custom-block' ),
array( 'core/heading', array(
@huanyichuang
huanyichuang / cf7-tag.php
Last active December 21, 2018 00:53
According to the original template file, I tried to create another tag that can integrate the TW address field in Contact Form 7.
<?php
add_action( 'wpcf7_init', 'wpcf7_add_address_tag' );
function wpcf7_add_address_tag () {
wp_enqueue_script( 'address-pick', get_template_directory_uri() . '/js/tw-city-selector/dist/tw-city-selector.min.js', array( 'jquery' ), true ) ;
//Get the js file from https://github.com/dennykuo/tw-city-selector
wpcf7_add_form_tag( array( 'address', 'address*' ),
//To-do: 還沒處理必填欄位的功能
'wpcf7_add_address_tag_handler', array( 'name-attr' => true ) );
}