Skip to content

Instantly share code, notes, and snippets.

Avatar

Mohammad Jangda mjangda

View GitHub Profile
@mjangda
mjangda / wp-command-line-script.php
Created Nov 2, 2011
Simple generic command line script for WordPress
View wp-command-line-script.php
<?php
// Let's load WordPress
require( 'wp-load.php' );
if ( ! function_exists( 'wp' ) )
die( 'Sorry, looks like WordPress isn\'t loaded.' );
// Run the core code for our script
my_command_line_script();
@mjangda
mjangda / ngrok-and-jetpack.md
Last active Feb 17, 2022
How to connect ngrok to your local WordPress environment (props @DanReyLop)
View ngrok-and-jetpack.md

How to develop with Jetpack locally with ngrok

To connect Jetpack in your local installation, you'll need a way for WP.com servers to reach your server. That can be done in a number of different ways:

  • You can open your router's ports and use your public IP
  • You can use some kind of Dynamic DNS provider.

But these options fall short of ngrok, which is a "localhost tunnel". It basically allows the Internet to hit a local port on your machine without worrying about ports or IPs.

As long as ngrok is running, Jetpack / WP.com will be able to communicate with your local site. This will allow remote modules like Site Search and Manage to work.

@mjangda
mjangda / allow-ids.php
Last active Jan 21, 2022
Don't let kses strip out ids from section tags
View allow-ids.php
<?php
function x_allow_ids_on_tags() {
global $allowedposttags;
$tags = array( 'section', 'article' );
$new_attributes = array( 'id' => array() );
foreach ( $tags as $tag ) {
if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) )
$allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes );
@mjangda
mjangda / wp-error-merge.php
Created Jun 26, 2013
Merge multiple WP_Error objects together.
View wp-error-merge.php
<?php
function wpcom_wp_error_merge() {
$wp_error_merged = new WP_Error();
$wp_errors = func_get_args();
foreach ( $wp_errors as $wp_error ) {
if ( ! is_wp_error( $wp_error ) )
continue;
foreach ( $wp_error as $key => $errors ) {
@mjangda
mjangda / domain-whitelist-check.php
Created Jan 17, 2012
Checks if a given url matches a domain whitelist
View domain-whitelist-check.php
<?php
function my_is_valid_domain( $url ) {
$whitelisted_domains = array( 'mydomain.com', 'mydomain.net' );
$domain = parse_url( $url, PHP_URL_HOST );
// Check if we match the domain exactly
if ( in_array( $domain, $whitelisted_domains ) )
return true;
$valid = false;
View test-vip-private-files-unpublished-files.php
<?php
<?php
namespace Automattic\VIP\Files\Acl;
use WP_Error;
class VIP_Files_Acl_Unpublished_Files_Test extends \WP_UnitTestCase {
const TEST_IMAGE_PATH = VIP_GO_MUPLUGINS_TESTS__DIR__ . '/fixtures/image.jpg';
@mjangda
mjangda / beta.php
Last active Nov 17, 2020
Client-side, randomized, percentage-based segmentation using the WordPress Cache Segmentation API. Only supports 1 experiment and 1 control group.
View beta.php
<?php
require_once( WPMU_PLUGIN_DIR . '/cache/class-vary-cache.php' );
use Automattic\VIP\Cache\Vary_Cache;
Vary_Cache::register_group( 'beta' );
add_action( 'wp_head', function() {
// TODO: if possible, load this directly very early in the header.php (instead of wp_head)
@mjangda
mjangda / wpcom-open-graph.php
Created Sep 5, 2011
Add Open Graph tags to your WordPress site
View wpcom-open-graph.php
<?php
/**
* Open Graph Tags
*
* Add Open Graph tags so that Facebook (and any other service that supports them)
* can crawl the site better and we provide a better sharing experience.
*
* @link http://ogp.me/
* @link http://developers.facebook.com/docs/opengraph/
*/
@mjangda
mjangda / remove-post-term.php
Created Dec 21, 2011
Remove a given term from the specified post. This function is missing from core WordPress.
View remove-post-term.php
<?php
/**
* Remove a given term from the specified post
*
* Helper function since this functionality doesn't exist in core
*/
function my_remove_post_term( $post_id, $term, $taxonomy ) {
if ( ! is_numeric( $term ) ) {
@mjangda
mjangda / autosaver.js
Created Mar 3, 2010
How to hook into and pass your plugin data though WordPress Autosave
View autosaver.js
// Must be in an external file or loaded at the end of wp_footer()
jQuery(document).ajaxSend(function(e, x, a) {
var awesome = 1;
a.data += '&' + jQuery.param( {is_awesome: awesome} );
});