Tag Archives: php

Adding a link to logout of password protected posts in WordPress

I needed to add a “logout” link to a theme on which I’m working that would clear the cookie WordPress sets for password protected posts. This is what I came up with:

<?php if ( $_COOKIE['wp-postpass_' . COOKIEHASH] ) { ?>
  | <a href="<?php echo get_template_directory_uri(); ?>/includes/logout.php">Log Out</a>
<?php } ?>

COOKIEHASH is a constant set by WordPress that contains a hash of your blog URL.

For the actual cookie clearing functionality, you can use something like this script over at Stack Overflow.

Some snippets for password-protected pages in WordPress

I was working on a site that required protected pages in WordPress, and had a few snippets that I thought were worth capturing.

By default WordPress prepends the title of a page or post with “Private:” or “Protected:” if you change the document’s visibility. This is ugly, and I can’t see the point of it. The following snippet, added to your functions.php, will remove that.

add_filter( 'private_title_format', 'no_private_title_format' );
add_filter( 'protected_title_format', 'no_private_title_format' );
 
function no_private_title_format( $format ) {
  return '%s';
}

The next function creates a custom form. I’m using the Modest theme, this is a modified version of their form. You can get more detail at WP Tuts+

add_filter( 'the_password_form', 'custom_password_form' );
function custom_password_form() {
    global $post;
    $label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
    $action = get_option('siteurl') . '/wp-login.php?action=postpass';
    $o  = <<<EOT
<div id="et-login">
  <div class='et-protected'>
    <div class='et-protected-form'>
      <form action='$action' method='post'>
        <p><label><span>Password: </span><input type='password' name='post_password' id='$label' size='20' /><span class='et_protected_icon et_protected_password'></span></label></p>
        <input type='submit' name='submit' value='Login' class='etlogin-button' />
      </form>
    </div>
  </div>
</div>
EOT;
    return $o;
}

Finally, don’t forget that the sidebar and custom fields aren’t hidden by password protection. If you don’t want something to show up, wrap it like this (this example is grabbing a field set by Advanced Custom Fields):

<?php
  if ( !post_password_required() ) {
    echo get_field( 'sidebar' );
  }
?>

Convert .webloc files to a Markdown list

I have a bunch of bookmarks I was keeping in folders on my Mac as .webloc files. I still want the bookmarks, but I don’t want all these tiny files cluttering up my drive, so I wrote a quick script called webloc2md. The script takes a single argument, the target directory, and outputs the links as an unordered list formatted with Markdown. The list is output directly, you can pipe it into the file of your choice.

One caveat: the .webloc files must be in XML format. If not, the script will skip them, and output a warning as the last list item. You can use the program plutil to do the conversion. To convert all the .webloc files in the current directly, type plutil -convert xml1 *.webloc.

You can grab the script from my GitHub account.