Skip to content

Instantly share code, notes, and snippets.

@ncserny
ncserny / wordpress-instant-articles-sanitize.php
Created July 16, 2016 22:40
WordPress Instant Articles Sanitize Post
<?php
// Sanitizing / replace the table shortcode with the table before the post gets transformed to IA.
add_action('instant_articles_before_transform_post', function () {
add_filter('the_content', 'sanitize_content_for_ia');
});
function sanitize_content_for_ia($content) {
if (in_category(array('videos', 'mehrseitig'))) {
$content = preg_replace('@\[sam id=.*?\]@', '', $content); // sam id repalcement
@ncserny
ncserny / wordpress-instant-articles-plugin-removal.php
Created July 16, 2016 22:37
WordPress Instant Articles Remove Extra Code
<?php
// remove extra tags above the feed which comes from other plugins.
add_action('wp_footer', 'remove_extra_code_above_feed', 9); // 9 because by default above plugin functions executes at 10 priority.
function remove_extra_code_above_feed()
{
if (is_feed()) {
// removing these methods because they are generating extra content before the Facebook IA feed.
remove_filter('wp_footer', 'gdlr_additional_script');
remove_filter('wp_footer', 'an_prepare');
remove_filter('wp_footer', 'cookielawinfo_inject_cli_script');
@ncserny
ncserny / instant-articles.php
Last active February 7, 2018 11:56
WordPress Instant Articles Query
<?php
add_filter('pre_get_posts', 'instant_articles_query_modified', 10, 2);
function instant_articles_query_modified($query) {
if ($query->is_main_query() && null !== INSTANT_ARTICLES_SLUG && $query->is_feed(INSTANT_ARTICLES_SLUG)) {
$query->set('cat', 1,2);
}
}
?>
@ncserny
ncserny / varnish3-cloufdlare-wordpress-w3totalcache.vcl
Last active July 27, 2016 04:37
Varnish 3 VCL for Cloudflare + WordPress + W3 Total Cache
# Backend Server
backend default {
.host = "XXX.XXX.XXX.XXX"; # IP Address of your Web Server
.port = "80"; # Port of your Web Server
}
# Hosts allowed to purge Varnish
acl purge {
"localhost"; # Allow varnish to be purged from the same server it is running on
"XXX.XXX.XXX.XXX"/24; # IP of your web server that is allowed to purge the cache
@ncserny
ncserny / Varnish Redirect Old URLs To New Domain
Last active December 25, 2015 17:39
Varnish: Easily redirect "olddomain.com/..." to "newdomain.com/..." URLs at the cache level.
# Redirect "olddomain.com/..." to "newdomain.com/..." URLs at the cache level.
# Just set an error code (in our case 750) in vcl_recv when accessing old urls, then define the redirect in vcl_error.
# Tested with Varnish 3.X
sub vcl_recv {
# Set error code when accessing an old url.
if (req.http.host ~ "^(www\.)?olddomain\.com$") {
error 750;
}
}