Community Forums › Forums › Archived Forums › General Discussion › Functions = Speed issues?
- This topic has 19 replies, 4 voices, and was last updated 10 years, 8 months ago by
boldplan.
-
AuthorPosts
-
July 5, 2014 at 12:38 am #113021
boldplan
ParticipantWhile I enjoy Genesis themes, I've found it difficult to work with them, especially with additional plugins, e.g. Advanced Custom Fields and the lack of ease of being able to place things easily in specific templates. But another issue occurred to me. On every page load, WordPress loads the functions.php file. It's getting progressively larger each time I need to change something -- even mundane things such as the insertion of a footer, the changing for the name of breadcrumbs, etc. So.... I noticed the following.
Let's say I make a change ONLY to archive pages. With genesis, I have to add my modification to the functions.php file and it will load on every page and then not be used since it's inapplicable. It seems WordPress has to work much harder to rifle through all of functions - including those not applicable to a certain page. If this was using straightforward templating, the code would only be seen and executed on pages IF and when they are called. I'm wondering about thoughts on this.
July 5, 2014 at 6:33 am #113035DTHkelly
MemberGlad I use Dynamik (and Genesis extender with Genesis childthemes) for "straightforward templating." Works with ACF, too.
July 5, 2014 at 8:30 am #113045Summer
MemberOkay this is weird... my post is being eaten by the forums? Even when I remove the link to the WP Codex?
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After DarkJuly 5, 2014 at 8:42 am #113047Summer
MemberOkay, I'm going to leave out my entire explanation and just include the link.
Genesis follows the template structure that WordPress themes are built on, it's just that those templates may not exist by default in particular child themes, but the framework has them and is doing the heavy lifting with them.
Nothing's stopping you from creating those additional templates in the child theme you're using if you need to do customizations for them. In fact, you should.
http://codex.wordpress.org/Template_Hierarchy is your friend. Run with it, have fun with it.
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After DarkJuly 8, 2014 at 7:38 am #113044Summer
MemberIf you have functions that don't apply to the entire site, it's better to add them to the individual templates instead of to functions.php.
If you have something that only applies to archive pages, then that should be in archive.php. If you have something that applies to single posts, then that should be in single.php. Just because those templates don't exist in the child theme you're working with doesn't mean you can't add them so you can customize them to meet your specific needs.
That's basic WordPress templating, and I've never understood why so many people throw the kitchen sink into functions.php without even considering using the template hierarchy that WordPress is built on for that very reason. Seems like a really weird Genesis blind spot to me, and I haven't the faintest idea why so many Genesis users have that particular affliction 🙂
http://codex.wordpress.org/Template_Hierarchy is your friend. Run with it, have fun with it.
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After DarkJuly 8, 2014 at 7:38 am #113046Summer
MemberFine. Here's what I said, without the link 🙂
If you have functions that don't apply to the entire site, it's better to add them to the individual templates instead of to functions.php.
If you have something that only applies to archive pages, then that should be in archive.php. If you have something that applies to single posts, then that should be in single.php. Just because those templates don't exist in the child theme you're working with doesn't mean you can't add them so you can customize them to meet your specific needs.
That's basic WordPress templating, and I've never understood why so many people throw the kitchen sink into functions.php without even considering using the template hierarchy that WordPress is built on for that very reason. Seems like a really weird Genesis blind spot to me, and I haven't the faintest idea why so many Genesis users have that particular affliction 🙂
Go to the WordPress Codex, search for Template Hierarchy, and have fun with making the different templates you need.
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After DarkJuly 8, 2014 at 7:39 am #113043Summer
MemberIf you have functions that don't apply to the entire site, it's better to add them to the individual templates instead of to functions.php.
If you have something that only applies to archive pages, then that should be in archive.php. If you have something that applies to single posts, then that should be in single.php. Just because those templates don't exist in the child theme you're working with doesn't mean you can't add them so you can customize them to meet your specific needs.
That's basic WordPress templating, and I've never understood why so many people throw the kitchen sink into functions.php without even considering using the template hierarchy that WordPress is built on for that very reason. Seems like a really weird Genesis blind spot to me, and I haven't the faintest idea why so many Genesis users have that particular affliction 🙂
See http://codex.wordpress.org/Template_Hierarchy -- run with it, have fun with it.
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After DarkJuly 12, 2014 at 2:57 am #113999boldplan
ParticipantThanks for the detailed reply.
Regarding the suggestions of the way things are handled - I agree with you regarding the wondering why it's always "edit your functions.php file." For example - I need to revise my footer so it appears the way it should. Right now in order to get rid of accreditation or to change it, e.g. powered by Genesis or whatever was there, the solution was to put it into functions.php. This seems to be incredibly inefficient. The same goes for every little ticky-tack change you need to make, e.g. You Are Here in searches, etc. It's always yet another function to add rather than making changes in template files or directly to PHP files.
So now the question becomes, what do I do next in Genesis to create a custom footer template? If you look at the Genesis parent, the footer.php is as follows:
<em>genesis_structural_wrap( 'site-inner', 'close' ); echo '</div>'; //* end .site-inner or #inner do_action( 'genesis_before_footer' ); do_action( 'genesis_footer' ); do_action( 'genesis_after_footer' ); echo '</div>'; //* end .site-container or #wrap do_action( 'genesis_after' ); wp_footer(); //* we need this for plugins ?> </body> </html></em>
So... how does one create a template, e.g. a customized formatted footer area, and not mess up everything else? This would definitely be preferable.
PS - Yes, weird stuff was happening with your posts!
July 12, 2014 at 7:56 am #114018boldplan
ParticipantAnother issue - I want to remove the date from the template. This too needs to be handled by another Genesis function. As a result, you end up with a multitude of functions to do what a simple template would with far greater efficiency. I'm a little stuck on why I'm preferring to use the system and wondering as to the best way to simply template things again like single.php etc.
July 12, 2014 at 8:49 am #114019Summer
MemberYou want to remove the date from the nav menu extras, or you want to remove the date from the post info?
If you're talking about the post info date, and you are talking about the credit tags in the footer data, have you tried the Genesis Simple Edits plugin for controlling the post info, post meta, and footer data?
If you had wanted to change things with the footer widgets, then defining or removing those should go in functions.php since the footer appears on all pages, but since you're just fiddling with the various data tags, Genesis Simple Edits should take care of all the things you want to change in one place.
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After DarkJuly 12, 2014 at 9:01 am #114021Summer
MemberAs for the other changes, the benefit of Genesis is to be able to use functions, hooks and filters to make changes to the layout structure of your theme, without having to modify entire templates of mixed PHP, HTML and js like many standalone templates have to do.
You'd have a template file with one or two lines of code in them, an action hook instead of a few hundred lines of raw code, which kinda cuts down on clutter and tends to mean less code for someone to maintain or modify in the long run. At least that's how I see it.
Everything else is learning which changes to make using plugins and which changes to make to templates... the ones you described here don't need to be template modifications. Even changing the page style of individual pages or posts doesn't have to be done in functions or templates.
Using a template generally is for detailed customizations like modified divs or multiple custom loops, and typically aren't needed for the cosmetic "ticky tack" changes you're trying to do. Not typically, anyway.
and yes, I think those first few posts of mine got caught in a spam trap, and released a couple days afterwards, which explains the duplicates that appeared out of thin air 🙂
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After DarkJuly 12, 2014 at 11:09 am #114032boldplan
ParticipantThanks for the detailed replies:
(1) Yes, I'm aware of the Simple Edits plugin. But it's actually just a way to insert functions into functions.php without having to copy and paste. It's convenience and unnecessarily adds to the load time in the same manner.
(2) The issue isn't an action hook - it's a lack of being able to gain access to the variables themselves to custom code. Almost all the Genesis themes, as a result, look the same way. For example, on my single.php or post file I want the header to look like this:
<h1>Thoughts on <title here> by John</h1>
I don't think there is a way to do this properly using hooks and probably best to just make a template edit which results in no need for back ending your way into these edits. In addition, instead of "leave a reply" before the comment fields I'd like to insert a form and something else in there, but I don't see an easy way to accomplish this since the form isn't there and, again, would involve inserting code that exists in some other file and using it as some type of an include. I don't recall seeing a tutorial or information about this either.
July 12, 2014 at 12:07 pm #114040DTHkelly
MemberBased on #114032 , you should consider Cobalt Apps (Genesis Extender). I have Extender as well as their Dynamik ChildTheme (on different wp installations).
Extender lets you create Custom Hook Boxes [custom content areas for php, html, css, widget areas, etc.) which would answer #114032.
July 12, 2014 at 12:27 pm #114047boldplan
ParticipantKelly - thanks. I've seen extender and it adds a lot of functionality. But I don't think it replaces the content, just puts in a content area into a hook area. This is key.
Of note, I think it works the same way as all the other plugins. It creates unnecessary functions rather than having a template created that needs to "erase" what was previously stated. For the most part, most of these plugins merely add the functions.php code through a more manageable in a plugin. So instead of creating a simple function to say "echo this content in this hook area", the plugin does that for you.
July 12, 2014 at 1:54 pm #114060Summer
MemberYou might be overthinking the problem and rejecting the solutions because of that.
Everything you do to customize a child theme is going to rely on functions and filters. There's no way to completely avoid functions.php because that's what hooks the theme into the framework. They aren't unneccesary, and I really don't think they add as much to load time as you are arguing they do.
You seem to be obsessing over measurements of time that are negligible, without having explained why it's such an important factor for you... we're not talking the difference between a 3-second and a 4-second site load time; what we're talking about here is milliseconds or nanoseconds.
Anyway, as long as you're aware that changing what the post title displays on the page will not change the permalink or the title as displayed in the page title (things that matter to search engines), this snippet might help you:
//* Add the post title filter add_filter( 'genesis_post_title_text', 'ct_modify_post_title' ); function ct_modify_post_title($post_title) { $post_title = 'Thoughts on ' . $post_title . ' from John'; return $post_title; }
It's sorta-tested, and when you put it in single.php (followed by a closing
genesis();
call), it will change the post title displayed for readers and hook in the rest of things Genesis and your template rely on, but as I said it will not change the other uses of the post title.If I misunderstood what you were trying to do, maybe this is more of a disconnect about what Genesis does and can do (something that Genesis' major lack of documentation for non-coders or even beginning coders bugs me about).
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After DarkJuly 12, 2014 at 5:06 pm #114082boldplan
ParticipantThanks -- ah yes, that's right.... you have to filter everything out and replace, sort of like glorified find/replace. And while this works, I can't get the modification to only appear on the home page of a certain blog properly. I've been using the standard codex conditions, (is_single(), !is_home(), !is_front_page() ) but the front page no longer displays the title at all although it does appear modified on post pages. The back ending into these things makes it a huge pain to work with and a visual nightmare to read templates and edit. It's like spaghetti templates, especially if you want to find later where things are.
While it may be a few milliseconds for each function, they add up very quickly. I already have almost 3 dozen modifications and a functions.php file that is 500 lines and growing. Add in multiple users accessing the site and this adds up significantly. Honestly, I don't think this is trivial and can easily add a half second to page load for every user.
July 12, 2014 at 6:18 pm #114086Summer
MemberThat's because different pages require different templates, if you choose to customize them differently. That's a WordPress thing, not a Genesis thing.
You had said that you wanted to modify the display of the title of a single post, which is why I said customize a single.php, but now you're saying you want that customization to appear on the home page, which in your case is just a different way to list an content archive.
That code wouldn't go in single.php to achieve that... you'd have to put it in front-page.php or if it is going to appear modified in more than 2 locations, then it would better off in functions.php, so that you wouldn't have to put the same snippet in 3-4 different templates.
That's what functions.php is best for, actions that are needed globally across the entire theme, and if you're changing your post title mod to be in more than one place, then it needs to be where it can be executed globally.
What has page speed testing shown you for your site? I don't think it's anywhere near as bad as you're afraid it might get. If you do think it could get that bad, you might be better off hiring a designer who's familiar with Genesis and coding to examine and tweak a few things for you.
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After DarkJuly 14, 2014 at 6:08 pm #114313Gary Jones
Member+1 for everything Summer has said here.
I just run Xdebug profiler on my local VVV site, with a custom Genesis child theme active. Here's a snap of the results:
https://www.dropbox.com/s/ro62amgjcwbl62q/Screenshot%202014-07-15%2000.43.35.png
Important things to note:
* There were 1412 *different* functions called. Only two Genesis ones made the top of the list in my viewport.
* Look at the invocation counts for the ones you can see - some get called thousands of times, and one (and one further down the list) get called 11,000+ times. Making your site call one extra customisation function on your site therefore becomes negligible, assuming it's written correctly. Let's take the code snippet that was already in this thread (sorry Summer!)://* Add the post title filter add_filter( 'genesis_post_title_text', 'ct_modify_post_title' ); function ct_modify_post_title($post_title) { $post_title = 'Thoughts on ' . $post_title . ' from John'; return $post_title; }
That assigns a value to a variable, and then returns the variable. Why not just return the value? Not only will it save (picoseconds? of) time, it will make the code cleaner too:
add_filter( 'genesis_post_title_text', 'ct_modify_post_title' ); function ct_modify_post_title( $post_title ) { return 'Thoughts on ' . $post_title . ' from John'; }
The expensive functions are ones that access the database or the file system. You probably won't be doing the latter, and the former has caches at the WP and Genesis levels. A simple thing like returning a different string really is nanoseconds. If you want to test, then create a function that does the filter, and another which assigns the current microtime(true) to a variable, then run a for loop from 1 to 100,000, and then subtract the previous time variable from the new current microtime(true). The result will be the time for 100,000 executions, so assuming you've not got some caching going on, you can divide it by 100,000 to get the approximate time it takes for one call.
if it is going to appear modified in more than 2 locations, then it would better off in functions.php, so that you wouldn’t have to put the same snippet in 3-4 different templates.
This. This is the benefit of Genesis. The abstraction of data from logic is what makes Genesis so powerful. Why edit several templates to add a Google Ad into the same spot on each type of page (archive, home, single post, single page etc.) when you can do it once, and have it apply multiple times automatically? If you don't want it in all the places, then use the template hierarchy or conditionals that you've already discussed.
If you don't want a long functions file, then split it up into several logical files that make sense to you, and include them in functions.php file if you wish.
My plugin https://github.com/GaryJones/genesis-ignore-deprecated can help save a small bit of time as well, as it tells Genesis not to load the deprecated functions file, which stops 74 deprecated functions being defined unnecessarily.
In the grand scheme of things, your choice of theme, and how it's built is not even close to being the most important factor for the speed of your site. Server hardware, Nginx, Varnish, Memcache, HHVM vs mod-php vs php-fcgi, DNS, CDN and browser caches are all going to make far more impact on how quick your site appears to be, instead of worrying about a few extra function calls that helps to customise the theme.
WordPress Engineer, and key contributor the Genesis Framework | @GaryJ
July 14, 2014 at 6:24 pm #114318Summer
MemberLOL, no apologies needed, Gary 🙂
I'm sure I have another few functions in any of my custom and customized themes that could use a little streamlining!
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After DarkJuly 16, 2014 at 11:16 am #114546boldplan
ParticipantThanks guys - I really appreciate the brutal details. I'm getting a better understanding of where you're coming from. I'll probably end up hiring someone for the more ambitious work shortly.
Gary - truly appreciated your very detailed explanation. I guess that my discomfort is that fine tuning areas is not easy to manage in a functions.php and I hate adding yet more plugins -- but it's probably the only practical solution. I run a very large Genesis based multisite which has about a dozen multisites because it's the only practical way to manage content areas, e.g. professional journal, the student journal and company blog, etc. With template files It's much easier for me to see how all blogs are treated as they are in one template and reading is linear. Functions are difficult to visualize and would require some real organization. I want to also insert a form on the bottom of every post in some but not all multisites (I guess is genesis_after_content). I also want to insert a form which includes some php and javascript to be executed. I guess that with Genesis, it's made far more for coders and not hackers and I'll need to reenter my coding days to learn all the syntax and deal with the learning curve.
Yes, there are advantages if you can place once and have it serve everywhere. I guess no one size fits all and I'll have to take the pros with the cons! Thanks again for your patience. I can't tell you how much I appreciate both of your assistance.
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.