0

I have read this stack article, but it refers to themes.

I am writing a plugin not a theme. Do I basically do the same in the plugin's activation method? I have been reading the developer documents, but they don't seem to cover where things like this should be done. Just that they can be done.

3
  • Welcome to WPSE. Actually, the question you refer to is about plugins.So you should be fine following that approach.
    – cjbj
    Aug 23, 2018 at 14:27
  • Thanks cjbj. I'll do that so. Seems like a straight forward approach. But I am trying very hard to adhere to best practice as this is a project someone else may take over.
    – Wing
    Aug 23, 2018 at 14:28
  • 1
    Actually, things like this should always be done in a plugin. Themes are about representation of the content, plugins about adding functionality
    – cjbj
    Aug 23, 2018 at 14:31

1 Answer 1

0

I have read this stack article, but it refers to themes.

It does not. The word "theme" is mentioned nowhere in the question or answers. Edit: Apparently it mentions functions.php. Regardless, the below still applies.

Do I basically do the same in the plugin's activation method?

You do not need to (and should not) use the activation method of the plugin. The code from the answer can go anywhere in a plugin or theme.

Developing themes and plugins is essentially the same process, with the same code. The only differences are:

  • WordPress will only automatically load template files from themes.
  • Plugins are loaded/run before the theme.
  • You need to use different functions to get the path or URL of plugins (eg. plugins_url()) to those used for themes (eg. get_theme_file_uri()).
  • Functions that should only run once when the plugin or theme is activated are hooked differently.

Plus the philosophical difference that themes should be responsible for presentation, and everything else belongs in a plugin.

Any code you see for doing something in WordPress will work the same ina theme or plugin unless any of the above points are relevant.

11
  • "It does not." Both of the links the article sources refer to a functions.php file in the theme.
    – Wing
    Aug 23, 2018 at 14:47
  • And saying "It should not use the activation method" but "can go anywhere in a plugin or theme" is incongruous. If it should not go in the activation method; where should it go?
    – Wing
    Aug 23, 2018 at 14:48
  • 1
    Literally anywhere else. Maybe you should start wondering if you're already up to writing a plugin if this is the question you need to ask. I'd advice by diving into WordPress hooks (codex.wordpress.org/Plugin_API/Hooks) first. Plugin code is literally run as soon as the plugin runs. So if you're code needs to be run whenever then place it simply in the plugin file. If not, add the code to the correct hook to ensure the code runs at the correct time
    – Jebble
    Aug 23, 2018 at 14:52
  • I have read it. It tells you how to do it. Not where it should be done.
    – Wing
    Aug 23, 2018 at 15:08
  • @JeffreyvonGrumbkow, "you should start wondering if you're already up to writing a plugin" I am sure you could have figured a nicer way to say this, or just not say that at all as it doesn't add any value to your comment. Aug 23, 2018 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.