Skip to content

Instantly share code, notes, and snippets.

View odil-io's full-sized avatar
🪐

Odilio Witteveen odil-io

🪐
View GitHub Profile
@odil-io
odil-io / functions.php
Last active November 22, 2018 10:04
WP: Maintenance function to very easily and quickly put a website 'under construction'
<?
add_action('get_header', function(){
if ( !current_user_can( 'administrator' ) ) {
wp_die('<p>This website is under construction. This field accepts HTML markup so you can customize this <i>as</i> you would <b>see fit</b>.</p><');
}
});
?>
@odil-io
odil-io / javascript.js
Last active July 20, 2020 08:10
JavaScript: Do something after X amount of scrolling. Either pixels or percentage.
// Track scrolling percentage from top
function getScrollPercentage() {
var h = document.documentElement,
b = document.body,
st = 'scrollTop',
sh = 'scrollHeight';
return (h[st]||b[st]) / ((h[sh]||b[sh]) - h.clientHeight) * 100;
}
// Track scrolling pixels from top
function getScrollPixel() {
@odil-io
odil-io / functions.php
Last active January 22, 2020 14:43
WordPress: Add Admin User via functions.php
<?
// Source: https://www.wpbeginner.com/wp-tutorials/how-to-add-an-admin-user-in-wordpress-using-ftp/
/**
* Use this as a last resort to get back in your WordPress installation without using MySQL.
*/
add_action('init', function(){
$username = 'Username';
$password = 'Password';
@odil-io
odil-io / functions.php
Last active December 3, 2022 07:36
WordPress: Allow [user_role] to edit menu's only
// Source: https://wordpress.stackexchange.com/questions/4191/allow-editors-to-edit-menus
add_action( 'admin_menu', function(){
$role_object = get_role( 'editor' );
$role_object->add_cap( 'edit_theme_options' );
if (current_user_can('editor')):
add_menu_page( __('Menu\'s'), 'Menu', 'edit_pages', 'nav-menus.php', '', 'dashicons-menu', 50 );
remove_menu_page( 'themes.php' );
@odil-io
odil-io / template-part.php
Last active August 2, 2022 07:42
PHP: Get Google Company Reviews
<?php
$api_key = 'YOUR_PLACES_API_KEY';
$place_id = 'PLACE_ID';
$file = file_get_contents( 'https://maps.googleapis.com/maps/api/place/details/json?placeid=' . $place_id . '&key=' . $api_key );
$json = json_decode( $file );
if ( is_object( $json ) ) :
foreach ( $json->result->reviews as $review ) :
@odil-io
odil-io / file.php
Last active June 29, 2018 13:46
WordPress: List Menu items, does NOT work with WP Nav Plus
<?php $locations = get_nav_menu_locations(); ?>
<?php $menu = wp_get_nav_menu_object( $locations['theme_location'] ); ?>
<?php $menu_items = wp_get_nav_menu_items( $menu->term_id ); ?>
<ul class="list-group list-group-flush">
<?php foreach ( (array) $menu_items as $key => $menu_item ): ?>
<li class="list-group-item list-group-item-action">
<a href="<?= $menu_item->url; ?>">
<?= $menu_item->title; ?>
@odil-io
odil-io / file.php
Created May 17, 2018 11:45
WordPress: List Posts
<?php $args = array('post_type' => 'posttype', 'post__not_in' => array($post->ID), ); ?>
<?php $query = new wp_query( $args ); ?>
<?php if($query->have_posts()): ?>
<?php while( $query->have_posts() ) : ?>
<?php $query->the_post(); ?>
// All Post relative stuff here
<?php the_title(); ?>
<?php echo esc_url( get_post_permalink() ); ?>
@odil-io
odil-io / javascript.js
Last active February 28, 2018 10:06
JS: Vanilla Cross-Browser scroll position reporting
//https://stackoverflow.com/a/8028584
function getScrollPercent() {
var h = document.documentElement,
b = document.body,
st = 'scrollTop',
sh = 'scrollHeight';
return (h[st]||b[st]) / ((h[sh]||b[sh]) - h.clientHeight) * 100;
}
document.addEventListener('scroll', function(){
@odil-io
odil-io / readme.md
Last active January 23, 2018 13:01
Regex Youtube and Vimeo URL extraction
/**
 * Get Youtube video ID from URL
 *
 * @param string $url
 * @return mixed Youtube video ID or FALSE if not found
 */
function getYoutubeIdFromUrl($url) {
    $parts = parse_url($url);
    if(isset($parts['query'])){
@odil-io
odil-io / index.html
Created January 11, 2018 10:40
Google Maps: Direct link to Routing
https://www.google.com/maps/dir/Current+Location/Streetname%20housenumber+postalnumbers+postalletters+city