Creating a Custom Drupal Block With PHP

The main goal I had was to add a list of forums to the left sidebar of The Central America Forum, a website I implemented when I lived in Costa Rica. I wanted to do this for SEO so that my content is not buried so deeply but can be reached more easily by search engines and users from the front page. (I've already got a block for anonymous users in the very last part of the page to load, the right block, with the all-time most popular pages; that's good, of course, but it leaves the less-popular pages buried.) 

It's not hard to create a custom block, but all the steps have to be in place.

Here is the basic roadmap: http://drupal.org/node/246986

But because I'm running drupal 6, the PHP input format is not enabled by default - it has to be enabled by enabling an additional module.  So go to admin/build/modules and enable:

6.8 Allows embedded PHP code/snippets to be evaluated.

Set the permissions properly for that the input format (the default it is not even enabled for moderator; you must enable for moderator if you have a moderator user class like I do and intend the user to insert the php; do NOT enable PHP for any users other than moderator because it is a huge security hole).

Easy enough.  Now back to the roadmap, create the block (I set visible for moderator only for testing), enable it on the left side, insert the php code, and specify the input format to be PHP.  Works fine.  After I've finished customizing it the way I like it, I'll enable it for registered and anonymous users and make some further additions to this blog.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Setting permissions?

Um, it was simple to follow your instructions about going to admin/build/modules and enabling PHP filter. The next, and apparently vital, step was vague enough ("set the permissions properly") that I can't be sure whether I've done it. I went to the permissions page, saw only authenticated and unauthenticated users, added a user called "admin", set as many permissions for "admin" as seemed relevant, saved configuration, restarted servers, and still the block input format is empty.

setting permissions

Hi,

I'll try to describe more clearly.  First of all - my comments are deliberately vague because setting the permissions properly depends on your installation and userbase.  Are you the only "authenticated user" on your site?  If so, there is no need to add any other user class, just use "authenticated user".  The reason this is so important is because if you allow anyone else to run php code, they can easily destroy your entire site, either maliciously, or by mistake.  You have to be very careful to only allow yourself and very trustworthy and competent programmers to have "php" permission.  If you do have many other users, you do need a separate "administrative" class to administer blocks containint php code.

Assuming you've gotten that figured out - also don't forget as well to set the "input format" of the block you created to be "php" format.

If it's still not working, tell me more details of your system and we can troubleshoot it further.

-Peter 

SEO

Thanks for sharing the information.This post has clearly explained all the things properly... 

Thanks!

Great post. I have recently started learning drupal and this is just what I needed.