Skip to content

Instantly share code, notes, and snippets.

@Farmatique
Farmatique / gist:5a4e6eaa134e15c31a9feb347fc082ab
Created November 28, 2019 19:41
Refresh page onload only once
(function()
{
if( window.localStorage )
{
if( !localStorage.getItem('firstLoad') )
{
localStorage['firstLoad'] = true;
window.location.reload();
}
else
@Farmatique
Farmatique / gist:d9e77afd8e4d63c95ceb0197fe2d3d4a
Created November 26, 2019 11:38
Get query strings from url
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
// from url like sales/?search=roadmap+mcp+test
// using getUrlParameter('search') it return "roadmap mcp test"
@Farmatique
Farmatique / gist:d3c2c525602068a4567b339ba9451ddb
Created November 19, 2019 10:25
Check if touch slide up or down
var ts;
$(document).bind('touchstart', function(e) {
ts = e.originalEvent.touches[0].clientY;
});
$(document).bind('touchmove', function(e) {
var te = e.originalEvent.changedTouches[0].clientY;
if (ts > te) {
console.log('down');
} else {
@Farmatique
Farmatique / gist:8f3a8f1f4f67ebd117ef3d2eaa2bc412
Created November 18, 2019 17:54
Scroll direction detect based on mousewheel
$(window).bind('mousewheel', function(event) {
if (event.originalEvent.wheelDelta >= 0) {
console.log('Scroll up');
}
else {
console.log('Scroll down');
}
});
@Farmatique
Farmatique / gist:fe2a405c6a102f9ce2b7acf4e3a9d856
Created November 4, 2019 12:16
Override Opengraph image Wordpress YOAST SEO
$default_opengraph = 'https://www.rafaeldejongh.com/wp-content/uploads/2017/08/RafaelDeJongh-Web-Developer-3D-Artist.jpg';
function add_default_opengraph($object){global $default_opengraph; $object->add_image($default_opengraph);}
add_action('wpseo_add_opengraph_images','add_default_opengraph');
function default_opengraph(){global $default_opengraph; return $default_opengraph;}
add_filter('wpseo_twitter_image','default_opengraph');
@Farmatique
Farmatique / gist:64e7ff3eaee2134b346b576c2ca936eb
Created October 11, 2019 09:17
Wordpress create query to get posts with specifica category name
<?php $query = new WP_Query( array( 'category_name' => 'blog' ) ); ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php get_template_part('partials/content-category-blog'); ?>
<?php
endwhile;
wp_reset_query();
?>
@Farmatique
Farmatique / gist:586d3ab4705e1627c2ed3b87707f2a02
Created September 27, 2019 17:13
Get User ip php wordpress
function GetIP(){
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key)
{
if (array_key_exists($key, $_SERVER) === true)
{
foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip)
{
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false)
{
return $ip;
@Farmatique
Farmatique / gist:b8ad801a8ab313cae5335cf87449e7d2
Created September 13, 2019 19:06
php sort array by name
$api_response_sorted; // initial array
$sortArray = array();
foreach($api_response_sorted as $person){
foreach($person as $key=>$value){
if(!isset($sortArray[$key])){
$sortArray[$key] = array();
}
$sortArray[$key][] = $value;
@Farmatique
Farmatique / gist:138974675fcb24c6ea7ecb86bfd87fbc
Created September 11, 2019 13:40
Iframe send data to parent using postMessage
// source: https://stackoverflow.com/questions/9162933/make-iframe-height-dynamic-based-on-content-inside-jquery-javascript
//parent:
window.addEventListener('message', function(e) {
var $iframe = jQuery("#myIframe");
var eventName = e.data[0];
var data = e.data[1];
switch(eventName) {
case 'setHeight':
$iframe.height(data);
@Farmatique
Farmatique / gist:eda931a2c2cb05b2cc3814b2c0de649a
Created September 6, 2019 18:12
REmove query string in url (remove after ?q)
$(document).ready(function(){
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, clean_uri);
}
});