Skip to content

Instantly share code, notes, and snippets.

View aliboy08's full-sized avatar

Alistair Ponce aliboy08

  • Philippines
View GitHub Profile
@aliboy08
aliboy08 / convert-array-to-key-value.js
Last active March 30, 2023 03:40
Convert array to key value pair
function convert_kv(arr, k, v){
var temp = {};
for( var i = 0; i < arr.length; i++ ) {
temp[arr[i][k]] = arr[i][v];
}
return temp;
}
@aliboy08
aliboy08 / get-array-item-value-by-key.js
Created March 30, 2023 03:18
Get array item value by key
Object.prototype.get_item_value = function(key, key_value, return_value_key ){
for( var i = 0; i < this.length; i++ ) {
if( this[i][key] == key_value ) {
return this[i][return_value_key];
}
}
return null;
}
@aliboy08
aliboy08 / load-more-ajax.php
Created March 28, 2023 04:37
wp load more posts ajax - compact
<?php
// Front-end
$args = [ ... ];
// query + loop here...
if( !ff_have_more_posts( $args, count($q) ) ) return;
echo '<button class="load-more-btn" onclick="load_more_posts(this)">Load more</button>';
?>
<script>
var load_more = {
@aliboy08
aliboy08 / cache-wp-ajax.php
Last active March 26, 2023 10:41
Cache Wordpress Ajax Sample
<?php
add_action( 'wp_ajax_ajax_cache_test', 'ajax_cache_test' );
add_action( 'wp_ajax_nopriv_ajax_cache_test', 'ajax_cache_test' );
function ajax_cache_test(){
$data = [];
$cache_file = ABSPATH . '/ajax-cache/'. $_POST['cache_key'] . '.json';
$time_start = microtime(true);
$cache_duration = 21600; // 6hrs
if( file_exists( $cache_file ) && (filemtime($cache_file) > (time() - $cache_duration)) ) {
$data['debug'][] = 'from cache';
@aliboy08
aliboy08 / google-reviews-badge.css
Created March 25, 2023 05:04
Google reviews badge
selector .google-reviews-badge {
display: inline-flex;
height: 45px;
border: 1px solid #E9ECEF;
border-radius: 27px;
background-color: #FFFFFF;
align-items: center;
padding: 5px 14px 5px 5px;
}
selector .google-reviews-badge:before {
@aliboy08
aliboy08 / dynamic-stars-fill
Created March 25, 2023 04:33
Dynamic stars + fill
$rating = 3.5;
echo '<span class="rating_stars">';
echo '<span class="fill" style="width:'. (floatval($rating)/5)*100 .'%;"><span></span></span>';
echo '<span class="stars"></span>';
echo '</span>';
// Styles
.rating_stars {
position: relative;
overflow: hidden;
@aliboy08
aliboy08 / custom-sticky-section.js
Last active March 23, 2023 01:48
custom sticky js with waypoints
function FF_Sticky(options){
if( window.outerWidth < 1024 ) return;
var _ = this;
_.options = options;
_.el = _.options.el;
_.el.addClass('ff-sticky-el');
_.el.wrap('<div class="ff_sticky_container">');
_.container = _.el.parent();
@aliboy08
aliboy08 / convert-to-webp.php
Last active March 12, 2023 06:56
PHP - convert to webp
function ff_convert_attachment_to_webp( $attachment_id ){
$guid = get_post_field('guid', $attachment_id);
if( !$guid ) return;
$file = str_replace( site_url() . '/', ABSPATH, $guid );
$convert_success = ff_convert_image_to_webp( $file, 100 );
if( $convert_success ) {
$file_webp = str_replace('.'. pathinfo($file, PATHINFO_EXTENSION), '', $file) . '.webp';
update_attached_file( $attachment_id, $file_webp );
}
}
@aliboy08
aliboy08 / scrolling-text
Last active March 11, 2023 02:13
scrolling text
// JS
function scrolling_text(){
$('.h-scroll-text').each(function(){
var id = this.id;
var el = $(this);
var spacing = $(this).data('spacing') ? $(this).data('spacing') : 300;
var speed = $(this).data('speed') ? $(this).data('speed') : 35;
var text_con = el.find('.elementor-widget-container');
@aliboy08
aliboy08 / woocommerce-phone-validation.js
Last active March 2, 2023 00:06
Woocommerce phone validation
function phone_validation(){
var phone_is_valid = false;
var phone = $('#billing_phone');
validate_phone( phone );
$( 'body' ).on( 'change', '#billing_phone', function(){
validate_phone( $(this) )
});
function validate_phone( el ){