Skip to content

Instantly share code, notes, and snippets.

@sudar
sudar / wp-increase-timeout.php
Created February 13, 2013 15:54
Increase the curl timeout in WordPress
<?php
Copied from http://fatlabmusic.com/blog/2009/08/12/how-to-fix-wp-http-error-name-lookup-timed-out/
//adjustments to wp-includes/http.php timeout values to workaround slow server responses
add_filter('http_request_args', 'bal_http_request_args', 100, 1);
function bal_http_request_args($r) //called on line 237
{
$r['timeout'] = 15;
return $r;
}
@sudar
sudar / error_msg.php
Created June 27, 2015 08:10
Creating single select WordPress taxonomies. Explanation at http://sudarmuthu.com/blog/creating-single-select-wordpress-taxonomies/
<?php
/**
* Display an error message at the top of the post edit screen explaining that ratings is required.
*
* Doing this prevents users from getting confused when their new posts aren't published, as we
* require a valid rating custom taxonomy.
*
* @param WP_Post The current post object.
*/
function show_required_field_error_msg( $post ) {
@sudar
sudar / backup.sh
Created April 14, 2020 03:44
Bash script to backup WordPress sites. Add this to a cron job and run it every day
#!/bin/bash
NOW=$(date +%Y-%m-%d-%H-%M-%S)
SQL_FILE=${NOW}_db.sql
# Backup database
/usr/local/bin/wp db export ../backups/$SQL_FILE --add-drop-table
# Compress the database file
gzip ../backups/$SQL_FILE
@sudar
sudar / 1.php
Last active September 10, 2022 17:59
How To Properly Create Tables In WordPress Multisite Plugins. Explanation at http://sudarmuthu.com/blog/how-to-properly-create-tables-in-wordpress-multisite-plugins/
<?php
// Creating tables in Single site installations
function on_activate() {
create_table();
}
function create_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'table_name';
@sudar
sudar / store-post-meta-gutenberg.js
Created August 6, 2020 09:32
store-post-meta-gutenberg
const { domReady } = wp;
const { useMemo } = wp.element;
const { TextControl } = wp.components;
const { select, useSelect, useDispatch } = wp.data;
const { PluginDocumentSettingPanel } = wp.editPost;
const { registerPlugin } = wp.plugins;
const { __ } = wp.i18n;
/**
* Output the Meta panel for Report pages.
@sudar
sudar / kses.php
Created August 4, 2020 05:58
Sample code to demonstrate the issue with wp_kses and allowed_html for sanitizing svg's that contain attributes with numbers at the end
<?php
/**
* Sample code to demonstrate the issue with wp_kses and allowed_html for sanitizing svg's that contain attributes with numbers at the end.
*/
$svg = <<<SVG
<svg>
<path x1="10" y1="10"/>
</svg>
SVG;
@sudar
sudar / apache-error-mailer.py
Last active March 8, 2020 17:57
Automatically send unique errors (with count) from Apache error log as email. Details at http://sudarmuthu.com/blog/automatically-send-unique-errors-with-count-from-apache-error-log-as-email/
#!/usr/bin/env python
import sys
import mandrill
MANDRILL_API_KEY = "api-key"
EMAIL_FROM = "your@email.com"
EMAIL_TO = "your@email.com"
EMAIL_SUBJECT = "Error Log"
@sudar
sudar / mspdebug
Created June 3, 2012 06:47
Using mspdebug to flash a firmware in ez430
sudar@sudar-desktop:~/code/watch/openchronos-ng-modular$ mspdebug rf2500
MSPDebug version 0.18 - debugging tool for MSP430 MCUs
Copyright (C) 2009-2011 Daniel Beer <dlbeer@gmail.com>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Trying to open interface 1 on 002
rf2500: warning: can't detach kernel driver: No data available
Initializing FET...
FET protocol version is 30001000
@sudar
sudar / url-encode.c
Created August 20, 2012 14:59
URLEncoding in C
int c;
char *hex = "0123456789abcdef";
while( (c = getchar()) != EOF ){
if( ('a' <= c && c <= 'z')
|| ('A' <= c && c <= 'Z')
|| ('0' <= c && c <= '9') ){
putchar(c);
} else {
putchar('%');
/**
* Retrieve posts ids given their title.
* Use this function if there are more than one post with the same title.
*/
function get_multiple_posts_by_title($title) {
global $wpdb;
$posts_ids = array();
$posts = $wpdb->get_results( $wpdb->prepare( “SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type=’post_type’”, $title), OBJECT );
foreach ($posts as $post) {