Skip to content

Instantly share code, notes, and snippets.

@pshapiro
pshapiro / amp-post-template-actions.php
Last active March 7, 2016 15:44
SEL - Enable Google Analytics for AMP WordPress Plugin
add_action( 'amp_post_template_head', 'amp_post_template_add_analytics_js' );
function amp_post_template_add_analytics_js( $amp_template ) {
$post_id = $amp_template->get( 'post_id' );
?>
<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
<?php
}
add_action( 'amp_post_template_footer', 'xyz_amp_add_analytics' );
@pshapiro
pshapiro / class-amp-post-template.php
Last active March 15, 2016 20:54
Fix Schema Validation for WordPress AMP Plugin where posts don't have a featured image.
# Edit AMP Plugin file: amp/includes/class-amp-post-template.php
/*
Change this bit of code to the below uncommented code:
$image_metadata = $this->get_post_image_metadata();
if ( $image_metadata ) {
$metadata['image'] = $image_metadata;
}
@pshapiro
pshapiro / class-amp-post-template.php
Last active March 15, 2016 20:55
Fix for publisher logo in AMP WP Plugin
/* Edit the class-amp-post-template.php file, either via FTP or within your WordPress Dashboard (go to Plugins > Editor and then select “AMP”) and change this:
if ( $site_icon_url ) {
$metadata['publisher']['logo'] = array(
'@type' => 'ImageObject',
'url' => $site_icon_url,
'height' => self::SITE_ICON_SIZE,
'width' => self::SITE_ICON_SIZE,
);
}
@pshapiro
pshapiro / functions.php
Last active February 23, 2017 02:52
Sample WordPress theme functions.php modification to generate Client ID
// Source: https://www.simoahava.com/analytics/google-analytics-client-id-amp-pages/
// REST API for GTM container
add_action( 'rest_api_init', function() {
register_rest_route(
'amp-gtm',
'/amp.json',
array(
'methods' => 'GET',
'callback' => 'retrieve_gtm_json',
)
@pshapiro
pshapiro / search.html
Created May 12, 2017 21:13
Basic example of using the GET method using amp-form
<form action="https://searchwilderness.com/" method="get" target="_top">
<input name="s" placeholder="Search The Blog" required="" type="text">
<input type="submit" value="Search">
</form>
@pshapiro
pshapiro / mailchimp.html
Created May 11, 2017 04:45
Example of posting an email subscriber form to MailChimp in AMP using amp-form
<!doctype html>
<html amp>
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.1.js"></script>
<link rel="canonical" href="http://localhost">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-custom>
@pshapiro
pshapiro / BulkAMPValidator.py
Created February 21, 2017 21:39
AMPBench API Bulk Validator
import requests
import json
import csv
import os
# For details about AMPBench and the API:
# https://github.com/ampproject/ampbench
urlinput = os.path.join(os.path.dirname(__file__), input('Enter input text file: '))
urls = open(urlinput, "r")
@pshapiro
pshapiro / plot-internal-pr.r
Created April 7, 2016 18:07
Plotting Internal PageRank with 1 to 10 map function
library("igraph")
internallinks <- read.csv("C:/Users/username/folder/screamingfrog-all-inlinks.csv")
g <- graph.data.frame(internallinks)
pr <- page.rank(g, algo = "prpack", vids = V(g), directed = TRUE, damping = 0.85)
values <- data.frame(pr$vector)
write.csv(values, file = "output-pagerank.csv")
map <- function(x, range = c(0,1), from.range=NA) {
if(any(is.na(from.range))) from.range <- range(x, na.rm=TRUE)
@pshapiro
pshapiro / SearchLove.py
Created April 28, 2016 19:02
Generate interactive keyword research growth matrix with Python + Bokeh plots from CSV file.
# library imports
import pandas as pd
from bokeh.io import output_notebook, show
from bokeh.plotting import figure, output_file, ColumnDataSource
from bokeh.models import HoverTool, BoxAnnotation, BoxSelectTool, BoxZoomTool, WheelZoomTool, ResetTool
from bokeh.resources import CDN
from bokeh.embed import file_html
# Import csv into pandas dataframe, direct to KNIME version to follow
@pshapiro
pshapiro / GoogleTrendsSlopeCalculator.py
Last active May 12, 2020 15:41
Python script to calculate slope from Google Trends
# Requires pandas. For Windows users, I recommend installing the Anaconda Python distirbution.
# Requires the pytrends library. To install, run "pip install pytrends".
from pytrends.pyGTrends import pyGTrends
import time
import os
from random import randint
import pandas as pd
# Add your Gmail username to the google_username variable and your Gmail password to the google_password variable.
google_username = ""