Fetching remote pages

The two main reasons why I need to fetch a remote page are either to further transform the data there or for some sort of API call.

Derived resources from your HTML

Web pages these days are rather dynamic. Shopping carts, purchase orders, timetables — the list goes on. These are all pages presented to the person visiting your site, their content transient. It can be useful to capture that page and derive something more permanent from it. Setting a print css can be useful for your visitor, but if you want to have a trail you could create a PDF of the page and email it. This is useful for receipts and invoices.

API calls

Many sites that act like a resource (especially resource management) often have an API to allow you to poke and peek at your data in there.

All this means that you may want to fetch a remote page (even if it is your own). Here is a simple example.

$tcbfeed = wp_remote_get( 'http://www.tcbarrett.com/feed/' );
if ( is_wp_error( $tcbfeed  ) ) {
   echo 'Something went wrong!';
} else {
echo 'TCB Feed:
' . print_r( $tcbfeed, 1 ) . '
';
}

If you put this in a page template, you should see how it works.

Using WordPress page templates to restrict access to content

If you have a complex site, perhaps extending WordPress roles by adding your own custom roles. Or even just making extensive use of the core roles. You may have need to quickly create pages that are only readably by certain groups of users. I try to use capabilities over roles for checking these sorts of things, as often you don’t want to exclude users that a role that you consider superior (e.g. checking for ‘subscriber‘ means that ‘editor‘ users cannot see the page, whereas checking for ‘edit_posts‘ means everyone who has a role of ‘contributor‘ or above can view the page).

<?php
/* Template Name: Restricted to Contributors and above */
if ( ! current_user_can( 'edit_posts' ) ) {
  wp_redirect( wp_login_url( get_permalink() ), 302 );
  exit( 0 );
}
?>

If you don’t want it to redirect to the login page, but instead display a message in place of the post content, then you will need to use it as a conditional around the loop. This is a little trickier to give an example of, as it depends

Add more buttons to the WordPress TinyMCE WYSIWYG post editor

WordPress uses the TinyMCE javascript library to power the WYSIWYG post editor. The core team have done a good job of selecting the most commonly needed buttons, but on occasion it can be useful to add more. In my experience, the three most commonly requested buttons are horizontal rule <hr>, superscript <sup> and subscript <sub>.

function tcb_add_tinymce_buttons( $tinyrowthree ) {
  $tinyrowthree[] = 'hr';
  $tinyrowthree[] = 'sub';
  $tinyrowthree[] = 'sup';
  return $tinyrowthree;
}
add_filter( 'mce_buttons_3', 'tcb_add_tinymce_buttons' );

This adds a third row to the editor with your three new buttons. There are more to choose from, you can visit the TinyMCE buttons page to have a look.

You may want to pair this with forcing the kitchen sink always on, so your clients can always find them..

YouTube video thumbnails using WordPress shortcodes

We all love a good video on YouTube. This simple snippet lets you insert a thumbnail, with a link to the video you want to show, using a WordPress shortcode. All you need is the video ID to pop in the shortcode attributes.

add_shortcode( 'tcb_youtube', 'tcb_youtube_video_thumbnail' );
function tcb_youtube_video_thumbnail( $atts ) {
  extract( shortcode_atts( array(
    'id'    => 'RxoZ-AZKpX8',
    'size'  => '0',
    'align' => 'left'
  ), $atts ) );
  // Allow more human readable 'small' and 'large' to be used.
  $sizes = array( 'large' => 0, 'small' => 1, 'l' => 0, 's' => 1 );
  if ( isset( $sizes[$size] ) ) $size = $sizes[$size];

  $img_src = 'http://img.youtube.com/vi/' . $id . '/' . $size . '.jpg';
  $href    = 'http://youtu.be/' . $id;

  return '<a href="' . $href . '"><img class="hiddenSpellError" alt="" />src="' . $img_src . '" alt="" class="align' . $align . '" /></a>';
}

Example YouTube video thumbnail shortcode:

[tcb_youtube]

Extending the attributes

This can easily be extended to include other attributes. If you don’t link youtu.be, you could change the base href. Alternatively you can wrap it with your own caption styling.

Show future (scheduled) posts

Scheduling posts is a very useful technique, especially for blog authors. Why not add further value to your site by teasing visitors with upcoming posts? Place this code in your page template, and it will show the title of the next five future scheduled posts.

<?php query_posts( 'showposts=5&post_status=future' ); ?>
<?php if ( have_posts() ) : ?>
  <div class="future-posts">Future Posts</div>
  <?php while ( have_posts() ) : the_post(); ?>
    <div>
      <p class="future-title">
        <?php the_title(); ?> <br />
        <span class="datetime"><?php the_time( 'l, jS F Y' ); ?></span>
      </p>
    </div>
  <?php endwhile; ?>
<?php endif; ?>

I don’t use it personally, because I change my mind a lot and change the order of my scheduled posts quite often.