{"id":6324,"date":"2016-03-16T11:22:52","date_gmt":"2016-03-16T15:22:52","guid":{"rendered":"https:\/\/themespress.ca\/?p=6324"},"modified":"2022-09-10T11:20:43","modified_gmt":"2022-09-10T15:20:43","slug":"adding-wordpress-php-filter-inside-widgets","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/adding-wordpress-php-filter-inside-widgets\/","title":{"rendered":"Adding WordPress PHP Filter inside Widgets"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>With this tutorial, we will set up a function that will enable you to display PHP content in widgets. It is not a common practice, but it can come in handy. Our function runs the PHP inside the widget.<\/p>\n<p>This is a <strong>widget_text<\/strong> filter.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-7915909\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-7915909\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"standard\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ filter php pour widget WordPress HTML \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ \nadd_filter(&#039;widget_text&#039;,&#039;via_execute_php&#039;,100); \nfunction via_execute_php($html){ \nif(strpos($html,&quot;&lt;&quot;.&quot;?php&quot;)!==false){ \nob_start(); \neval(&quot;?&quot;.&quot;&gt;&quot;.$html); \n    $html=ob_get_contents(); ob_end_clean(); \n} \nreturn $html; \n}<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>The ob_start() triggers the process. As long as it&#8217;s executed, headers aside, nothing is sent to the browser, but it is temporarily cached.<\/p>\n<p>The cached content can be copy\/pasted with the ob_get_contents() function. In order to display the content, use the ob_end_flush() function where as ob_end_clean() will delete the content.<\/p>\n<p>Learn more about the <a href=\"https:\/\/codex.wordpress.org\/Plugin_API\/Filter_Reference\/widget_text\" target=\"_blank\" rel=\"noopener\">Widget Text<\/a> filter<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial, we will set up a function that will enable you to display PHP content in widgets. It is not a common practice, but it can come in<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/adding-wordpress-php-filter-inside-widgets\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":4524,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[353],"class_list":["post-6324","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-filters"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=6324"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6324\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/4524"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=6324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=6324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=6324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}