Skip to content

Instantly share code, notes, and snippets.

@techslides
techslides / cards-shuffle.go
Last active January 4, 2023 05:34
Shuffle Array of Objects with Go Lang
package hello
import (
"net/http"
"github.com/go-martini/martini"
"github.com/martini-contrib/cors"
"github.com/martini-contrib/render"
"math/rand"
)
@techslides
techslides / form3.js
Created October 16, 2014 21:01
HTML Form POST Submit via Ajax to Node.js
// Load the http module to create an http server.
var http = require('http');
// Create a function to handle every HTTP request
function handler(req, res){
var form = '';
if(req.method == "GET"){
@techslides
techslides / template-eq-check.go
Last active November 2, 2022 19:26
Equality checking in Go Lang Templates
{{if eq .authuser.Id .user.Id }}<button type="button" class="delete_user" rel="{{.user.Id}}">Delete My Account</button>{{end}}
@techslides
techslides / pinterest-pin.js
Created April 22, 2015 18:14
Pinterest Pin Image Script
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
pageSettings: {
loadImages: true,
loadPlugins: true,
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
}
});
@techslides
techslides / golang-prettyurl.go
Created February 13, 2014 16:38
Generating Pretty Urls for SEO in GoLang using string and regex packages
type Post struct {
// db tag lets you specify the column name if it differs from the struct field
Id int64 `db:"post_id"`
Created int64
Title string `form:"Title" binding:"required"`
Body string `form:"Body"`
UserId int64 `form:"UserId"`
Url string
}
@techslides
techslides / github-gist-api.js
Last active January 1, 2022 23:03
GitHub API to make Gists with Ajax
/*
Assuming jQuery Ajax instead of vanilla XHR
*/
//Get Github Authorization Token with proper scope, print to console
$.ajax({
url: 'https://api.github.com/authorizations',
type: 'POST',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa("USERNAME:PASSWORD"));
@techslides
techslides / wp-ajax-upload.php
Last active November 26, 2020 14:15
Upload to WordPress with Ajax and FormData
<input type="file" name="file" id="file">
<input type="submit" id="submit" name="Upload" onclick="upload();return false;">
<script type="text/javascript">
function upload(){
var formData = new FormData();
formData.append("action", "upload-attachment");
var fileInputElement = document.getElementById("file");
formData.append("async-upload", fileInputElement.files[0]);
@techslides
techslides / debug-wordpress-queries.php
Created January 15, 2016 20:02
Debug WP Queries. Include this file in functions.php and then add ?debug=sql to any WP URL
<?php
if ( !defined('SAVEQUERIES') && isset($_GET['debug']) && $_GET['debug'] == 'sql' )
define('SAVEQUERIES', true);
if ( !function_exists('dump') ) :
/**
* dump()
*
@techslides
techslides / save.php
Created October 20, 2014 19:15
Save Ajax POST data as a file with PHP
<?php
//error_reporting(E_ALL);
//var_dump($_SERVER);
$post_data = $_POST['data'];
if (!empty($post_data)) {
$dir = 'YOUR-SERVER-DIRECTORY/files';
$file = uniqid().getmypid();
$filename = $dir.$file.'.txt';
$handle = fopen($filename, "w");
fwrite($handle, $post_data);
@techslides
techslides / multiple-insert-on-duplicate-update.php
Created July 12, 2016 14:36
INSERT on duplicate update PHP function for WordPress multiple or batch operation
<?php
//based on Wordpress Multiple Insert https://github.com/mirzazeyrek/wordpress-multiple-insert
function wp_insert_update_rows($row_arrays = array(), $wp_table_name) {
global $wpdb;
$wp_table_name = esc_sql($wp_table_name);
$update = "";
$query = "";
$query_columns = "";
$query. = "INSERT INTO {$wp_table_name} (";