Welcome to Geeklog Saturday, November 18 2017 @ 04:53 am EST

PHP Block How-To

  • Contributed by:
  • Views: 12,955
Announcements Ok, so 1.3 has been on the market now for a few weeks and many of you have started tweaking your site by modifying the templates and what not. But now what? Well, I thought I'd write a quick how-to with a real life example on using a Geeklog PHP block. Overview

PHP Blocks give you the power of letting a Geeklog block execute some code to generate dynamic output. To make it more secure, you are not allowed to add PHP code directly to the content of your block but, instead, must add a function to your /path/to/geeklog/lib-custom.php and then assign your PHP block to execute that function. To keep someone from executing arbitrary PHP functions we require PHP block functions to start with the phpblock_ prefix.

Once your function is called you have the full power of PHP at your disposal to do what ever you little heart desire.

Example

To drive the power of this home, let's try a real-world example that I recently added to Iowa Outdoors. This example will put a "refer this site" form on your site so others can refer your Geeklog website to others.

1) First, code the function in your lib-custom.php (remember function name must start with phpblock_ prefix). You can get the code for this how-to here.

2) Create the PHP block via the Geeklog block administration page:

  • Navigate to http://www.mygeeklogsite.com/admin/block.php
  • Click on the "New Block" link
  • Enter the block name, title, blockorder, side and be sure to select php_block from the block type drop-down
  • Scroll Down To PHP Block Options and enter the name of your function (for our example it would be phpblock_refer_site)
  • Assign appropriate permissions
  • Save it and you are done!
  • Other Information

    The function in the code-sample stores the number of referrals made in the database. If you want to ever find out how many referrals you have or want to add this to your stats.php page the SQL you would need is:
    SELECT value FROM vars WHERE name = 'num_site_referrals'

    Was this easy enough to follow?