Skip to content

Instantly share code, notes, and snippets.

View adamcrampton's full-sized avatar
💪
Crushing it

Adam Crampton adamcrampton

💪
Crushing it
  • Sydney, Australia
View GitHub Profile
@adamcrampton
adamcrampton / wpAdminWidgetImageUploader.js
Last active May 3, 2019 01:10
WordPress - JS for uploading images to widget
/**
* Steps:
* 1. Pop this into your theme's assets/js directory, set the filename as widgets.js (or add JS to existing widgets.js)
* 2. See the other gist for widget PHP instructions if you haven't already
* 3. Profit++
*/
// ============================================
// Custom JavaScript used in Widgets (WP Admin)
// ============================================
// Image uploader
@adamcrampton
adamcrampton / acf_fields_post_table.md
Last active July 2, 2019 22:49 — forked from PurpleBooth/README-Template.md
ACF field values for WordPress wp_posts table

Manual Insertion of ACF Fields Into The WordPress Database

To add ACF fields to the WordPress database (e.g. when using a seeder), you need to add each ACF field as a row into the wp_posts table using the post_type value of acf-field.

This guide assumes that you have defined the fields with PHP and are seeding posts directly into the WordPress database.

Note: You can also create ACF Groups in a similar way using acf-field-group post_type. I will cover this in a separate gist.

Getting Started

Note: The only practical use case for this is a database seeder (e.g. for demo data) - any other interaction with ACF and the WordPress database should really be done via the plugin itself.

@adamcrampton
adamcrampton / getImageMimeType.php
Last active July 11, 2019 23:23
Returns image Mime type
<?php
/**
* @param string $imageUrl
* @return string|bool
*/
public function getImageMimeType($imageUrl) {
$mimeTypes = [
IMAGETYPE_GIF => "image/gif",
IMAGETYPE_JPEG => "image/jpg",
@adamcrampton
adamcrampton / set_laravel_permissions.sh
Last active August 14, 2019 00:18
Simple bash script for setting ownership and permissions for a Laravel project
echo Enter project directory name
read dirname
echo Setting permissions for $dirname...
sudo chown -R $USER:www-data /var/www/$dirname/storage
sudo chown -R $USER:www-data /var/www/$dirname/bootstrap/cache
chmod -R 775 /var/www/$dirname/storage
chmod -R 775 /var/www/$dirname/bootstrap/cache
@adamcrampton
adamcrampton / setHashKeys.php
Last active August 22, 2019 03:13
Loops through a nested array and adds a hash key for each data set
<?php
/**
* Generates a hash for each nested array in the data set.
* Sets itself as the parent key - useful for comparisons.
*
* @param array $dataArray
* @param array $preserveFields Fields to return but not include in hash
* @return array $updatedArray
*/
private function setHashKeys(array $dataArray, array $preserveFields = [])
@adamcrampton
adamcrampton / jQueryBuildOption.js
Created August 29, 2019 02:17
Dynamically build jQuery select options
function buildOption(value, text, selected) {
return $("<option/>", {
value: value,
text: text,
selected: selected
});
}
// Usage:
// $('#myselect').append(buildOption(key, value, selected));
@adamcrampton
adamcrampton / toTitleCase.js
Created August 29, 2019 02:19
Convert JavsScript string to Title Case
function toTitleCase(string) {
return string.replace(
/\w\S*/g,
function(text) {
return text.charAt(0).toUpperCase() + text.substr(1).toLowerCase();
}
);
}
@adamcrampton
adamcrampton / setElementValueInLocalStorage.js
Last active August 29, 2019 02:27
Sets a jQuery input element value in local storage
// Set field value for element if it has a value
function setFieldValueInStorage(elementId, storageKey) {
if (window.localStorage) {
let storage = window.localStorage;
// If no value, remove item. Otherwise store.
if ($('#' + elementId).val()) {
storage.setItem(storageKey, $('#' + elementId).val());
} else {
storage.removeItem(storageKey);
@adamcrampton
adamcrampton / setElementValueFromLocalStorage.js
Created August 29, 2019 02:31
Set a jQuery element's value using a specified key in local storage
// Fetch storage item if it exists and set element value
function updateFieldValueFromStorage(elementId, storageKey) {
let storage = window.localStorage;
if (storage.getItem(storageKey)) {
($('#' + elementId).val(storage.getItem(storageKey)));
}
}
@adamcrampton
adamcrampton / validationErrorMacro.php
Last active August 29, 2019 06:42
Laravel macro for returning validation errors to front end using Form class
<?php
/**
* Bootstrap macros in Service Provider.
*
* @return void
*/
public function boot()
{
// Build form error macro to easily set inline error labels.
Form::macro('errorMessage', function($field) {