Skip to content

Instantly share code, notes, and snippets.

View goranseric's full-sized avatar

Goran Šerić goranseric

View GitHub Profile
@goranseric
goranseric / wp-singleton-namespace-example.php
Created December 3, 2015 11:28 — forked from szbl/wp-singleton-namespace-example.php
Quick Singleton class to be included in WordPress plugin (or theme functions.php file) that will create a simple Person post type and shows some methods of encapsulating functionality in a class as a "namespace," as well as applying filters to things, allowing other users to extend your code.
<?php
class Sizeable_Person
{
const POST_TYPE_SLUG = 'szbl-person';
public static $instance;
public static function init()
{
if ( is_null( self::$instance ) )
@goranseric
goranseric / wpengine-cdnurl.php
Created January 14, 2016 17:09 — forked from pospi/wpengine-cdnurl.php
Premodify URLs on WPEngine to directly point to your CDN. Avoids issues some link generators have with HTTP redirects.
<?php
function wpe_noRedirectUrl($srcURL)
{
if (class_exists('WpeCommon')) {
global $wpe_netdna_domains;
static $cdn_domain;
if (!isset($cdn_domain)) {
$wpe_common = new WpeCommon();
if ($wpe_common->is_cdn_enabled()) {
@goranseric
goranseric / get_domain_mapped_url.php
Created January 14, 2016 18:21 — forked from Coopeh/get_domain_mapped_url.php
Get Domain Mapped URL From BlogID
<?php
// Function to get a blog/site's domain mapped url.
//
// You need to call it with a blog ID
//
// Example:
// $custom_blog_id = '14';
// echo get_domain_mapped_url( $custom_blog_id );
//
@goranseric
goranseric / 0_reuse_code.js
Created January 18, 2016 08:53
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
@goranseric
goranseric / gist:1f4d80fc7ddb051360a5
Created January 18, 2016 10:58
Fetch all <img> tags from a php string using regex
/* the regex pattern will look for an image tag and create individual capture groups
 * for 'class'(1), 'src'(2), 'alt'(3), 'width'(4) and 'height'(5). The numbers within the brackets stand for the capture group number
 * respectively the key within the PHP array. The array key 0 will contain the whole <img> tag with all attributes (the full img link)
 * First we define the pattern, then we use the PHP function 'preg_match_all' fo find all occurences within the given string. */
$pattern = '/<img\s*(?:class\s*\=\s*[\'\"](.*?)[\'\"].*?\s*|src\s*\=\s*[\'\"](.*?)[\'\"].*?\s*|alt\s*\=\s*[\'\"](.*?)[\'\"].*?\s*|width\s*\=\s*[\'\"](.*?)[\'\"].*?\s*|height\s*\=\s*[\'\"](.*?)[\'\"].*?\s*)+.*?>/si';
preg_match_all($pattern, $item->fulltext, $matches);
/* as an example we iterate through the list of image sources (src) and build up a custom image link. We needed this in order to create
* proper a links from images in order to be able to use fancybox for opening images that were added in an article in Joomla */
foreach
@goranseric
goranseric / gist:e95532245c8561254fbd
Created January 18, 2016 10:59 — forked from joshuabaker/gist:313648
Get all images in a HTML string
<?php
/**
* Returns all img tags in a HTML string with the option to include img tag attributes
*
* @author Joshua Baker
*
* @example $post_images[0]->html = <img src="example.jpg">
* $post_images[0]->attr->width = 500
*
@goranseric
goranseric / gist:0a141f75c2b9300328fc
Created February 9, 2016 08:12
Add additional HTTP origins to allowed origins via WP Origin API
<?php
add_filter( 'allowed_http_origins', 'my_add_origins' );
function my_add_origins( $origins ) {
@goranseric
goranseric / class-virtualthemedpage-bc.php
Created February 14, 2016 18:45 — forked from brianoz/class-virtualthemedpage-bc.php
WordPress Virtual page with theme
<?php
/*
* Virtual Themed Page class
*
* This class implements virtual pages for a plugin.
*
* It is designed to be included then called for each part of the plugin
* that wants virtual pages.
*
* It supports multiple virtual pages and content generation functions.
@goranseric
goranseric / nav-menu-exporter-importer.php
Created February 17, 2016 06:17 — forked from hissy/nav-menu-exporter-importer.php
[WordPress Plugin] Nav Menu Exporter and Importer / Export and Import nav menus. Requires WordPress Importer plugin.
<?php
/*
Plugin Name: Nav Menu Exporter and Importer
Description: Export and Import nav menus. Requires WordPress Importer plugin
Author: hissy, megumi themes
Version: 0.1
Text Domain: nav-menu-exporter-importer
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
@goranseric
goranseric / wp-api-call-jquery.js
Created February 28, 2016 06:22
jQuery code to display recent posts from the WordPress API
jQuery(document).ready(function($) {
$.get( "wp-json/wp/v2/posts", function( data ) {
$.each( data, function( i, val ) {
$( "#app" ).append(
'<li>' +
'<h3>' +
val.title.rendered +
'</h3>' +
'<p>' +
val.excerpt.rendered +