Community Forums › Forums › Archived Forums › Design Tips and Tricks › Adding widgets – How to protect from update
- This topic has 7 replies, 2 voices, and was last updated 10 years, 6 months ago by David Chu.
-
AuthorPosts
-
October 24, 2013 at 2:34 pm #68688DigitalEssenceMember
Hi,
I have added a new widget to the executive pro theme in front-page.php and it's working fine but how do I preserve my edit when it comes to updating the theme?
Ok I can run a diff on the files every time I update a theme but as time goes on this is going to become more and more tedious. I know that the themes don't update that often but am I missing a trick here?
What do you guys do to preserve your edits?
thanks,
October 24, 2013 at 2:55 pm #68696David ChuParticipantHi,
It is always intriguing to me how when just updating or changing a theme, one's widget settings may get torched. That's not surprising when moving a site to a different URL or something big like that (because of BLOB fields), but is a bit less expected when updating in place. But it does happen. I guess in the case of the latest crop of Genesis 2 versions of themes, maybe because they are using a completely different folder name, there may be some room for borkiness, to use a technical term.I'd suggest one of these - do a backup before updating, and then just reload if something is messed up by the update.
http://wordpress.org/plugins/widget-settings-importexport/
http://wordpress.org/plugins/widget-importer-exporter/I've used the first one a bunch of times, and it's good. The only drawback is that it won't work on a lower-than-php-5.3 site, although elsewhere on that site you can get the previous version.
Good luck, Dave
Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers
October 24, 2013 at 3:05 pm #68699DigitalEssenceMemberThanks David, those plugins look useful and I will be using them in the future.
How do I retain the code I added to the php files in the event of an upgrade?
Say for example I've added widgets to function.php and then the next time the theme gets updated it would overwrite my edits.
What I've done in the past is create template files with different names and place my functions in them and then call tehm from within WP.
But is this the easiest way to do it or am I missing something? It's almost as though I need a grandchild theme.
October 24, 2013 at 4:17 pm #68718David ChuParticipantHi,
That's a good question. I mostly make custom themes, so this doesn't effect me. ๐ But I also work on stock Genesis themes, and if someone's hiring me to do that, they are asking for changes!So in those cases, I will generally make a separate functions script, and a separate CSS file. I write a 1-liner in functions.php which is an include_once to pull in my custom functions file. This file, in turn, enqueues my custom CSS file. So the only change to the stock theme is a 1-liner! Very easy to recover from if an over-excited client upgrades without thinking. And you know as well as I do that they're not backing up anything. ๐ So probably no need for a grandchild theme, but the general concept of caution is a good one.
One can also write a plugin to handle all of that, so it's abstracted even more. Then you need to do some fancy footwork to make sure the plugin stuff is loaded after Genesis is initiated. I am experimenting with that, but it may be overkill for making a theme mod.
Cheers, Dave
Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers
October 25, 2013 at 10:05 am #68847DigitalEssenceMemberDavid,
I like your idea and have gone ahead and implemented it but...
I have some issues:
I thought it might be a good idea to move my edits from the functions.php file into my own custom_functions.php so they don't get overwritten in a later theme update.
I've added this to the top of my functions.php file right after the opening <?php
//Include our custom_functions.php
$found = locate_template( 'functions/custom_functions.php', TRUE, TRUE );I've got the custom file loading but it's giving me some strange results.
Firstly the following lines:
remove_action( 'genesis_after_header', 'genesis_do_nav' ); add_action( 'genesis_before_header', 'genesis_do_nav' );
Should remove the menu that comes after the header by default and add it before the header. This worked when it was in my functions.php file but since moving it to the custom file the add function works but the remove hasn't and I now see both menus.
Also
I'm trying to register a new widget:
genesis_register_sidebar( array( 'id' => 'meet-the-team', 'name' => __( 'Home - Meet The Team', 'executive' ), 'description' => __( 'This is the slider section on the home page.', 'executive' ), ) );
Again, this worked when it was in the functions.php file but now gives me:
Fatal error: Call to undefined function genesis_register_sidebar() in E:\wamp\www\borderschiropractic.dev\wp-content\themes\executive-pro\functions\custom_functions.php on line 13
Line 13 contains:
the genesis_register_sidebar( array(
Where have I gone wrong?
Thanks.
October 25, 2013 at 12:24 pm #68881David ChuParticipantHey,
I just ran all your code from my own custom-functions.php, and it worked flawlessly! So you're getting there! ๐That "locate_template" command is unfamiliar to me, interesting! Anyway, I don't have Executive, but I have things to look for:
Move your "locate" command down - it has to come after the init.php "gentleman, start your engines" bit. Otherwise, Genesis is not loaded, so you can't run any Genesis commands. That's why the genesis sidebar command barfed. In fact, sometimes due to timing of various commands, I will "include" my custom functions at the very end of functions.php.
Second: look in your functions.php file... chances are good that you have more of those NAV action menu lines (like yours above) somewhere in there. You may need to comment them out. And yes, this means another change besides the 1-liner. ๐ Some Genesis themes get all wild with functions.php commands.
Dave
Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers
October 25, 2013 at 1:06 pm #68888DigitalEssenceMemberDave you are a star!
I've moved it below the init.php call and it worked.
I used the locate_template as a post on StackOverflow said:
"Use locate_template() instead because a child theme can overwrite the loaded file then.
$found = locate_template( 'functions/my-custom-widget.php', TRUE, TRUE );
The first TRUE tells WordPress not only to search for the file but to load it actually. The second makes it a require_once call.The function return the path to the located file in case you need it later. If nothing is found it returns an empty string."
Again,
thank you for your help, it really is appreciated.
Hedley
October 25, 2013 at 1:49 pm #68894David ChuParticipantHedley,
Cool! Glad that helped. Since you get the whole functions thing, and even understand upgrade-proofing, Genesis should be a cakewalk for you. Once you get familiar with the hooks and filters, there'll be no stopping you.Yeah, of course I had to look up the function when I saw your code, so I'll put that in my toolbox.
I've been having fun moving my fave non-Genesis WP functions to a plugin. With some fiddling, one can do that with the Genesis stuff, too, and then you don't even need to care if someone goes wild and updates the theme. You'll be doing stuff like that soon, I'll bet. So far I still like my Genesis stuff in the theme itself.
Good meeting you, and enjoy your weekend,
Dave
Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.