Skip to content

Instantly share code, notes, and snippets.

View soderlind's full-sized avatar

Per Søderlind soderlind

View GitHub Profile
@soderlind
soderlind / wp-gears-heartbeat.php
Last active November 11, 2016 23:32
Testing WP Gears and the Heartbeat API, WP Gears is at https://github.com/10up/WP-Gears (NOTE: this plugin is run continuously, triggered by 'plugins_loaded', you should use an event triggered by the user (onclick, onhover etc))
<?php
/*
Plugin Name: WP Gears & Heartbeat
Version: 0.0.6
Description: Testing WP Gears and the Heartbeat API
Author: Per Soderlind
Author URI: https://soderlind.no
Plugin URI: https://gist.github.com/soderlind/94629e4ae933407d823c
License: GPL
*/
@soderlind
soderlind / mb_similar_text.php
Last active June 27, 2023 03:28
Multibyte Similar Text for PHP, mb_similar_text()
<?php
/*
* solves the problem at http://stackoverflow.com/questions/31002690/how-to-use-similar-text-php-code-in-arabic, not that I know Arabic, but Norwegian also has multibyte charachters: æøåÆØÅ
*/
//from http://www.phperz.com/article/14/1029/31806.html
function mb_split_str($str) {
preg_match_all("/./u", $str, $arr);
return $arr[0];
}
// dropzoneWordpressForm is the configuration for the element that has an id attribute
// with the value dropzone-wordpress-form (or dropzoneWordpressForm)
Dropzone.options.dropzoneWordpressForm = {
//acceptedFiles: "image/*", // all image mime types
acceptedFiles: ".jpg", // only .jpg files
maxFiles: 1,
uploadMultiple: false,
maxFilesize: 5, // 5 MB
//addRemoveLinks: true,
//dictRemoveFile: 'X (remove)',
@soderlind
soderlind / dropzonejs-wp-rest-api.js
Last active March 27, 2024 19:38
DropzoneJS & WordPress REST API
/*
Uploading images is a two step process (from https://github.com/WP-API/WP-API/issues/1768#issuecomment-160540932):
POST the data to /wp/v2/media - this can either be as the request body, or in multipart format. This will upload the file, and give you a 201 Created response with a Location header. This header points to the post object for the attachment that has just been created.
PUT the post data to the endpoint returned in the Location header (which will look something like /wp/v2/media/{id}).
I do step 2 (PUT), if POST is a success, in myDropzone.on("success", function(file, response){}
*/
// dropzoneWordpressRestApiForm is the configuration for the element that has an id attribute
@soderlind
soderlind / dropzonejs-wp-rest-api-custom-endpoint.js
Created February 1, 2016 19:33
DropzoneJS & WordPress REST API with Custom Endpoint
// dropzoneWordpressRestApiForm is the configuration for the element that has an id attribute
// with the value dropzone-wordpress-rest-api-form (or dropzoneWordpressRestApiForm)
Dropzone.options.dropzoneWordpressRestApiForm = {
//acceptedFiles: "image/*", // all image mime types
acceptedFiles: ".jpg", // only .jpg files
maxFiles: 1,
uploadMultiple: false,
maxFilesize: 5, // 5 MB
init: function() {
console.group('dropzonejs-wp-rest-api:');
@soderlind
soderlind / wp.media.view.Modal.prototype.on.open.js
Created February 22, 2016 16:57
wp.media.view.Modal.prototype.on('open', function() {});
jQuery(document).ready(function($){
if (wp.media) {
wp.media.view.Modal.prototype.on('open', function() {
console.log('media modal open');
});
}
});
@soderlind
soderlind / provision-post.sh
Created February 25, 2016 10:45
Adding Gearman to VVV
# #!/bin/bash
# #
# # provision-post.sh (save the file in VVV/provision)
# #
# # Adding Gearman to VVV
# #
# # This file is specified in Vagrantfile and is loaded by Vagrant _after_ the primary
# # provisioning script whenever the commands `vagrant up`, `vagrant provision`,
# # or `vagrant reload` are used.
@soderlind
soderlind / jquery-ui-fix-divi.php
Last active March 1, 2016 22:33
Dequeue old script and style loaded by the Divi theme
<?php
function load_divi_fix() {
global $wp_scripts;
wp_enqueue_script( 'jquery-ui-core' );
// get registered script object for jquery-ui
$ui = $wp_scripts->query( 'jquery-ui-core' );
// dequeue old style and scripts loaded by the Divi theme
wp_dequeue_style( 'et_pb_admin_date_css' );
@soderlind
soderlind / class.first-page-image.php
Last active March 12, 2016 21:59
WordPress: Find the first image on a page in this order: Feature image, oembed image, image in post
<?php
class First_Page_Image {
private static $instance;
private static $post_ID = 0;
public static function instance() {
if ( self::$instance ) {
return self::$instance;
}
@soderlind
soderlind / dss_convert_nbsp_wp_insert_post_data.php
Last active May 24, 2016 07:52
On save/update, convert nbsp (\xc2\xa0 and \xa0) to space
<?php
/**
* Plugin Name: On save/update, convert nbsp to space
*
* From http://wordpress.stackexchange.com/a/168358, modified by me
*
* @package 'dss_convert_nbsp_wp_insert_post_data'
*/
add_filter( 'wp_insert_post_data', 'dss_convert_nbsp_wp_insert_post_data', '99', 2 );