Skip to content

Instantly share code, notes, and snippets.

View mklasen's full-sized avatar

Marinus Klasen mklasen

View GitHub Profile
@mklasen
mklasen / functions.php
Created January 18, 2017 11:54
WordPress: Wrap content output in a div
<?php
add_action('loop_start', function() {
// Only for archive
if (is_archive()) {
echo '<!--- Start loop wrap --><div class="wrap">';
}
});
add_action('loop_end', function() {
// Only for archive
if (is_archive()) {
@mklasen
mklasen / order.archive.php
Created January 24, 2017 15:45
Order posts in archive page based on child terms
add_filter( 'posts_clauses', function( $pieces, $obj ) {
global $wpdb;
$pieces['join'] .= " LEFT JOIN `$wpdb->term_relationships` AS trs ON ($wpdb->posts.ID = trs.object_id)";
$pieces['join'] .= " LEFT JOIN `$wpdb->term_taxonomy` AS tt ON (trs.term_taxonomy_id = tt.term_taxonomy_id)";
$pieces['join'] .= " LEFT JOIN `$wpdb->terms` AS t ON (tt.term_id = t.term_id)";
$pieces['join'] .= " LEFT JOIN `$wpdb->term_taxonomy` AS parent ON (parent.parent = trs.term_taxonomy_id)";
$pieces['join'] .= " LEFT JOIN `$wpdb->terms` AS parent_terms ON (parent.term_id = parent_terms.term_id)";
$pieces['where'] .= " AND (tt.taxonomy = '*YOUR_TAXONOMY*')";
@mklasen
mklasen / order.terms.php
Last active January 24, 2017 15:46
Influence order for parent terms
// Set program archive to order on post meta date/time
add_action('pre_get_posts', function($query) {
// Order on time
$query->set( 'meta_key', 'start_time' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
});
@mklasen
mklasen / remove.version.numbers.php
Created February 6, 2017 13:12
Remove version numbers from enqueues
// Used mainly for live reload with Chrome workspaces
add_filter( 'style_loader_src', 'mk_remove_version_numbers', 9999 );
add_filter( 'script_loader_src', 'mk_remove_version_numbers', 9999 );
function mk_remove_version_numbers( $src ) {
if ( strpos( $src, 'ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
@mklasen
mklasen / woocommerce.emails.php
Last active February 21, 2017 09:31
Use Woocommerce's email function and templates to send e-mails
<?php
function my_email_function($email, $header, $content) {
// We'll use Woocommerce e-mail function to send e-mail to users
ob_start();
// Get WC e-mail header
do_action( 'woocommerce_email_header', $header, $email );
// Content
echo $content;
@mklasen
mklasen / import.font.styl
Created February 24, 2017 10:42
Import font from Google API
@import url('https://fonts.googleapis.com/css?family=Lato:400,700,900')
@mklasen
mklasen / compare.dates.php
Created February 28, 2017 13:37
Compare between dates in PHP
$today = new Datetime();
$min = DateTime::createFromFormat('d/m/Y', '25/02/2017');
$max = DateTime::createFromFormat('d/m/Y', '03/03/2017');
if ($min->getTimestamp() <= $today->getTimestamp() && $max->getTimestamp() >= $today->getTimestamp()) {
//
}
@mklasen
mklasen / AWP.snippet.photoheight.html
Last active March 3, 2017 16:41
Keep those photos the same height
<style>
.mk-flex-photo-container {
display: flex;
width: 100%;
justify-content: center;
box-shadow: 0 0 black;
}
.mk-flex-photo-container a {
margin: 0 10px;
}
@mklasen
mklasen / gotoline.atom.keymap.cson
Created March 7, 2017 13:54
Atom custom keybinding for Go to Line
'atom-text-editor':
'cmd-shift-G': 'go-to-line:toggle'
@mklasen
mklasen / check.edit.post.php
Last active March 11, 2017 00:20
Show a message on front-end when someone is editing the post
<?php
function mk_check_edit_in_progress($content) {
if (!is_single())
return $content;
if ( !$lock = get_post_meta( get_the_ID(), '_edit_lock', true ) )
return false;
$lock = explode( ':', $lock );
$time = $lock[0];
$user_id = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );