Support » Plugins » Add the Gallery Random Block to WP

  • Resolved warpig

    This is a little hack to enable you to display Random Photos from your Gallery installation
    Open up your block-random.php file (which should be included with the Gallery dist. If not, search the Gallery Forums for a copy, or download from this URI), in block-random.php add;
    $GALLERY_BASEDIR = "/abolute/path/to/your/gallery/";
    Including the back slash. Be sure to add this just before the Hack Prevention code.
    Next, modify your WordPress template to include the default PHP output buffering as;
    < ?php
    /* begin output buffering */
    /* Don't remove this line. */

    you can see, I have ensured that ; is the first function called when the page loads.
    Finally, you need to setup the template to send all the headers so, add;
    < ?php

    At the end of your page.
    Now, the most important thing. FTP your block-random.php script into your WordPress folder, and simply use a PHP include. I have used it on my site as;
    < ?php include(ABSPATH . 'block-random.php'); ?>
    There you have it. A Random photo block for your WordPress Blog.

Viewing 15 replies - 1 through 15 (of 18 total)
  • Thread Starter Anonymous

    Now although this little hack does work (tested with Gallery 1.4.2 and 1.4.3 with WP 1.2) can anyone comment on whether using ob_start() is going to cause any sort of security risk?

    There is no point in using ob_start(); unless its at the very beginning of your main PHP code and you flush it at the end of the code. All you’re doing is output buffering. Its not a security risk, all it does is delays sending the page until the HTML is fully pieced together.
    Check out Output Buffering in the PHP docs for more info.

    Thread Starter Anonymous

    Is it best to use ob_start() at the beginning of the WP index.php file (as above) or the beginning of the php inclue where it calls the block-random file?
    I will have a play with the actual block-random.php file and see if I can sort it out to work with WordPress regardless. I know gallery 2.0 is due out soon, but no harm in a temp hack until the next big version is out eh? šŸ™‚

    Thread Starter Anonymous

    i need some help with your hack can you email me? I couldn’t find your contact info anywhere {locopalla at yahoo dooot com} I am getting this error:
    Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 24117 bytes) in /public_html/gallery/util.php on line 349
    Anyone have any ideas?

    Hey warpig, I tried doing as you say above, and then my index page started saying:
    Parse error: parse error, unexpected $ in /www/hosts/ on line 173
    So I deleted everything I added, and it still says that. Obviously, something went wrong and I’m a PHP newbie… can you lend me a hand? email same as forum name, at yahoo.

    Hey warpig, I fixed my “oops”, but I’m still confused. Where exactly do I put the ob_end_flush and the include? I put ob_end_flush at the very very end of index.php, which resulted in it being simply printed at the bottom of the page. When placed right after the php code for posts and comments, I got a function_main() error. The include, whereever I put it, also simply gets printed instead of generating html.

    I’m a bit confused… where does one place the:
    < ?php include(ABSPATH . ‘block-random.php’); ?>
    Also, has anyone tried this with “block-random-enhanced.php”?
    I added the GALLERY_BASEDIR near the top of my block-random-enhanced.php file and copied the file to my WP 1.2 directory.

    Does this work with WP1.3 and Gallery 1.4.4pl2? I’m getting an error that it’s trying to redeclare _()
    Fatal error: Cannot redeclare _() (previously declared in /disks/ideraid1/web/ in /disks/ideraid1/web/ on line 179. Seems to be some undeclared variables concerning language in Gallery’s initLanguage() and functions.php.

    is there any way to use this code to display multiple random images? i got it working with one, but if i duplicate the < ?php include(ABSPATH . ‘block-random.php’); ?> line, it breaks. i’d like to show five random images if possible…

    I can’t get this to work, I get the following error:

    Warning: main(/Library/WebServer/Documents/wordpress/init.php): failed to open stream: No such file or directory in /Library/WebServer/Documents/wordpress/block-random.php on line 46

    Fatal error: main(): Failed opening required ‘/Library/WebServer/Documents/wordpress/init.php’ (include_path=’.:/usr/lib/php’) in /Library/WebServer/Documents/wordpress/block-random.php on line 46

    It looks like WordPress isn’t allowing the path to resolve back to /gallery at all.

    Any help is appreciated.

    Anyone? Bueller?

    Can someone PLEASE provide some solid information for those new to WP? It would be nice to have answers to a couple of the questions above, like:
    Where do we put the php include for the block_random.php?
    Do we put the ob_end_flush on index.php, along with the other stuff that’s already there? (WP 1.5 has a rather threadbare index.php–do we put the ob_end_flush in it’s own set of <? ?> after the ones that are already there?)
    I’m using an unmodified version of WP 1.5, so can someone please provide specific instructions on where to put these things (i.e. which files, where exactly in those files, etc.)? And please remember, we’re not all php programmers.
    Thanks in advance for any help.

    Using the clues from this entry, I’ve got the random block showing on my site (Gallery 1.5 RC1 (installed in htdocs/gallery/, WP 1.5 (installed in htdocs/), parishuddha theme). It should be applicable to any theme.

    In wp-content/themes/parishuddha/sidebar.php, I added
    <li id="photo"><?php _e('Photo:'); ?>

    • <?include(ABSPATH . 'gallery/block-random.php'); ?>

    after the meta section and before the closing /UL for the menu. I also edited the style.css file to make the body width 790 (was 770) so that the graphic had enough room to fit.

    There was no need to make any changes to the block-random.php file or to use the ob_*() functions. ABSPATH probably only works for me because wordpress is in /, and gallery is in /gallery. If your wordpress install is in /wordpress, and gallery is in /gallery, then you will probably have to edit block-random.php and add $GALLERY_BASEDIR='/real/path/to/gallery'; (/var/www/localhost/htdocs/gallery for me) after the line that reads $GALLERY_NO_SESSIONS. Then copy block-random.php to your WP install directory, and edit your theme file appropriately. As stated, I modified my sidebar.php file, but you may want the random photo elsewhere.

    Just a note. I had trouble with one of my WordPress sites getting block-random.php to work properly. Had nothign to do with WordPress. You can call block-random.php directly form your browser to see what kind of error you might be getting. TUrns out for me I had some orphaned albums. Under the Admin Page in gallery there is a tool called ‘find orphans’ I ran that, reattached them to the root gallery album and deleted them (since they were empty – but you should check for) Once I did this, block-random.php worked fine and I was able to include it in WordPress with no problem.

    I too added it in my sidebar by simply including:

    <?php include(ABSPATH . ‘/gallery/block_random.php’); ?>

    in a sidebar division. Note I’m calling the block-random from within the gallery subdirectory – not a copy in the wordpress tree – this ensures it’ll be updated when I update gallery. Works great! Note if you are using it in a sidebar, you should do somethign like this to ensure the font style is right (ie small):
    <div class=”whatever your sidebar class is”>
    <ul id=”grand_list”>

    • <?php include(ABSPATH . ‘/gallery/block-random.php’); ?>
    • </div>

      You can see it in action at my family website

    I’m getting a weird error message :
    Warning: Cannot modify header information – headers already sent by (output started at /home/chewxy/public_html/blog/wp-content/themes/AA/header.php:5) in /home/chewxy/public_html/gallery/lib/lang.php on line 356

    Images show up though. Any help here? I’m a PHP dummy.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Add the Gallery Random Block to WP’ is closed to new replies.