Wordpress's functions.php - how do you work?
March 21, 2008 10:39 AM   Subscribe

What are the secrets to the efficient coding of my Wordpress core file using functions.php? Oh, and by the way, I'm a PHP idiot!

I am pretty new to Wordpress (hosted on my own server) and I am no PHP wizard by any stretch of the imagination, but I recognize the value of using the functions.php file to save little snippets of HTML code for use as plugins or widgets, rather than hard-coding the core files, thereby saving tons of trouble when switching themes or upgrading. I'm slogging through a PHP tutorial, but I think I'd learn quicker by taking some examples and pressing them into practical use immediately.

The thing is, with my limited current knowledge, I can't really parse the 'entries' in the functions.php file to use them as examples of how to write my own - there are an awful lot of parentheses, question marks, greater-thans and lesser-thans embedded within each other to tell where one function ends and the next begins. I just need to find out what needs to precede and proceed my HTML snippets to make it a discrete function, and then what code I need to put in the core file to call that function.

Any pointers or examples will be appreciated!
posted by DandyRandy to Computers & Internet (8 answers total) 1 user marked this as a favorite
 
A function would be coded as funtion my_function() {
…stuff here…
}


This all assumes that the whole thing is bracketed in the normal &lt?php…?> wrapper.

If you wanted to be ably to insert a chunk of static HTML, you'd do something like this:

funtion my_function() {
$output = "<p>Here's some HTML-formatted text!</p>";
echo $output;
}


This is pretty boring, of course. And unless you've got a plugin that will interpret php inside your blog entries, you can only use it in your templates, where you're better off just pasting the actual content in. And if you only want to add these snippets of text to your sidebar, you can just use WP's native widgets.
posted by adamrice at 10:53 AM on March 21, 2008


err, for funtion, read function. Apparently my functions were having to much fun.
posted by adamrice at 10:53 AM on March 21, 2008


well, from the Wordpress docs:
Functions File

A theme can optionally use a functions file, which resides in the theme subdirectory and is named functions.php. This file basically acts like a plugin, and if it is present in the theme you are using, it is automatically loaded during WordPress initialization (both for admin pages and external pages). Suggested uses for this file:

* Define functions used in several template files of your theme
* Set up an admin screen, giving users options for colors, styles, and other aspects of your theme

The "Default" WordPress theme contains a functions.php file that defines functions and an admin screen, so you might want to use it as a model. Since functions.php basically functions as a plugin, the Plugins Resources list is the best place to go for more information on what you can do with this file.
I've done 5 or 6 wordpress themes in the last couple of months and never needed to use the functions file. I don't really consider changing template code to be 'hard-coding' -- your template should still be portable between wordpress installs.

The functions file can be useful for organzing your template files so they are more readable, and for any tasks that need to be done more than once (ie, you output some type of block in your header, footer and sidebar).

Generally, I find it's nice to do as much inline as possible, when it comes to templates. If you do use functions, make sure they are discrete and well-named. I wouldn't want to dig around looking for where 'my_test()' was defined and what it returned, but if you have something like 'external_rss_display()', that's a useful abstraction.

So yeah, basically if you're not overly familiar with PHP, I would not use a functions file for your template -- inlining HTML will probably be more suitable for you in most cases.

what are you trying to do? Maybe we can help.
posted by fishfucker at 12:09 PM on March 21, 2008


"what are you trying to do? Maybe we can help."

I have a few gadgets on my blog like a Twitter feed, SiteMeter, Plugoo, my webcam feed, etc., all of which kindly provide a bit of HTML or Javascript to place in my HTML code.

I'm thinking that when it comes time to refresh my theme, it would be more efficient to make those little HTML/Javascript code snippets into the aforementioned function calls and point to those, rather than have to cut and paste those snippets (never mind remembering to do so for the several things I have already inserted).

Seems like if I can create a function for each of them in the functions.php file, it'd be much more portable.

I've been looking at the functions.php file that came with the theme that I'm using now, and I get some of it, but I think it's the various wrappers that adamrice refers to above that are throwing me off - there seems to be a few php wrappers nested within others, so it's hard to tell where one leaves off and one starts. Once I have the creation part down, where do I insert it in the functions.php file?

I was gonna paste the current contents of the file, but the preview scared me off!
posted by DandyRandy at 12:36 PM on March 21, 2008


All that stuff would be better off placed as WP widgets. For that matter, I'd bet there are packaged WP widgets for half the stuff you're talking about.

If you're determined to use functions, your functions.php file should only have one php wrapper around the whole thing (I think). And you can paste a new function in anywhere inside that wrapper, as long as it's not inside another function. But I'm not sure what kind of portability you'd be achieving.
posted by adamrice at 1:23 PM on March 21, 2008



I'm thinking that when it comes time to refresh my theme, it would be more efficient to make those little HTML/Javascript code snippets into the aforementioned function calls and point to those, rather than have to cut and paste those snippets (never mind remembering to do so for the several things I have already inserted).


ah, ok.

Here's what you would do:
function twitter_snippet() {

$html=  <<<EOT
PASTE HTML HERE 
Yadda, yadda, paste paste
EOT;

return $html;
}
Then, in one of your template files, you'd simply add
$lt;?php print twitter_snippet(); ?>
Alternatively, you could do this:
function twitter_snippet() {
print <<<EOT
PASTE HTML HERE
Yadda, yadda, paste paste
EOT;
}
and then in your template files you'd simply use:
<?php twitter_snippet(); ?> 
The latter method seems to be how the Kubrick author prefers to do it. My belief is that printing should only be done at the template level; not as part of a function.

A couple notes:
* it's important that you have the <<<EOT at the beginning, and the EOT; at the end. the ending EOT; MUST be on a line by itself. This is what is known as HEREDOC syntax and should make it simpler for you to paste in bulk content. EOT is an arbitrary token, you could also use <<&ltFISHFUCKERISAWESOME and FISHFUCKERISAWESOME, but why not save on typing? (and just use <<<FISHRAD)
posted by fishfucker at 3:01 PM on March 21, 2008


crap, I butchered a couple <s, but you get the idea, i hope.
posted by fishfucker at 3:02 PM on March 21, 2008


This is very helpful to create new functions. But how about over-riding existing functions? For example, I would like to change the automated widget titles so that the markup outputs '' instead of ''. Does anyone know how to handle this in functions.php?
posted by marc0047 at 5:27 PM on August 21, 2008


« Older Can we share our peep fight with the world?   |   How to stop worrying, and move on Newer »
This thread is closed to new comments.