Skip to content

Instantly share code, notes, and snippets.

@EugeneCib
EugeneCib / image_size_src_or_default.php
Created December 9, 2015 13:05
WP function exapmle to make images use defaul image if no image exists or use exteranl tool for resizing images which are smaller then required sizes
<?php
function get_image_src_or_default($attachment_id, $image_size)
{
$image_size_info = false;
global $_wp_additional_image_sizes;
if(isset($_wp_additional_image_sizes[$image_size])) {
$image_size_info = $_wp_additional_image_sizes[$image_size];
}
$img = wp_get_attachment_image_src( $attachment_id, $image_size);
@EugeneCib
EugeneCib / timber_acf_file.php
Created October 29, 2015 13:14
Timber acf file wrapper function
<?php
class BasePost extends TimberPost
{
public function get_file($key) {
$id = $this->get_field($key);
$file_path = get_attached_file( $id );
$file = new SplFileInfo($file_path);
try {
@EugeneCib
EugeneCib / twig-wp-escape-extension.php
Created September 9, 2015 11:06
WordPress escape function extension for Twig. Escape functions as twig filters and functions.
<?php
class Twig_Extension_WP_basic_functions extends Twig_Extension
{
/**
* {@inheritdoc}
*/
public function getFunctions()
{
@EugeneCib
EugeneCib / Twig+PHP static page caching.php
Last active August 29, 2015 14:26
Twig + PHP static page caching example. Caching based on unique URL
<?php
// CONFIG
define("DEBUG", true);
define("CACHE", false);
define("CACHE_TIME", 86400);
define("CACHE_GZ_HANDLER", true);
define("BASE_DIR", __DIR__);
define("CACHE_DIR", BASE_DIR.'/cache/');
define("SITE_URL", 'http://' . $_SERVER['HTTP_HOST'] );
// . $_SERVER['QUERY_STRING']); // In this page we don't use query string
<script type="text/javascript">
(function() {
// IE detection
var device = {};
device.ie8 = /MSIE 8/i.test(navigator.userAgent);
device.ie9 = /MSIE 9/i.test(navigator.userAgent);
device.ie10 = /MSIE 10/i.test(navigator.userAgent);
device.ie11 = /rv:11.0/i.test(navigator.userAgent);
for(var i in device)
(function() {
// Test if is touch device
if(typeof window.addEventListener == 'function')
{
window.addEventListener('touchstart', function setHasTouch() {
document.getElementsByTagName('html')[0].className += ' is-touch-screen'
window.removeEventListener('touchstart', setHasTouch);
}, false);
}
@EugeneCib
EugeneCib / plugin-load-filter.php
Last active August 29, 2015 14:22
WP Selective plugin loading
<?php
/**
* WP mu-plugin to control plugin loading
*/
add_filter( 'option_active_plugins', function($val){
if(!is_admin())
{
$admin_only_plugins = [
'plugin1/plugin1.php',
<?php
// originally by Camden Ross. Thanks
/* bypass wordpress are you sure you want to logout screen when logging out of an already logged out account. */
function smart_logout() {
if (!is_user_logged_in()) {
$smart_redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '/';
wp_safe_redirect( $smart_redirect_to );
exit();
} else {
@EugeneCib
EugeneCib / get-client-ip.php
Last active August 29, 2015 14:15
PHP client IP
<?php
private function get_client_ip()
{
$ipaddress = '';
if ($_SERVER['HTTP_CLIENT_IP'])
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['HTTP_X_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if($_SERVER['HTTP_X_FORWARDED'])
@EugeneCib
EugeneCib / javascript_resources.md
Last active August 29, 2015 14:14 — forked from jookyboi/javascript_resources.md
Here are a set of libraries, plugins and guides which may be useful to your Javascript coding.

Libraries

  • jQuery - The de-facto library for the modern age. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers.
  • Backbone - Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
  • AngularJS - Conventions based MVC framework for HTML5 apps.
  • Underscore - Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects.
  • lawnchair - Key/value store adapter for indexdb, localStorage