Skip to content

Instantly share code, notes, and snippets.

View msaari's full-sized avatar

Mikko Saari msaari

View GitHub Profile
@msaari
msaari / gist:eeb04acc32d089d3299c89e42e961178
Last active September 2, 2016 05:21
Zippy Courses permissions for Relevanssi
add_filter('relevanssi_post_ok', 'zc_user_can_access_page', 10, 2);
// Return a Boolead of if a logged in user can access a page
function zc_user_can_access_page($post_ok, $page_id)
{
// If Zippy Courses isn't on this site, skip this check
if (!class_exists('Zippy')) {
return $post_ok;
}
$zippy = Zippy::instance();
// Get the student access middleware
@msaari
msaari / wp-amp-tutorial-menu.php
Last active February 2, 2017 07:12 — forked from johnregan3/wp-amp-tutorial-menu.php
Creating a Menu using amp-sidebar in WordPress
<?php
/**
* Render the Primary Nav Menu
*
* Uses amp-sidebar to handle
* slideout animation.
*
* Be sure to include the amp-sidebar component script.
* Also, rememeber that the amp-sidebar element must be
* a direct child of the <body>.
@msaari
msaari / relevanssi-pn.php
Last active February 26, 2017 04:10
Relevanssi part number checker
function rlv_is_a_pn($candidate) {
$array = str_split($candidate);
$has_letters = false;
$has_numbers = false;
foreach ($array as $letter) {
if (is_numeric($letter)) {
$has_numbers = true;
}
else {
$has_letters = true;
@msaari
msaari / block-keywords.php
Last active March 5, 2017 04:01
WordPress search keyword blocker
<?php
/* Add this function to theme functions.php */
add_filter( 'pre_get_posts', 'rlv_block_search' );
function rlv_block_search( $query ) {
if (!empty($query->query_vars['s'])) {
$blacklist = array( '大奖', 'q82' ); // add blacklist entries here; no need for whole words, use the smallest part you can
foreach( $blacklist as $term ) {
if( mb_stripos( $query->query_vars['s'], $term ) !== false ) exit();
@msaari
msaari / defer.php
Created March 6, 2017 08:44
Defer loading JS in WordPress
<?php
if (!(is_admin() )) {
function defer_parsing_of_js ( $url ) {
if ( FALSE === strpos( $url, '.js' ) ) return $url;
if ( strpos( $url, 'jquery.js' ) ) return $url;
// return "$url' defer ";
return "$url' defer onload='";
}
add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
@msaari
msaari / relevanssi-exact-title-match.php
Created March 7, 2017 04:08
Relevanssi filter to get exact title matches on top
<?php
add_filter('relevanssi_hits_filter', 'rlv_find_exact_title_match');
function rlv_find_exact_title_match($hits) {
$title_match = array();
$the_rest = array();
foreach ($hits[0] as $hit) {
if (mb_strtolower($hit->post_title) == $hits[1]) {
$title_match[] = $hit;
}
@msaari
msaari / relevanssi_the_permalink_media.php
Created March 9, 2017 09:55
the_permalink() replacement that uses attachment image URL instead of attachment page URL
@msaari
msaari / attachment-taxonomy.php
Created March 9, 2017 12:15
Media taxonomy that Relevanssi can index
<?php
// Add this code to theme functions.php
add_action( 'init', 'create_media_tax' );
function create_media_tax() {
register_taxonomy(
'media_tag',
'attachment',
@msaari
msaari / relevanssi-no-images.php
Created March 9, 2017 18:37
Relevanssi: do not index image attachments
<?php
// Add this to theme functions.php
add_filter('relevanssi_do_not_index', 'rlv_no_image_attachments', 10, 2);
function rlv_no_image_attachments($block, $post_id) {
$mime = get_post_mime_type($post_id);
if (substr($mime, 0, 5) == "image") $block = true;
return $block;
}
@msaari
msaari / relevanssi-post-type.php
Created March 15, 2017 03:51
Force wider post type parameter
<?php
/* Add this function to theme functions.php */
add_filter('relevanssi_modify_wp_query', 'rlv_force_post_types');
function rlv_force_post_types($query) {
$query->set('post_types', 'post,page,accommodation,room_type,things_to_do,tour');
return $query;
}