• Resolved wpfan1000

    (@wpfan1000)


    Hi,

    I currently have some code in a page template to display a random image.

    I would like to move that code to a plugin and then call a function in the template to execute the code.

    Can I just create a function in the plugin and then place the code inside the function?

    And then call this function in the template?

    I ask this partially because I have read that a plugin must use a hook or filter and I do not have this nor know how to do that.

    Thanks ahead of time!

Viewing 5 replies - 1 through 5 (of 5 total)
  • HI wpfan

    A simple solution would be to put your code into a separated php file and include that in your functions.php file, that way you can call your function directly.

    Moderator bcworkz

    (@bcworkz)

    I agree with Amras that placing your function in functions.php makes the most sense, with the exception that ideally it should be the functions.php of a child theme, along with any other changes you make to your theme.

    However, since you asked, you may also define a function as part of a plugin and call it directly too, assuming the plugin is activated of course. While plugins invariably use filter and action hooks, there is no actual requirement that they do so. Plugins typically alter the inner workings of WP. To do that, filters and action hooks must be used.

    Themes and templates alter the appearance of WP content. Any functions related to templates and how content is presented rightfully belong in functions.php. Your function will work equally well from either location, but for the sake of staying organized, functions.php is better IMO.

    Thread Starter wpfan1000

    (@wpfan1000)

    Hi Amras and bcworkz,

    Thank you both for your reply.

    What you say makes total sense to me.

    I should have explained a bit more about my situation and requirements. I do some support in the How-To and Troubleshooting forum and sometimes I wish people would tell more about their situation and requirements and here I am, not doing that 🙂

    I did start off writing all code within my functions.php file. In a child theme. Over time I wrote a whole bunch of shortcodes / functions in that file to the point where the file got too big.

    So as you rightly suggested I put the code for each function in a sep file and then used the include statement in functions.php to include each sep code file.

    What I also did not explain is that most of the functions are not theme related. For example I have some code right now in the page template of the child theme that shows a random image. Another piece of code shows previous and next links of a page.

    And I would now like to use them with other themes in other sites.

    So I thought the best way would be to move the code to a plugin, define the function in the plugin, and then call the function in a theme template file.

    Hence my original post.

    Please let me know if you think this is the best way to go now that I have explained more. I would appreciate other suggestions.

    It is also very good to know that I don’t have to use a hook or filter, that it is optional.

    Thanks again for your help!

    Moderator bcworkz

    (@bcworkz)

    You really only have two choices, child theme or plugin. Calling functions from a page template still sounds theme related to me. But if you think a plugin is the most expedient way to distribute your code, then go ahead and do so. If that is what keeps your code organized in a way that makes sense to you, then that is the best way to go.

    Thread Starter wpfan1000

    (@wpfan1000)

    Thanks again for clarifying my options.

    I do agree with you that calling functions from a template is theme related.

    I just don’t know enough at this stage to do differently – i.e. to modify the theme from the plugin without putting a function in a template.

    I do see one can insert the output of a plugin at the bottom of a page programatically, I just don’t know how to do it. So I will put a function in the template.

    But since I would like to use the code in other sites, I think it is better to move the code from a template to a plugin so that the code is more portable. And then call a function in the template.

    The first way I learned to modify WP was to write code in a template. Now I am learning how to write a plugin. Maybe not the best way, but this is how I am progressing.

    Thanks for your help to help me progress 🙂

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Move code from template to plugin’ is closed to new replies.