loading
I currently have availability
logo

Hi, I'mKevin Shoffner

PHP function into WordPress Widget

So you have found a great plugin on the WordPress plugin site, or elsewhere, but it wasn’t turned into a WordPress widget?  Do you really want to modify your theme, especially if you have multiple different plugins you need throughout your theme.  Then you have to remember which ones you used, where they went, and how to do it every time there is a theme update?  I doubt you have the time to do that.

Why not just make the PHP functions into simple copy/paste widgets without having to know any coding?

Lots of people try to simply use the WordPress Text widget, and they are close.  Unfortunately the normal Text widget allows you to insert arbitrary Text and/or HTML code, but not PHP. This method I am going to show you how to do allows you to still have the same Text and/or HTML capability, but also parses any inserted PHP code and executes it. This makes it easier to migrate to a widget-based theme.

Here is a quick step-by-step walk-through, only 5 steps, on one of the most handy ways I recommend to my clients

Step #1:

Find the plugin you want to use and when you see in the Installation process the WP plugin author suggests you do something like=

Insert

<?php if ( function_exists('insert_anyPluginName') )
 { insert_anyPluginName(); } ?>

into your template file.

Normally this would mean, it’s time to open your

header.php, footer.php

or some other theme page and figure where to put it into your PHP.  But let’s make it slightly less painful for somebody who doesn’t have PHP experience, or has the ability to update their theme every time it gets automatically updated. Go to Step 2

Step #2:

Open your theme

functions.php

file, and go to the very end.  Now copy/paste the following information

add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}

You don’t even have to know what it is doing here

 

Step #3:

Save your

functions.php

file and upload via FTP

 

Step #4:

Now go to your WordPress Dashboard, navigate to Appearance >> Widgets
Move the “Text” widget to your sidebar which you want it to show.

Step #5:

Paste the original PHP function code the author of the plugin recommended, and it will work on your site.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: