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.

Examples

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:

<?php
global $ptw_post_id;
if (in_category(23, $ptw_post_id) {
?>
TEXT TO SHOW
<?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.

by Stefano Lissa

8 thoughts on “PHP Text Widget

  1. Pingback: プロフィール表示ウィジェット作ったよ! | みもらのドラクエ日記

  2. Pingback: PHP Text Widget — Allow PHP In WordPress Text Widget | Ace Campaign

  3. Pingback: PHP Text Widget — Allow PHP In Wordpress Text Widget

    1. Ian

      Hi Stefano

      I’m using your free version of Newsletter plugin for WP (thanks!). I’m trying to combine it with a free pop-up plugin, so that when someone opens my site, they get a pop-up that invites them to subscribe to my newsletter. So far, however, I’m not able to find the right HTML code so that I can enter the newsletter into the pop-up.

      Is this something that’s possible as far as you’re concerned?

      Thanks

      Ian

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>