0

I am an experienced PHP developer, but new to WordPress. I created a site and added some custom JavaScript, CSS, and a number of shortcodes. My CSS was just added to style.css (The default when I clicked on Appearance->Editor in the sidebar) and the hooks to my JavaScript as well as my shortcodes were in my theme's functions.php file.

Naturally, the theme updated and all my changes were wiped. Thankfully I had backups saved, so I was able to restore my custom code, but I really want all my custom code to be completely independent of the theme. What's the proper way to handle shortcodes, css, and script/style enqueing so that theme changes won't remove them?

I found a number of blogs recommending using child themes, but as I understand it that's not truly theme agnostic, for lack of a better term. I want my shortcodes and JS libraries to remain in place no matter if the theme updates, wordpress updates, or I change themes completely.

I also made a small update to my theme's header.php file (I wanted the ability to add a quote to my site, above the menu items, and reposition where the site logo was displayed) I'm imagining this will be more difficult to maintain since, unlike CSS and shortcodes, it is actually changing the content, not adding to it. Is there any way to protect/maintain changes to a theme's header.php as well?

  • 3
    Hi. Then creating a plugin will solve your problems, if you want to preserve your stuff even if you change the theme. – Jack Johansson Jun 16 '17 at 16:31
  • 1
    As Jack said, for any functionality a plugin is your move here. If you're going to code any CSS or do any template changes, make a child theme so updates don't wipe you out. I always make a child theme, but anything not specific to that theme (to templating) gets a site-specific plugin. – hwl Jun 16 '17 at 20:17
1

I want my shortcodes and JS libraries to remain in place no matter if the theme updates, wordpress updates, or I change themes completely.

Normally the suitable place for such in WP would be a plugin or as variation of must use plugin.

More elaborate site might also use some other (generic PHP) methods, such as Composer packages.

Is there any way to protect/maintain changes to a theme's header.php as well?

There is no way to "protect" a change to a template file. This should definitely be handled by a child theme — copy modified template to it and it will be picked over parent template.

If theme offers sufficient hooks it might be possible to inject logic with code, rather than making template changes. It depends.

1

It's like @hwl said. And because you are planing to change the template file header.php, a child-theme would be the way to go in your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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