Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I recently spotted the code for a wp-lunch.php file intended for a WordPress template, what would this look like if following the proper WordPress coding standards?

wp-lunch.php

<?php if ( current_user_can('has_sandwich') ): ?>

    <?php get_sandwich_header(); ?>

        <?php while( has_filling() ): the_filling(); ?>

            <?php get_sandwich_part( 'thick_layer',
    get_filling() ); ?>

        <?php endwhile; ?>

    <?php get_sandwich_footer(); ?>

<?php endif; ?>
share|improve this question
    
Excellent question, let the games begin! –  userabuser May 24 at 11:27
    
I think the perfect question to ask here, how should the_filling() look like –  Pieter Goosen May 24 at 13:18
    
What do all those WP devs do on weekends? ;) –  ialocin Jun 2 at 18:38

3 Answers 3

up vote 5 down vote accepted

What happen if the user don't have capability to eat sandwich? WSOF?

If I'd want to follow average default themes templates, I'd go for

// eat-sandwich.php (as @Rarst said avoid wp-lunch.php as it's not part of WP core)

get_header( 'sandwich' );

if ( current_user_can( 'eat_sandwich' ) ) {

  get_template_part( 'eat-sandwich', 'content' );

} else { // user can't eat sandwich. An apple?

  $alternative = apply_filters( 'alternative_to_sandwich', 'apple' );

  if ( 'sandwich' == $alternative ) {
     // No sandwich allowed!
     $alternative = 'apple';
  }

  get_template_part( "eat-$alternative", 'content' );

}

get_footer( 'sandwich' );

And then

// eat-sandwich-content.php

$fillings = get_fillings_query(); // in functions.php

if ( $fillings->have_posts() ) : while ( $fillings->have_posts() ) :

   get_template_part( 'filling', get_filling_type() );

endwhile;

wp_reset_postdata();

else :

  _e( 'Sorry, no fillings found. Eating an apple may help to stop hunger.', 'txtdomain');

endif;
share|improve this answer
    
This query would break if there was no bread, since bread is not considered a filling and an essential part of a sandwich. I recommend adding get_ingredients(); instead of get_fillings_query(); of which bread and filling are a part of. Also the filling should have a front facing JSON API;) –  Wyck May 24 at 17:02
<!-- file shouldn't be named wp-lunch.php as it's not part of WP core -->

<?php if ( current_user_can( 'eat_sandwich' ) ): // more specific verb makes more sense to me ?>

    <?php get_header( 'sandwich' ); // native function accepts type argument ?>

    <?php while ( have_fillings() ): the_filling(); // maybe native API, but feels acceptable wrapper for semantics ?>

        <?php get_template_part( 'filling', get_filling_type() ); // native API, what would be `thick_layer` base? ?>

    <?php endwhile; wp_reset_postdata(); // reset $post global ?>

    <?php get_footer( 'sandwich' ); // native function accepts type argument ?>

<?php endif; ?>

Spacing adjusted for coding style, etc.

Sandwich template with bit of Twig, eaten on Meadow will look something like:

{% if ( current_user_can( 'eat_sandwich' ) ) %}

    {% include 'header-sandwich.twig' %}

    {% loop fillings %}

        {% include 'filling-' ~ get_filling_type() ~ '.twig' ignore missing %}

    {% endloop %}

    {% include 'footer-sandwich.twig' %}

{% endif %}
share|improve this answer
3  
All that <?php give me chills. –  G. M. May 24 at 11:40
7  
@G.M. I thought about introducing Mustache template, but who wants mustache in their sandwich? –  Rarst May 24 at 11:41
4  
If I found a Twig in my sandwich I could handle it, but not a Mustache. –  userabuser May 24 at 11:48

No need for all of the opening and closing delimiters or clear linespaces when already indented:

<?php
if ( current_user_can( 'has_sandwich' ) ) {
    get_sandwich_header();
    while ( has_filling() ) {
        the_filling();
        get_sandwich_part( 'thick_layer', get_filling() );
    }
    get_sandwich_footer();
}

Should probably have a reset filling data afterwards too...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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