Skip to content

Instantly share code, notes, and snippets.

@ncserny
Created July 16, 2016 22:40
Show Gist options
  • Save ncserny/6184f32e74af6615cfc49fc84288f21e to your computer and use it in GitHub Desktop.
Save ncserny/6184f32e74af6615cfc49fc84288f21e to your computer and use it in GitHub Desktop.
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
$shortcode_pattern = '@\[table id=\s*(\d+).*?\]@'; // Pattern to match the [table id=N /]
$matches = array();
// replacment if table shortcode found
if (preg_match_all($shortcode_pattern, $content, $matches)) {
if (isset($matches[1]) && !empty($matches[1])) {
$table_ids = $matches[1];
$table_data = replace_table_shortcode_with_table($content, $table_ids);
$content = $table_data; // table + content
}
}
$content = sanitize_post_content($content);
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment