Skip to content

Instantly share code, notes, and snippets.

@efuller
Forked from kellenmace/output-buffering-example.php
Last active March 1, 2017 18:05
Show Gist options
  • Save efuller/2d3a82e94abcfb945fc2ec5fadfa50c1 to your computer and use it in GitHub Desktop.
Save efuller/2d3a82e94abcfb945fc2ec5fadfa50c1 to your computer and use it in GitHub Desktop.
Template tags and Output Buffering at WDS

Information

This exploded into a big discussion about the creation of template tags and why you don’t have to always create a get and a do.

Example, in a loop you run do_my_thing(), the function do_my_thing() does not need to buffer the output if you’re just going to echo it out in the loop, so ob_start is not needed!

If, later, you find you need to get that string you can write a simple getter: https://gist.github.com/cd948f2b9f83625e09518c531d272d1c#file-output-buffering-example-php

Usually a template tag is wrote as a do first, and then a get later when it’s needed, but either way write your do and get when you need them, don’t build a get for the future unless it will be needed in the future

We are going to need to change some standard documentation to be clear on that. Hopefully Kellen will make sure documentation get’s changed for that.

/**
* Build social sharing icons.
*
* @return string
*/
function wds_eight_display_social_share() {
// Build the sharing URLs.
$twitter_url = 'https://twitter.com/share?text=' . rawurlencode( html_entity_decode( get_the_title() ) ) . '&url=' . rawurlencode( get_the_permalink() );
$facebook_url = 'https://www.facebook.com/sharer/sharer.php?u=' . rawurlencode( get_the_permalink() );
$linkedin_url = 'https://www.linkedin.com/shareArticle?title=' . rawurlencode( html_entity_decode( get_the_title() ) ) . '&url=' . rawurlencode( get_the_permalink() );
// Output markup.
?>
<div class="social-share">
<h5 class="social-share-title"><?php esc_html_e( 'Share this:', 'wds' ); ?></h5>
<ul class="social-icons menu menu-horizontal">
<li class="social-icon">
<a href="<?php echo esc_url( $twitter_url ); ?>" onclick="window.open(this.href, 'targetWindow', 'toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, top=150, left=0, width=600, height=300' ); return false;">
<?php echo wds_eight_get_svg( array( 'icon' => 'twitter-square', 'title' => 'Twitter', 'desc' => __( 'Share on Twitter', 'wds' ) ) ); // WPCS: XSS ok. ?>
<span class="screen-reader-text"><?php esc_html_e( 'Share on Twitter', 'wds' ); ?></span>
</a>
</li>
<li class="social-icon">
<a href="<?php echo esc_url( $facebook_url ); ?>" onclick="window.open(this.href, 'targetWindow', 'toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, top=150, left=0, width=600, height=300' ); return false;">
<?php echo wds_eight_get_svg( array( 'icon' => 'facebook-square', 'title' => 'Facebook', 'desc' => __( 'Share on Facebook', 'wds' ) ) ); // WPCS: XSS ok. ?>
<span class="screen-reader-text"><?php esc_html_e( 'Share on Facebook', 'wds' ); ?></span>
</a>
</li>
<li class="social-icon">
<a href="<?php echo esc_url( $linkedin_url ); ?>" onclick="window.open(this.href, 'targetWindow', 'toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, top=150, left=0, width=475, height=505' ); return false;">
<?php echo wds_eight_get_svg( array( 'icon' => 'linkedin-square', 'title' => 'LinkedIn', 'desc' => __( 'Share on LinkedIn', 'wds' ) ) ); // WPCS: XSS ok. ?>
<span class="screen-reader-text"><?php esc_html_e( 'Share on LinkedIn', 'wds' ); ?></span>
</a>
</li>
</ul>
</div><!-- .social-share -->
<?php
}
/**
* Get the social share markup
*
* @return string The markup.
*/
function wds_eight_get_social_share_markup() {
ob_start();
wds_eight_display_social_share();
return ob_get_clean();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment