• hi all

    i’ve written a small custom function which allows me to easily create an image gallery based on the content of a specified folder. it’s based on a tip i saw somewhere, which allows lightbox js to create a slideshow after clicking on a single thumbnail, by hiding all the images except the first one with css.

    since i’ve never done this before, i’m not sure how the code is supposed to integrate wordpress. for now, it’s inside a custom page template (the function as well as the call to it), but this doesn’t seem right. it makes the template become the actual content, and i would like to be able to call the function from within a post (actually, from within a “page”).

    i’d like know if the function code is supposed to go into the functions.php file of my theme, or as a plugin, or somewhere else. and how can i call it from within the post, like other custom functions do (with code such as [gallery]). will i be able to pass parameters to it?

    i’ve posted the code at https://www.talino.org/gallery_code.txt (it is inside the page template, right before the loop). it simply takes a folder, a title and the css selector which is set to hide, scans a folder and spits out the correct html. it works fine for me, it just seems in the wrong place.

    thanks for any help

Viewing 4 replies - 1 through 4 (of 4 total)
  • i’d like know if the function code is supposed to go into the functions.php file of my theme, or as a plugin, or somewhere else.

    That’s up to you.

    A plugin is the best way as it is the most portable.

    In the functions.php for a theme works well if this is code you do not plan to distribute to others, nor use more than one theme on your site (or better, for functions which are tied to a theme).

    The ‘somewhere else’ option is pretty general. It’s recommended one not slip their own code into the WordPress core source, as it tends to make upgrading more of a pain than it need be. Beyond that, you can certainly create your own PHP documents and include these when needed. But the two previous methods are the typical WP-ways.

    Thread Starter morgentau

    (@morgentau)

    thanks, it’s becoming a little clearer now. i think i’ll distribute it as a plugin because it might be useful to other people (it’s built to be as generic as possible).

    a couple of more questions, though:

    1. in a tutorial by devlounge, it says “To avoid naming collisions, it is imperative that all plugins incorporate a PHP class structure.” according to the wordpress documentation, this is a possibility, not a rule. i don’t know anything about class structure in php, and i’d rather not include code that i don’t fully understand in my plugin. is it ok if i choose a function name that seems unique enough?

    2. the function scans a user-defined folder for files. for the time being it only uses readdir with a conditional statement which ignores specific files (such as ‘.’, ‘..’ etc.). are there some security issues that i should know about when using code like that in a plugin?

    thanks a lot

    1. Yes. I tend to bother with ’embedding’ my plugin’s functions in classes only when I personally find a need to, not because of some claimed “imperative.” Also, often a plugin’s functions are expected to be available publicly, say as a template function.

    One naming rule of thumb for WP plugin developers is to come up with a unique (but for you, regular) prefix for your functions, and stick that on each of your functions. For example, I often use “szub_” (from my website domain).

    2. There can be. As a gallery plugin we’re talking about dealing with images alone, so forcing further restrictions as to the types of files handled, probably based on extension, may help some.

    Thread Starter morgentau

    (@morgentau)

    thank you very much for the detailed reply. i’ve finished a first version of the plugin, a somewhat intense undertaking that i wouldn’t have gone through without your suggestions ??

    i’ve submitted it to the wordpress plugin repository. if you have some time and feel like pointing out major errors and omissions, please check out https://www.talino.org/sls/

    please note that i haven’t implemented file extension scanning yet.

    thanks again.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘newbie question: best way to integrate new function’ is closed to new replies.