Discussion:
What goes in a theme and what goes in a plugin
Nikola Nikolov
2014-01-23 19:34:44 UTC
Permalink
When I started working in web development, the work process of the company
I worked at was to put all of the code for a specific website in it's
theme. That means post types, custom fields, theme option interfaces,
shortcodes, etc.

I was recently introduced to the idea that this is definitely not a good
way of creating websites, simply because when the client decides to freshen
up the design of their website, they would either have to rewrite the
templates of the theme, or painfully extract all of the vital parts of the
code out of the theme(or create a child theme, but the themes we were
making were not really meant for using as parent themes).

Instead you should try to leave only the styling to the theme and have
everything else in one(or multiple?) plugins.

I agree with that idea, but my biggest question is how much do you put in
the plugin and how much you leave in the theme. For instance if you want to
display some custom data in the theme(in places different than with
the_content()), but still have templates(so that appearance can be tweaked
from the theme) - do you define some of the functions in the theme, do you
just rely on the plugin being active.

Let me give you an example. You have custom fields(maybe the plugin you
created for the theme is creating them, or it's a third plugin that does
that). You want to display the information from those fields in the theme
and you want to make them easy to access, so you have a function that
returns an array with all of the data. Do I create a function in the theme,
that would pull the data(possibly relying on the plugin by using
function_exists() to prevent fatal errors), do I just use the plugin's
function in the theme?

I would appreciate personal experience, as well as references to articles
on the topic(or a link to a message on the list if it's been discussed
before).

Thanks,
Nikola
Jamie Currie
2014-01-23 19:53:36 UTC
Permalink
Good question. I've got a few big projects that involve a lot of custom
data -- from meta fields to multiple custom tables. The main
functionality is used by a number of different clients, all with
different themes and different needs for how the data is displayed.

I've opted to go with a hybrid approach. Some data is accessible via
shortcodes with parameters. A list of accounts that is pulling data from
a custom table can be used in a page or template via something like
[LIST-ACCOUNTS type=' ' order=' '] etc. For parameters I try to follow
WP conventions for functions like get_posts().

Where possible, data is made accessible to the standard WP system. An
orders system that is using custom tables ultimately triggers
wp_insert_post() and adds a new instance of a custom post type. Some
filters allow any action taken on that order in the Dashboard to update
the custom tables as well. So then a designer can work with them just
like any other post.


You can ensure that a plugin (or multiple plugins) are active via your
theme's functions.php file. Check out the TGM Plugin Activation class:
https://github.com/thomasgriffin/TGM-Plugin-Activation


Jamie Currie
Founder / CEO
wunderdojo
wunderdojo.com
tel: 949-734-0758
1840 Park Newport, #409
Newport Beach, CA 92660
Master web & app developers




------ Original Message ------
From: "Nikola Nikolov" <***@gmail.com>
To: wp-***@lists.automattic.com
Sent: 1/23/2014 11:34:44 AM
Subject: [wp-hackers] What goes in a theme and what goes in a plugin
Post by Nikola Nikolov
When I started working in web development, the work process of the company
I worked at was to put all of the code for a specific website in it's
theme. That means post types, custom fields, theme option interfaces,
shortcodes, etc.
I was recently introduced to the idea that this is definitely not a good
way of creating websites, simply because when the client decides to freshen
up the design of their website, they would either have to rewrite the
templates of the theme, or painfully extract all of the vital parts of the
code out of the theme(or create a child theme, but the themes we were
making were not really meant for using as parent themes).
Instead you should try to leave only the styling to the theme and have
everything else in one(or multiple?) plugins.
I agree with that idea, but my biggest question is how much do you put in
the plugin and how much you leave in the theme. For instance if you want to
display some custom data in the theme(in places different than with
the_content()), but still have templates(so that appearance can be tweaked
from the theme) - do you define some of the functions in the theme, do you
just rely on the plugin being active.
Let me give you an example. You have custom fields(maybe the plugin you
created for the theme is creating them, or it's a third plugin that does
that). You want to display the information from those fields in the theme
and you want to make them easy to access, so you have a function that
returns an array with all of the data. Do I create a function in the theme,
that would pull the data(possibly relying on the plugin by using
function_exists() to prevent fatal errors), do I just use the plugin's
function in the theme?
I would appreciate personal experience, as well as references to articles
on the topic(or a link to a message on the list if it's been discussed
before).
Thanks,
Nikola
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Frank Bueltge
2014-01-23 20:00:59 UTC
Permalink
This is so much hepful in this question
http://wordpress.stackexchange.com/questions/73031/where-to-put-my-code-plugin-or-functions-php

Best
Frank
Post by Jamie Currie
Good question. I've got a few big projects that involve a lot of custom
data -- from meta fields to multiple custom tables. The main functionality
is used by a number of different clients, all with different themes and
different needs for how the data is displayed.
I've opted to go with a hybrid approach. Some data is accessible via
shortcodes with parameters. A list of accounts that is pulling data from a
custom table can be used in a page or template via something like
[LIST-ACCOUNTS type=' ' order=' '] etc. For parameters I try to follow WP
conventions for functions like get_posts().
Where possible, data is made accessible to the standard WP system. An
orders system that is using custom tables ultimately triggers
wp_insert_post() and adds a new instance of a custom post type. Some
filters allow any action taken on that order in the Dashboard to update the
custom tables as well. So then a designer can work with them just like any
other post.
You can ensure that a plugin (or multiple plugins) are active via your
https://github.com/thomasgriffin/TGM-Plugin-Activation
Jamie Currie
Founder / CEO
wunderdojo
wunderdojo.com
tel: 949-734-0758
1840 Park Newport, #409
Newport Beach, CA 92660
Master web & app developers
------ Original Message ------
Sent: 1/23/2014 11:34:44 AM
Subject: [wp-hackers] What goes in a theme and what goes in a plugin
Post by Nikola Nikolov
When I started working in web development, the work process of the company
I worked at was to put all of the code for a specific website in it's
theme. That means post types, custom fields, theme option interfaces,
shortcodes, etc.
I was recently introduced to the idea that this is definitely not a good
way of creating websites, simply because when the client decides to freshen
up the design of their website, they would either have to rewrite the
templates of the theme, or painfully extract all of the vital parts of the
code out of the theme(or create a child theme, but the themes we were
making were not really meant for using as parent themes).
Instead you should try to leave only the styling to the theme and have
everything else in one(or multiple?) plugins.
I agree with that idea, but my biggest question is how much do you put in
the plugin and how much you leave in the theme. For instance if you want to
display some custom data in the theme(in places different than with
the_content()), but still have templates(so that appearance can be tweaked
from the theme) - do you define some of the functions in the theme, do you
just rely on the plugin being active.
Let me give you an example. You have custom fields(maybe the plugin you
created for the theme is creating them, or it's a third plugin that does
that). You want to display the information from those fields in the theme
and you want to make them easy to access, so you have a function that
returns an array with all of the data. Do I create a function in the theme,
that would pull the data(possibly relying on the plugin by using
function_exists() to prevent fatal errors), do I just use the plugin's
function in the theme?
I would appreciate personal experience, as well as references to articles
on the topic(or a link to a message on the list if it's been discussed
before).
Thanks,
Nikola
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Nikola Nikolov
2014-01-23 20:17:55 UTC
Permalink
Jamie - yes shortcodes are definitely a good way to go about things. With
the option to use custom templates if the structure is more complicated. I
also like your approach for incorporating custom tables data with the
WordPress posts UI - it's some extra work, but it makes things more
consistent and that's quite important(especially with clients that have a
hard time to do something besides write an article :) )

Andrew - I usually stick to classes as well - it's just much more easy to
use them :) And with a function that returns the global object(the way that
bbPress does it with bbpress()) that gives easy access to class methods.

So, to kind of sum-up things:

- Bundle the plugin in the theme directory(like under /plugins/plugin.zip)
and use the TGM Plugin Activation in order to display a message to the
user, asking them to activate/install the plugin in order for the site to
function normally. This way even if the user accidentally deletes the
plugin, he'll still be able to restore it.
- Use plugin defined functionality(obviously not in a way that would break
everything - for instance using a plugin defined function in functions.php
on 'init') wherever is necessary.

Should I still try to avoid fatal errors when the plugin is missing,
despite of the fact that there will be a message asking the user to enable
the required plugin?
Post by Frank Bueltge
This is so much hepful in this question
http://wordpress.stackexchange.com/questions/73031/where-to-put-my-code-plugin-or-functions-php
Best
Frank
Post by Jamie Currie
Good question. I've got a few big projects that involve a lot of custom
data -- from meta fields to multiple custom tables. The main
functionality
Post by Jamie Currie
is used by a number of different clients, all with different themes and
different needs for how the data is displayed.
I've opted to go with a hybrid approach. Some data is accessible via
shortcodes with parameters. A list of accounts that is pulling data from
a
Post by Jamie Currie
custom table can be used in a page or template via something like
[LIST-ACCOUNTS type=' ' order=' '] etc. For parameters I try to follow WP
conventions for functions like get_posts().
Where possible, data is made accessible to the standard WP system. An
orders system that is using custom tables ultimately triggers
wp_insert_post() and adds a new instance of a custom post type. Some
filters allow any action taken on that order in the Dashboard to update
the
Post by Jamie Currie
custom tables as well. So then a designer can work with them just like
any
Post by Jamie Currie
other post.
You can ensure that a plugin (or multiple plugins) are active via your
https://github.com/thomasgriffin/TGM-Plugin-Activation
Jamie Currie
Founder / CEO
wunderdojo
wunderdojo.com
tel: 949-734-0758
1840 Park Newport, #409
Newport Beach, CA 92660
Master web & app developers
------ Original Message ------
Sent: 1/23/2014 11:34:44 AM
Subject: [wp-hackers] What goes in a theme and what goes in a plugin
Post by Nikola Nikolov
When I started working in web development, the work process of the
company
Post by Jamie Currie
Post by Nikola Nikolov
I worked at was to put all of the code for a specific website in it's
theme. That means post types, custom fields, theme option interfaces,
shortcodes, etc.
I was recently introduced to the idea that this is definitely not a good
way of creating websites, simply because when the client decides to freshen
up the design of their website, they would either have to rewrite the
templates of the theme, or painfully extract all of the vital parts of
the
Post by Jamie Currie
Post by Nikola Nikolov
code out of the theme(or create a child theme, but the themes we were
making were not really meant for using as parent themes).
Instead you should try to leave only the styling to the theme and have
everything else in one(or multiple?) plugins.
I agree with that idea, but my biggest question is how much do you put
in
Post by Jamie Currie
Post by Nikola Nikolov
the plugin and how much you leave in the theme. For instance if you want to
display some custom data in the theme(in places different than with
the_content()), but still have templates(so that appearance can be
tweaked
Post by Jamie Currie
Post by Nikola Nikolov
from the theme) - do you define some of the functions in the theme, do
you
Post by Jamie Currie
Post by Nikola Nikolov
just rely on the plugin being active.
Let me give you an example. You have custom fields(maybe the plugin you
created for the theme is creating them, or it's a third plugin that does
that). You want to display the information from those fields in the
theme
Post by Jamie Currie
Post by Nikola Nikolov
and you want to make them easy to access, so you have a function that
returns an array with all of the data. Do I create a function in the theme,
that would pull the data(possibly relying on the plugin by using
function_exists() to prevent fatal errors), do I just use the plugin's
function in the theme?
I would appreciate personal experience, as well as references to
articles
Post by Jamie Currie
Post by Nikola Nikolov
on the topic(or a link to a message on the list if it's been discussed
before).
Thanks,
Nikola
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Andrew Bartel
2014-01-23 19:54:05 UTC
Permalink
I basically follow the theme review ruleset. If it wouldn't be allowed on
a wp.org theme (cpts for example), I put it in a utilities plugin. If I
have a significant amount of custom functionality that cannot be fulfilled
by existing plugins, I break it out into a few different files and have on
a couple instances created parent utility classes that I extend.

Google something like functions.php vs plugin and you'll see a myriad of
articles on why.

As for your custom field example, I'd probably have a static function that
I can call. I prefer to use classes to avoid having to prefix *every* function
and *every *variable, but instead just the class name. And if you're going
to control the server, specifically the php version, you can use name
spaces too.

-Andrew
Post by Nikola Nikolov
When I started working in web development, the work process of the company
I worked at was to put all of the code for a specific website in it's
theme. That means post types, custom fields, theme option interfaces,
shortcodes, etc.
I was recently introduced to the idea that this is definitely not a good
way of creating websites, simply because when the client decides to freshen
up the design of their website, they would either have to rewrite the
templates of the theme, or painfully extract all of the vital parts of the
code out of the theme(or create a child theme, but the themes we were
making were not really meant for using as parent themes).
Instead you should try to leave only the styling to the theme and have
everything else in one(or multiple?) plugins.
I agree with that idea, but my biggest question is how much do you put in
the plugin and how much you leave in the theme. For instance if you want to
display some custom data in the theme(in places different than with
the_content()), but still have templates(so that appearance can be tweaked
from the theme) - do you define some of the functions in the theme, do you
just rely on the plugin being active.
Let me give you an example. You have custom fields(maybe the plugin you
created for the theme is creating them, or it's a third plugin that does
that). You want to display the information from those fields in the theme
and you want to make them easy to access, so you have a function that
returns an array with all of the data. Do I create a function in the theme,
that would pull the data(possibly relying on the plugin by using
function_exists() to prevent fatal errors), do I just use the plugin's
function in the theme?
I would appreciate personal experience, as well as references to articles
on the topic(or a link to a message on the list if it's been discussed
before).
Thanks,
Nikola
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Loading...