WPG2 Sidebar Block: Recent or Random Images from Specific Album

I do not understand why WPG2 does not already support this feature. How hard can this be? You can include one random image from a specific album or all the most recent images from the entire gallery but not a selection of random images from one gallery. Why not take the next logical step?

In g2embed.php, I copied the function g2_sidebargridblock and renamed it g2_sidebargridblock_specific. I added a g2_itemid input parameter and copied three lines of code from g2_sidebarimageblock into the new function, even removing an unnecessary line:

if ( $g2itemid ) {
$blockoptions['itemId'] = $g2itemid;
}

Now, I call the new function from within my WordPress sidebar.php like so:

print g2_sidebargridblock_specific(1234);

The number corresponds to the itemId of the album as found in Gallery2. What astounding programming I have accomplished!

I must admit that my current implementation is not incredibly robust. If no parameter is passed with the g2_sidebargridblock_specific call, recent or random images will be displayed without regard for album. Fine, great. If, however, an invalid itemId is specified, the function will dump a parameter array and output an error. Also, if an itemId for a specific image is given, rather than that of an album, nothing will be displayed at all.

I hope to add some error checking sometime soon but am happy enough with my current result. Configuring this function through the WPG2 control panel would also be convenient. Whatever: I can manage to edit a WordPress plugin PHP file so I hope I can correctly specify a itemId.
Does anyone know if there is an existing function in WPG2 that accomplishes this functionality? Is there a better way to do the same? I would love to hear about it.


3 Comments

  1. From lotech

    Commented February 5th, 2007 2:29 am

    Good stuff - I'm having issues with the above steps.
    Did you get any further along on the mod to report errors more cleanly?

    FYI the function I've got inside g2embed.php is:

    function g2_sidebargridblock_specific( $g2blocktype="", $g2blockelements="", $g2blockmaximgsize="", $g2blocktitle="" ) {
    if ( $g2itemid ) {
    $blockoptions['itemId'] = $g2itemid;
    }

    }

    Is that right, or have I cut out too much?
    Thanks

  2. From drew

    Commented February 5th, 2007 8:32 am

    You have cut too much. Please note that I said to copy g2_sidebargridblock (in its entirety) and then add the referenced bit from g2_sidebarimageblock. So, again, copy the g2sidebargridblock function completely and rename it and add an input parameter:

    function g2_sidebargridblock_specific( $g2itemid="", $g2blocktype="", $g2blockelements="", $g2blockmaximgsize="", $g2blocktitle="" )

    Then, replace this:

    if ($g2_option['g2_validated'] == "Yes") {

    // Do we have configuration?
    if ( $g2_option['g2_sidebarblockstype'] || $g2blocktype !="" ) {
    // if no option was passed, set to setting in options
    if ( $g2blocktype == "" ) {
    $g2blocktype = $g2_option['g2_sidebarblockstype'];
    }

    With this:

    if ($g2_option['g2_validated'] == "Yes") {

    // Do we have configuration?

    /************ BEGIN NEW CODE ************/

    if ( $g2_option['g2_sidebarblockstype'] || $g2itemid !="" ) {

    // Assign itemID
    $blockoptions['itemId'] = $g2itemid;

    /************ END NEW CODE ************/

    // if no option was passed, set to setting in options
    if ( $g2blocktype == "" ) {
    $g2blocktype = $g2_option['g2_sidebarblockstype'];
    }

  3. From lotech

    Commented February 5th, 2007 4:25 pm

    Thanks very much - got it going now - for anyone else following this I had an error when pasting the code as some 'special characters' had sneaked in “ and ’ - you will have to go through and replace them with " ' to get the code work properly.

    Also make sure your Side Bar Block options (in wp-admin/admin.php?page=wpg2/g2adminsideblock.php are set to Recent or Random Picture and not to Recent/Random Album - that also slowed me down for a couple of mins.

    Again thanks for the hack.

Add a Comment