Skip to content

Instantly share code, notes, and snippets.

@brandonkelly
brandonkelly / gist:1671737
Created January 24, 2012 18:27
Cross-compatible get_upload_preferences() for EE 2.0 thru 2.4
<?php
/**
* Get Upload Preferences (Cross-compatible between ExpressionEngine 2.0 and 2.4)
* @param int $group_id Member group ID specified when returning allowed upload directories only for that member group
* @param int $id Specific ID of upload destination to return
* @return array Result array of DB object, possibly merged with custom file upload settings (if on EE 2.4+)
*/
function get_upload_preferences($group_id = NULL, $id = NULL)
{
@brandonkelly
brandonkelly / gist:2723569
Created May 18, 2012 06:31
Remove <script> tags from HTML
// Remove any <script> tags
// Go backwards to factor in nested script tags, e.g. <script>document.write('<script></script>')</script>
$offset = strlen($html) - 9; // Minimum match length is 9 chars (<script/>)
for ($offset; $offset >= 0; $offset--)
{
$substr = substr($html, $offset);
if (preg_match('/^<script[^>]+?\/>|^<script(?:.|\s)*?\/script>/im', $substr, $match))
{
$html = substr($html, 0, $offset).substr($html, $offset+strlen($match[0]));
}
@brandonkelly
brandonkelly / wygwam-trim
Created June 22, 2012 19:26
Trim out whitespace when saving a Wygwam field
In system/expressionengine/third_party/wygwam/ft.wygwam.php, find this in the beginning of the save() function:
// Clear out if just whitespace
if (! $data || preg_match('/^\s*(<\w+>\s*(&nbsp;)*\s*<\/\w+>|<br \/>)?\s*$/s', $data))
{
return '';
}
And replace it with this:
@brandonkelly
brandonkelly / gist:3217046
Created July 31, 2012 13:30
Wygwam extension for adding custom toolbar buttons
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
require_once PATH_THIRD.'matrix/config.php';
/**
* Wygwam Custom Toolbar Button
*
* This is a sample extension that would add a plugin to Wygwam’s extraPlugins setting,
@brandonkelly
brandonkelly / assets2-simple-file-input.html
Created January 15, 2013 21:10
Here's how to create a simple file upload input for an Assets 2 field in SafeCracker
<input type="file" name="fieldname">
<input type="hidden" name="fieldname_filedir" value="1">
@brandonkelly
brandonkelly / gist:5643159
Created May 24, 2013 12:27
Upcoming BaseOptionsFieldType templating options
{% if entry.drinkAttributes.contains('alcoholic') %}
Alcoholic!
{% else %}
Not alcoholic
{% endif %}
<h2>Selected Attributes</h2>
<table>
{% for attr in entry.drinkAttributes %}
<tr>
@brandonkelly
brandonkelly / gist:5722967
Created June 6, 2013 16:42
New getNext() / getPrev() support coming to all elements in Craft 1.1
{% set criteria = {section: 'cocktails', order: 'title'} %}
{% set prev = entry.getPrev(criteria) %}
{% set next = entry.getNext(criteria) %}
{% if prev or next %}
<div class="pane">
{% if prev %}
<p>Previous: <a href="{{ prev.url }}">{{ prev.title }}</a></p>
{% endif %}
## Getting Started
* Introduction
* Installing & Updating
- Requirements
- Installing
- Updating
* Licensing
* CP Overview
* Packages & Plugins
@brandonkelly
brandonkelly / gist:6234999
Last active December 21, 2015 02:28
Turn a list of entries into JSON in Craft
{
entries: [
{% for entry in craft.entries.find() %}
{
title: "{{ entry.title | e('js') }}",
tags: [
{% for tag in entry.myTagsField %}
"{{ tag.name | e('js') }}" {% if not loop.last %},{% endif %}
@brandonkelly
brandonkelly / gist:6532664
Created September 12, 2013 03:07
How to protect some text from getting auto-escaped by Twig, without using the raw filter
$charset = craft()->templates->getTwig()->getCharset();
$value = new \Twig_Markup($value, $charset);