Skip to content

Instantly share code, notes, and snippets.

@musen
musen / gist:00ba40aa5edf4fc151c2
Created February 2, 2015 08:27
Wordpress pre_get_posts action hook. Comes in handy when you would like to modify query variables before running archive queries.
//order products alphabetically and display all of them
function prefix_products_archive_modify( $query ) {
if ( is_post_type_archive('product') &&
! empty( $query->query['post_type']) &&
$query->query['post_type'] == 'product') {
$query->set( 'orderby', 'title' );
$query->set('order', 'ASC');
$query->set( 'posts_per_page', -1 );
}
@musen
musen / extract_shortcode.php
Last active December 22, 2015 11:51
Extracts a shortcode tag from a post content and returns the content with target shortcode tag stripped out.
<?php
/**
* Based on:
* https://codex.wordpress.org/Function_Reference/get_shortcode_regex
*/
function extract_shortcode($content, $tag) {
$shortcode_match = false;
@musen
musen / Text Image mask with Rotating background images.markdown
Created March 29, 2016 12:55
Text Image mask with Rotating background images
@musen
musen / generate_toc.php
Last active May 23, 2016 15:39
Generate table of contents from html.
function _generate_toc( $content , $headings, $first_heading) {
preg_match_all( '/<h([' . $headings . '])(.*)>([^<]+)<\/h[' . $headings . ']>/i', $content, $matches, PREG_SET_ORDER );
global $anchors;
$anchors = array();
$toc = '<ul class="nav">'."\n";
$i = 0;
//add the fist anchor link
@musen
musen / generate_toc.php
Last active May 23, 2016 15:40
Generate table of contents from html.
function _generate_toc( $content , $headings, $first_heading) {
preg_match_all( '/<h([' . $headings . '])(.*)>([^<]+)<\/h[' . $headings . ']>/i', $content, $matches, PREG_SET_ORDER );
global $anchors;
$anchors = array();
$toc = '<ul class="nav">'."\n";
$i = 0;
//add the fist anchor link
@musen
musen / get_toc.php
Last active May 24, 2016 07:46
Extracts and returns table of contents (toc) and bookmarked content from any html.
<?php
function create_toc( $content ) {
preg_match_all( '/<h([1-6])(.*)>([^<]+)<\/h[1-6]>/i', $content, $matches, PREG_SET_ORDER );
global $anchors;
$anchors = array();
$toc = '<ol class="toc">'."\n";
$i = 0;
@musen
musen / 0_reuse_code.js
Created September 26, 2016 06:13
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@musen
musen / prng.js
Created May 17, 2018 15:27 — forked from blixt/prng.js
A very simple, seedable JavaScript PRNG.
/**
* Creates a pseudo-random value generator. The seed must be an integer.
*
* Uses an optimized version of the Park-Miller PRNG.
* http://www.firstpr.com.au/dsp/rand31/
*/
function Random(seed) {
this._seed = seed % 2147483647;
if (this._seed <= 0) this._seed += 2147483646;
}
@musen
musen / medapay-nodejs-snippet.js
Last active October 15, 2020 11:16
Code snippet for MedaPay node library.
const IS_SANDBOX = true;
//Require the MedaPay module and initialize with provided token
const MedaPay = require('medapay').init({
bearerToken: 'eyJ......'
}, IS_SANDBOX);
...
//Create bill
MedaPay.create(SAMPLE_BILL)
.then(createBillResponse => console.log(createBillResponse.billReferenceNumber))
.catch(error => console.error(error));