PHP Text Widget

Executes PHP code on WordPress default Text Widget

WordPress has a very useful Text Widget I use extensively to add ads code (usually HTML and JavaScript) on blog sidebar. That widget is limited to pure text but, often, I need to add some logic, for example display different text on different contexts (a page, a category, a post, …) while keeping only one sidebar position and only one text widget.

This plugin extends the standard text widget executing PHP code when present on the widget text.

You can find it here.


A login “widget”

How to create a login box with full control of layout and labels? On post How To Create a Login Widget With PHP Text Widget there is few line of code to copy and paste!

Showing something only for posts in a special category.

You can use this code:

global $ptw_post_id;
if (in_category(23, $ptw_post_id) {
<?php } ?>

The variable $ptw_post_id is the actual post or page id when a post or a page is displayed. On 99% of themes there is no need to save the current post id, but on 1% of themes badly written the global $post variable is changed to a value different of the current post. Sometime is not a theme issues but a problem generated by other plugins. PHP Text Widget tries to save the real post id and make it available on variable $ptw_post_id.

Leave a Reply