Skip to content

Instantly share code, notes, and snippets.

@giventofly
giventofly / safari_fix_100vh.css
Created June 15, 2021 10:16
Safari 15 viewheight height 100vh fix
//from https://lukechannings.com/blog/2021-06-09-does-safari-15-fix-the-vh-bug/
height:calc(100vh - env(safe-area-inset-bottom));
@giventofly
giventofly / import json google sheets.js
Created May 20, 2021 12:45
import json google sheets
/**
* Retrieves all the rows in the active spreadsheet that contain data and logs the
* values for each row.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
@giventofly
giventofly / add_items_menu_wp.php
Created May 12, 2021 20:10
wordpress add items to menu programatically
<?php
//add items to menu
add_filter( 'wp_nav_menu_items', 'add_extra_item_to_nav_menu', 10, 2 );
function add_extra_item_to_nav_menu( $items, $args ) {
if (is_user_logged_in() && $args->menu == 1357) {
$items .= '<li><a href="'. get_permalink( get_option('woocommerce_myaccount_page_id') ) .'">My Account</a></li>';
}
elseif (!is_user_logged_in() && $args->menu == 1357) {
$items .= '<li><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Sign in / Register</a></li>';
@giventofly
giventofly / my_get_posts.php
Last active May 6, 2021 12:41
wordpress my get posts custom query
<?php
function get_my_posts($max=12,$paged=1,$with_category=[],$meta_fields=[],$ignore_cats=[],$ignore_posts=[]){
$args = [
//woocommerce product
//'post_type' => 'product',
'post_status' => 'publish',
//'ignore_sticky_posts' => 1,
'posts_per_page' => $max,
'paged' => $paged,
@giventofly
giventofly / timeago.js
Created April 21, 2021 00:15
javascript time ago
const timeAgo = (unixtime)=>{
//no weeks
const periods = ["second", "minute", "hour", "day", "month", "year", "decade"];
const lengths = ["60","60","24","31","12","10"];
//js returns in ms instead of s
const now = Math.round(Date.now() / 1000);
let difference = now - unixtime;
let j=0;
for(j = 0; difference >= lengths[j] && j < lengths.length-1; j++) {
difference = difference / lengths[j];
@giventofly
giventofly / xampp localhost wordpress permissions.md
Last active March 9, 2021 12:15
xampp localhost wordpress permissions

from linux mint forum.

I solved this type of problems by

* giving ownership of /opt/lampp/htdocs folder and files to current user and group
* running xampp's apache httpd process under currrent user account
@giventofly
giventofly / .htaccess
Created February 9, 2021 12:30
wordpress htaccess
# Disable directory browsing
Options All -Indexes
# Setup browser caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
@giventofly
giventofly / string_csv_to_array.php
Created January 17, 2021 23:38
string csv to array
<?php
/**
* Receives CSV string and returns as an array.
*/
function csv_to_array($csv, $delimiter=',', $header_line=true) {
// CSV from external sources may have Unix or DOS line endings. str_getcsv()
// requires that the "delimiter" be one character only, so we don't want
// to pass the DOS line ending \r\n to that function. So first we ensure
// that we have Unix line endings only.
$csv = str_replace("\r\n", "\n", $csv);
@giventofly
giventofly / click modal outside click.js
Last active November 30, 2020 21:39
javascript click modal on outside click
//asume all modals have class modal and are closed by removing the class open.
//close open modal on outside click
const clickOutSideModal = (e)=>{
let current = e.target;
let insideModal = false;
while(current.parentNode && !insideModal && current.parentNode.tagName != 'BODY'){
if(current.parentNode.classList.contains('modal')){
insideModal = true;
}
current = current.parentNode;
@giventofly
giventofly / get_link_categories_from_post.php
Last active October 12, 2020 16:09
wordpress get links categories post
<?php
$categories = get_the_category();
$tags = "";
foreach ($categories as $key => $cat) {
$category_link = get_category_link($cat->cat_ID);
$tags .= '<a href="'.esc_url( $category_link ).'" title="'.esc_attr($cat->name).'">'.$cat->name.'</a>';
if(isset($categories[$key+1])){ $tags .= ", "; }
}