Support » Themes and Templates » Confused so as to how to call the additional template files I made.

  • Iva

    (@supersonicsquirrel)


    I’m creating a couple of themes for myself and potential clients and currently, I’m altering them according to WordPress coding standards. I’m, however, confused by what I read on:

    http://codex.wordpress.org/Theme_Review#Code_Quality

    and it directed me to:
    http://justintadlock.com/archives/2010/11/17/how-to-load-files-within-wordpress-themes

    …and it seemed that the statements were remarkably different.

    What I’m trying to do is include php files with functions in them (some native, some self-made) to display author information and links to social bookmarking sites.

    Say that I named a file social.php and placed it in inc/ subfolder of my theme? Should I use:
    <?php include (TEMPLATEPATH . '/inc/social.php'); ?>

    or

    <?php get_template_part( 'social' ); ?>

    (assuming that I placed social.php in the theme’s directory and not its subdirectory)

    or

    <?php locate_template( 'inc/social.php' ), true, false ); ?>

    I wrote the option of adding this stuff to functions.php as a bad coding practice (am I wrong) and I would like to re-use this social.php file on single.php, page.php and perhaps attachment.php, which is why I made it.

    Hope my question is not confusing, I would just like to know what’s the recommended way to do this. 🙂

Viewing 14 replies - 1 through 14 (of 14 total)
  • Chip Bennett

    (@chipbennett)

    Theme Review Admin

    What I’m trying to do is include php files with functions in them (some native, some self-made) to display author information and links to social bookmarking sites.

    This is somewhat confusing. Are you including function definitions, or markup/output? Function definitions and markup/output should not be mixed within files.

    Include files with function definitions into functions.php, using include()/require().

    Include files with template markup/output – i.e. template part files into other template files using get_template_part() or locate_template().

    Something like social.php also suggests that the content might be a prime candidate for being packaged up as a custom Widget.

    Iva

    (@supersonicsquirrel)

    I apologise for confusion, it was early in the morning and I eventually gave up. Let’s see if it makes more sense now…

    The file itself looks like this:

    <div id="share">
    	<ul>
    		<li><a href="javascript:window.external.AddFavorite('<?php bloginfo('name'); ?>',%20'<?php bloginfo('name'); ?>')"><img src="<?php bloginfo('template_directory'); ?>/images/icons/heart.png" alt=""/></a></li>
    		<li><a href="http://www.facebook.com/share.php?u=<?php the_permalink();?>" onclick="return fbs_click()" target="_blank"><img src="<?php bloginfo('template_directory'); ?>/images/icons/facebook.png" alt=""/></a> </li>
    		<li><a title="Stumble it!" href="http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/icons/stumbleupon.png" alt=""/></a></li>
    		<li><a title="Add To Google Bookmark!" href="http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk=<?php the_permalink();?>&title=<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/icons/google.png" alt=""/></a></li>
    		<li><a title="Digg it!" href="http://digg.com/submit?phase=2&url= <?php the_permalink();?>&title=<?php the_title();?>"><img src="<?php bloginfo('template_directory'); ?>/images/icons/digg.png"alt=""/></a></li>
    		<li><a href="http://del.icio.us/post?v=4&noui&jump=close&url=<?php the_permalink();?>&title=<?php the_title();?>"><img src="<?php bloginfo('template_directory'); ?>/images/icons/delicious.png"alt=""/></a></li>
    		<li><a href="http://technorati.com/faves?add=<?php the_permalink();?>"><img src="<?php bloginfo('template_directory'); ?>/images/icons/technorati.png" alt=""/></a></li>
    	</ul>
    	<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="<?php echo $funkyblog_twitter;?>">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script><br />
    </div>
    <div class="clear">&nbsp;</div

    >
    And technically, I assume it IS a template part, but a template part that I made myself.

    The function that calls it, at this point, looks like this:

    <!-- Social Networking Bookmarks -->
    <?php $show_social = get_option('funkyblog_show_social'); ?>
     <?php if ($show_social=='true') : ?><?php include (TEMPLATEPATH . '/inc/social.php'); ?>
    <?php endif; ?>
    <!-- END Social Networking Bookmarks -->

    So, the file is called only if the option to show the author biography is true. It works the way it is now, but I read about file inclusion on the Codex and it said that one should not use them that way, “unless they absolutely have to”, which made me puzzled.

    I would just like the theme to meet all standards and I’m a designer, not a programmer.

    It cannot be a widget as it appears right behind the loop, on the single Post/Page view. Otherwise, I would’ve packed it as a widget. 🙂

    Chip Bennett

    (@chipbennett)

    Theme Review Admin

    First, given the context, this:

    <?php include (TEMPLATEPATH . '/inc/social.php'); ?>

    Should be this:

    <?php locate_template( 'inc/social.php' ); ?>

    …because you’re including a template part file, within a template file.

    Second, it would be much better to wrap up that otuput in social.php as a custom function, e.g.:

    function funkyblog_social_links() { ?>
    	<div id="share">
    	<ul>
    		<li><a href="javascript:window.external.AddFavorite('<?php bloginfo('name'); ?>',%20'<?php bloginfo('name'); ?>')"><img src="<?php bloginfo('template_directory'); ?>/images/icons/heart.png" alt=""/></a></li>
    		<li><a href="http://www.facebook.com/share.php?u=<?php the_permalink();?>" onclick="return fbs_click()" target="_blank"><img src="<?php bloginfo('template_directory'); ?>/images/icons/facebook.png" alt=""/></a> </li>
    		<li><a title="Stumble it!" href="http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/icons/stumbleupon.png" alt=""/></a></li>
    		<li><a title="Add To Google Bookmark!" href="http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk=<?php the_permalink();?>&title=<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/icons/google.png" alt=""/></a></li>
    		<li><a title="Digg it!" href="http://digg.com/submit?phase=2&url= <?php the_permalink();?>&title=<?php the_title();?>"><img src="<?php bloginfo('template_directory'); ?>/images/icons/digg.png"alt=""/></a></li>
    		<li><a href="http://del.icio.us/post?v=4&noui&jump=close&url=<?php the_permalink();?>&title=<?php the_title();?>"><img src="<?php bloginfo('template_directory'); ?>/images/icons/delicious.png"alt=""/></a></li>
    		<li><a href="http://technorati.com/faves?add=<?php the_permalink();?>"><img src="<?php bloginfo('template_directory'); ?>/images/icons/technorati.png" alt=""/></a></li>
    	</ul>
    	<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="<?php echo $funkyblog_twitter;?>">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script><br />
    	</div>
    	<div class="clear">&nbsp;</div
    <?php }

    …which you would define in functions.php, and then use in your template like so:

    <!-- Social Networking Bookmarks -->
    <?php $show_social = get_option('funkyblog_show_social'); ?>
     <?php if ($show_social=='true') : ?><?php funkyblog_social_links(); ?>
    <?php endif; ?>
    <!-- END Social Networking Bookmarks -->

    …which is probably the route I would go.

    Iva

    (@supersonicsquirrel)

    So, the first thing is what I assumed (despite the Codex article being really, really confusing). As for the second, wouldn’t that make the whole thing less editable to someone who would like to add more social links?

    Thank you so much for clearing these things up for me and having patience. I was comparing themes with a lot of functionality from various sources and most of them were pretty contradictory and done in some Mickey Mouse way.

    Chip Bennett

    (@chipbennett)

    Theme Review Admin

    If you want the end user to be able to add more social links, the best way would be to implement either a custom Widget, or to add Theme options to add additional links to the function.

    If you go the function route, you could put the default links in an array, and make the function filterable, so that the end user, using a Child Theme or a custom Plugin, could filter the array to add more links.

    And, glad I could help. I know that the Codex can indeed be quite confusing sometimes!

    A matter of choice, I would use an options page with a template part as the template part produces the markup.


    <!– Social Networking Bookmarks –>
    <?php if ( get_option(‘funkyblog_show_social’) ) get_template_part(‘social’); ?>
    <!– END Social Networking Bookmarks –>

    HTH

    David

    Iva

    (@supersonicsquirrel)

    Chip, thank you so much. 🙂
    Once again, given its position, I doubt it can be a widget, but I am considering making one for the links to user’s social profiles.

    adeptris, I assume these are up to the latest standards? I did read that the tutorial on options pages from WP-Shout was out of date and I definitely want to do things the best way possible. Thanks. 🙂

    Iva

    (@supersonicsquirrel)

    Also, Chip, from what I understood here:
    http://wordpress.stackexchange.com/questions/13539/what-are-security-best-practices-for-wordpress-plugins-and-themes

    …looking at the way either you or the authors of Coraline have coded functions.php and the options pages would be a good way to improve my potentially unsafe and outdated options panel?

    Chip Bennett

    (@chipbennett)

    Theme Review Admin

    I’ve done my best to code Oenology to best practices. It isn’t (yet) a great resource for advanced sanitizing/validation, because it doesn’t (yet) have any options that require arbitrary user input.

    I’m thinking of writing a reference blog post, listing all of the available functions for escaping on output, and sanitizing on input.

    Iva

    (@supersonicsquirrel)

    Well, if you’re doing so, there’s at least one person whose confusion would be cleared bigtime. 🙂

    (Not to mention that I’ll have a proper reason to explain people not to mess with the template files, as security standards change and once I update the theme, they’ll lose all of their hardcoded customisations!)

    Hi Iva,
    I now use a mix of two tutorials to create a tabbed options page I found the tabbed options page tutorial, but there were no support files as a download so I created a set as a child theme.

    I have a dark theme ‘Atmosphere 2010’ on extend based on twenty ten that I created in November the end of last year, now with over 23000 downloads.

    A part of getting it accepted was to have the options page functions “up to date” using a serailized option, this is a free theme has the social media built in including all the icons and files that you can strip out and use, and it even has a show option for the social media icons!

    HTH

    David

    Hi Chip,
    Nice theme I like the show and hide!

    My theme is based on twenty ten, I did not update mine as a know a lot of people have modified it, and it would be over written if I was to release an update, I see you have updated your theme any issues with complaints about files being overwritten?

    Iva sorry to ask in your topic!

    Regards

    David

    Chip Bennett

    (@chipbennett)

    Theme Review Admin

    My theme is based on twenty ten, I did not update mine as a know a lot of people have modified it, and it would be over written if if I released an update, I see you have updated your theme any issues with complaints about files being overwritten?

    I’ve heard no complaints. But, if I were to hear such complaints, my response would be: use a Child Theme, or fork.

    I’ve tried to make Oenology as Child-Theme-friendly as possible (and in fact use it as a base for some other sites I maintain). I wrote a tutorial explaining how to create a Child Theme. Anyone who chooses to modify the “core” package does so at their own risk.

    Iva

    (@supersonicsquirrel)

    David, no need to be sorry for anything, it’s all similar anyway and since I’ve learnt a lot in the last 20 hours solely by reading this thread, I’d be crazy to have a problem with it.

    My theme is definitely meant to be standalone, not a child.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Confused so as to how to call the additional template files I made.’ is closed to new replies.