YouTube Embed Test

Getting a WP Post Excerpt Outside The Loop – Updated

Earlier, I posted a link to an excellent post which saved me the job of figuring out how to grab a WP post excerpt for use outside The Loop.

However, it wasn’t absolutely perfect for my needs, so here is my updated version – hopefully it comes in useful for somebody else:

function fr_excerpt_by_id($post_id, $excerpt_length = 35, $line_breaks = TRUE){
$the_post = get_post($post_id); //Gets post ID
$the_excerpt = $the_post->post_excerpt ? $the_post->post_excerpt : $the_post->post_content; //Gets post_excerpt or post_content to be used as a basis for the excerpt
$the_excerpt = apply_filters('the_excerpt', $the_excerpt);
$the_excerpt = $line_breaks ? strip_tags(strip_shortcodes($the_excerpt), '<p><br>') : strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
$words = explode(' ', $the_excerpt, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
  array_pop($words);
  array_push($words, '…');
  $the_excerpt = implode(' ', $words);
  $the_excerpt = $line_breaks ? $the_excerpt . '</p>' : $the_excerpt;
endif;
$the_excerpt = trim($the_excerpt);
return $the_excerpt;
}

The differences are based primarily on my use case. I’m attempting to use this to auto-generate <meta> tag content for certain post types to be used in Open Graph Tags for Facebook and Twitter Card markup. Just make it reusable elsewhere, I’ve added the ability to keep paragraphs or breaks as dictated in the Post Excerpt/Content; and am passing the Excerpt Length as a variable, rather than needing to set and forget in the actual function.

My primary use case is in code used in the <head> section of my site – it could be hardcoded in your header.php file or, preferably, passed in via a function. For example:

<?php $my_excerpt = fr_excerpt_by_id($post_id, 40, FALSE);

// $post_id is the id of the desired post. We want an excerpt of up to 40 words, with <p> and <br /> tags stripped.

echo '<meta property="og:description" content="' . $my_excerpt . '" />';
// example usage for an Open Graph description tag
?>

Alternatively, for use in a sidebar or similar:

<div class="widget">
<?php echo fr_excerpt_by_id($post_id); ?>
</div>

$post_id is the id of the desired post. Display a default 35 word excerpt with surrounding <p> tags, and line breaks (if applicable)

Those are a couple of limited use cases, but should be enough to help figure out other possible applications.

Getting a WP Post Excerpt Outside The Loop

http://www.uplifted.net/programming/wordpress-get-the-excerpt-automatically-using-the-post-id-outside-of-the-loop/

Nicely done, Mr Davis – saved me a job!

Changing YouTube Channel Display Name

There’s a roundabout route to the Channel Display Name setting on YouTube. I’ve had occasion to need to change it for a couple of clients lately, so this post is by way of a reminder for myself, next time I need to.

N.B. This is currently how it works if you’re already opted in the YouTube One Channel format.

  1. Log in to YouTube and go to your Channel.
  2. Hover over the display name under the Channel Art. Click the pencil icon that appears at the right.
  3. Choose “Channel Settings”
  4. Choose “Advanced” under Channel Settings in the left hand menu

WordPress: Switching Off Comments on Pages by Default

When setting up new WordPress sites, as I do frequently for client projects, I’m frequently irritated by the need to manually switch comments off for WordPress Pages. There are really very few instances when I will need them enabled.

Why is this a big deal? Mainly because I like to switch off, and hide, functions of the software which are unnecessary for my clients to lessen their confusion when navigating around the site admin. I also, of course, remove Comments in the page templates too but that leaves you (or the client actually) with that confusing checkbox when Quick Editing pages.

Anyway, to get back to the point… There are two things I wanted to mention that can be done.

1. Disable Comments for Pages in your Theme

This requires a simple snippet of code to be added to your functions.php file.

/**
* No Comments on Pages
*/
add_action('init', 'pages_nocomments');


function pages_nocomments() {
remove_post_type_support( 'page', 'comments' );
}

Simple as that! Now, you’re no longer bothered by the Comments. However, if you set up your Page structure before adding that to your theme, you will also need #2.

2. Disable Comments for Pages in your Database

This seems a bit scarier than just adding a little code to a file, but I’m keeping it simple here! Log in to phpMyAdmin, or whichever Database GUI is used by your host, and select your WordPress database. Then run the following code (in phpMyAdmin you click the SQL tab – other GUIs may vary)

update wp_posts set comment_status = 'closed' where post_type = 'page'

N.B. If you have changed the Table Prefix in your wp-config.php file, don’t forget to update the code above with the correct prefix for the Posts table.

Testing TwentyTwelve

Since I haven’t yet found the time to substantially work on the theme for this site, I’ve switched to the brand new TwentyTwelve theme, which is part of the newly released WordPress 3.5 beta 1. Looks pretty nice and minimal so far. I’ll live with it for a while, maybe mess with a few settings, and see what I think.

Impressions on the theme, and the rest of the 3.5b1, another time. Media handling is supposedly improved, so I’m looking forward to playing with that. Haven’t had a chance to read the changelog lately, so I forget what else they were introducing…

Are you testing the new WordPress version too? Anything I should watch out for?

Codekit

First time out testing Codekit on a new project today. What an awesome app! Nice to get a start on some CSS preprocessing, and generally streamline my workflow.

WordPress Queries – Ordering by ‘meta_value’ where the ‘meta_key’ is an Array

Given that most of my daily web work is building websites for Musicians, I developed a set of Custom Post Type code (I hesitate to call it plugin since it’s not packaged yet — on the to-do list!) to collect and display details about Artist Recordings. To try and keep database bloat down, I saved the numerous Custom Fields I used in a single key, as an array. This worked great until I realised that it would be advantageous to be able to display them ordered by Release Year; one of the fields in the array.

This has been on my mind for a while and I happened across a solution today. It turns out that WordPress will quite happily allow meta_key values which are arrays. It simply uses the 1st field in the array to sort by. Therefore, if you put the field you want to sort by first in the array, you can sort by that field. Feels a bit hackish, but it works!

Rare Family Photo-op

The Davis’ – June 9th, 2012

These photo opportunities don’t come around too often, now I live on a different continent!

WP Custom Post Types UI and “Insert Into Post”

On a WordPress site I was working on recently I ran into an issue that had me stumped. Namely on some, but not all, of the Custom Post Types I was using, when using the Add Media dialog to upload and insert media to an entry, the “Insert Into Post” button was missing!

I googled, as one does, and similar reports led me to realise that the problem occurs when you generate a custom Edit screen for a particular post type. Specifically, if you don’t allow support for the Editor in your post type setup and then place it manually in a custom meta box. This project was a new build in WP3.3+, and so the Editor is rendered with the new wp_editor() function, with the ID set to ‘content’ so it automatically hooks into the regular save routine and you can display the content using the_content() in your template. In this situation, the media buttons will display perfectly happily, if called, and the Media Uploader works as expected. It’s just that the “Insert Into Post” button is gone. If you add Editor support in the arguments when registering the post type, it appears as expected.

(You may be asking why I want to render the Editor manually, which is a topic for another post. In brief, it made more sense for the content I’m collecting to have some custom fields rendered before the Editor, and to be able to label and provide some instructions for what should be entered in the Editor. More about that another time…)

Eventually, I ran across a helpful post on the WordPress Support Forums with a method for forcing the button to appear:

add_filter( 'get_media_item_args', 'force_send' );

function force_send($args){
$args['send'] = true;
return $args;
}

That works as described and I thank ‘misternifty’ for it. However, it forces the button to appear in such a way that it also appears when viewing/editing an attachment through the Media Library. That’s not ideal, mainly because it simply won’t work, but also because it has the potential to be confusing for the client for whom the site has been built. Fortunately, I hit upon a solution fairly quickly, which only targets the Post Types for which it’s a problem. This is what I came up with:


add_filter( 'get_media_item_args', 'force_send' );

function force_send($args){
$pid = $_GET['post_id'];
if(get_post_type($pid) == '{post type}') {
$args['send'] = true;
}
return $args;
}

I realised that when the Add Media dialog is called from within a post (or other post type), there is a query string sent to media-upload.php in the iframe to attach the uploaded file to the current post object. It takes the form: {your path to WP installation}/wp-admin/media-upload.php?post_id=xxx.

Therefore, what this code does is GET the parent post id from the iframe URL. Then you check if that post has a post type that is missing Editor support and force the button, if so. Et voila, the “Insert Into Post” is back in place where it’s needed.

A longwinded explanation, perhaps, but it will hopefully make it easier to find a solution that stumped both Google and I for too long this afternoon! If you have questions or comments about this approach, comments are open. I look forward to reading what you have to say!