{"id":4051,"date":"2017-10-04T12:48:16","date_gmt":"2017-10-04T12:48:16","guid":{"rendered":"https:\/\/ampforwp.com\/tutorials\/?post_type=ht_kb&#038;p=4051"},"modified":"2020-05-07T05:32:50","modified_gmt":"2020-05-07T05:32:50","slug":"override-blacklist-sanitizer","status":"publish","type":"ht_kb","link":"https:\/\/ampforwp.com\/tutorials\/article\/override-blacklist-sanitizer\/","title":{"rendered":"How to Override Blacklist Sanitizer?"},"content":{"rendered":"\n<p><strong>Let&#8217;s override&nbsp;the blacklist sanitizer to allow forms in AMP.<\/strong><\/p>\n\n\n\n<p>First Create a new plugin say <strong>AMP Custom Plugin<\/strong> (You can check this tutorial if you don&#8217;t know how to do it: <a href=\"https:\/\/ampforwp.com\/tutorials\/article\/extend-amp-plugin\/\">Custom Plugin for AMP<\/a>&nbsp;)<\/p>\n\n\n\n<p>Create a new file for the class in the <strong>AMP Custom Plugin<\/strong> folder you have just created say <strong><em>&#8220;<\/em>class-amp-blacklist<em>&#8220;<\/em><\/strong><\/p>\n\n\n\n<p>In<strong><em>&#8220;<\/em>class-amp-blacklist<em>&#8220;<\/em><\/strong> include the blacklist sanitizer class and write a new class to extend&nbsp;<strong>AMP_Blacklist_Sanitizer<\/strong><em><strong>.<\/strong><\/em><\/p>\n\n\n\n<p><strong>Here is the code to achieve this task:<\/strong><\/p>\n\n\n\n<p><strong><a href=\"https:\/\/gist.github.com\/MARQAS\/b3e04c59804476314995c618292976ca\" target=\"_blank\" rel=\"noreferrer noopener\">AMP New Blacklist Sanitizer<\/a><\/strong><\/p>\n\n\n\n<p>When you go down in the code you will see a function called <strong>get_blacklisted_tags<em>,&nbsp;<\/em><\/strong>this is the function which is stripping the tags like form. As you can see I have commented the elements which will make the form to work in AMP.<\/p>\n\n\n\n<p>Call this file in the main file of <strong>AMP Custom Plugin<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">add_action('init', 'amp_new_class_sanitizer');\nfunction amp_new_class_sanitizer() {\n    require_once plugin_dir_path(__FILE__) .'\/class-amp-blacklist.php';\n}<\/pre><\/div>\n\n\n\n<p>Unset the <strong>AMP_Blacklist_Sanitizer&nbsp;<\/strong>and add Set our new class for the sanitization.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">add_filter('amp_content_sanitizers','amp_custom_blacklist', 100);\nfunction amp_custom_blacklist( $data ){\n unset($data['AMP_Blacklist_Sanitizer']);\n $data[ 'AMP_New_Blacklist' ] = array();\n return $data;\n}<\/pre><\/div>\n\n\n\n<p>After doing these steps, forms will not be stripped from the AMP Content.<\/p>\n\n\n\n<p>But there might still be an error of action in AMP, so there are two options for this, use custom AMP Editor and change action to action-xhr or filter the content and replace the action with action-xhr.<\/p>\n\n\n\n<p>And also need to add class name ampforwp-form-allow for sanitizing the form.<\/p>\n\n\n\n<p><strong>You can filter the content like this:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">add_filter('the_content','new_form_content');\nfunction new_form_content($content){\n if ( function_exists( 'ampforwp_is_amp_endpoint' ) &amp;&amp; ampforwp_is_amp_endpoint() ) {\n $content = preg_replace('#&lt;form action=&quot;(.*?)&quot; (.*?)&gt;#ius', '&lt;form action-xhr=&quot;$1&quot; class=&quot;ampforwp-form-allow&quot; $2 target=&quot;_blank&quot;&gt;', $content);\n \n }\nreturn $content;\n}<\/pre><\/div>\n\n\n\n<p>You can style the forms with either custom CSS or with the hook;<\/p>\n\n\n\n<p><strong>This is how you can do it with the hook:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">add_action('amp_post_template_css','new_css_for_form',999);\nfunction new_css_for_form(){ ?&gt;\n .amp-wp-content form&gt;div {\n display:none;\n}\n\n&lt;?php }<\/pre><\/div>\n\n\n\n<p>Now you can add forms in AMP without any worries.<\/p>\n\n\n\n<p>You can read more about AMP forms here: <a href=\"https:\/\/amp.dev\/documentation\/examples\/components\/amp-form\/\">Forms in AMP<\/a><\/p>\n\n\n\n<p><strong>That\u2019s it for this tutorial!<\/strong><\/p>\n\n\n\n<p>We hope it helped you. If you have any feedback or questions, then please share them in the comments section below.<\/p>\n<div id=\"div-1\" class=\"hkb-feedback\">\n\t\t\t<strong class=\"ht-mobile-exit__title\">Ask your Questions Directly!<\/strong><br>\n\t\t\t<div class=\"hkb_widget_exit__content\" style=\"font-size: 14px;\">If you can't find the solutions in this article, then just ask us in the comments and we will answer it for you!<\/div>\n\t\t\t<a href=\"https:\/\/ampforwp.com\/support\/#contact\" id=\"button\" target=\"_blank\" rel=\"noopener noreferrer\"><button>Yes, I have a Question<\/button><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s override&nbsp;the blacklist sanitizer to allow forms in AMP. First Create a new plugin say AMP Custom Plugin (You can check this tutorial if you don&#8217;t know how to do it: Custom Plugin for AMP&nbsp;) Create a new file for the class in the AMP Custom Plugin folder you have&#8230;<\/p>\n","protected":false},"author":5,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":[],"ht-kb-category":[183],"ht-kb-tag":[],"_links":{"self":[{"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb\/4051"}],"collection":[{"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/comments?post=4051"}],"version-history":[{"count":34,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb\/4051\/revisions"}],"predecessor-version":[{"id":17245,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb\/4051\/revisions\/17245"}],"wp:attachment":[{"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/media?parent=4051"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb-category?post=4051"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb-tag?post=4051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}