[wp-hackers] Insert stuff into the excerpt based on a custom field, wipe out custom field

Matt Martz matt at sivel.net
Thu Sep 24 19:44:28 UTC 2009


On Thu, Sep 24, 2009 at 10:35:06PM +0300, scribu wrote:
> You should probably use wp_parse_str(), but other than that, it looks good.
> It shouldn't timeout.
> 
> 
> On Thu, Sep 24, 2009 at 9:57 PM, Matthew Gerring <mgerring at emrl.com> wrote:
> 
> > Hello all,
> >
> > This is the first time I've posted on this mailing list, hope this request
> > isn't repetitive/annoying/whatever.
> >
> > I'm trying to create a system that will parse YouTube URLs into a linked
> > thumbnail with Shadowbox/lightbox/your favorite box integrated. To do that,
> > I need a function that will check for a custom field, parse the URL, spit it
> > back out formatted, then insert the formatted URL into post_excerpt and wipe
> > out the custom field.
> >
> > Here's what I have so far in my functions.php file of my theme:
> >
> > function example($post_ID)
> >
> > {
> > $yt = get_post_meta($post_ID, 'yt', TRUE);
> > if ($yt!="") {
> >        $qs = parse_url($yt);
> >        parse_str($qs[query]);
> >        $lnk = '<a href="http://www.youtube.com/v/'.$v.<http://www.youtube.com/v/%27.$v.>'"><img
> > src="http://img.youtube.com/vi/'.$v.'/0.jpg<http://img.youtube.com/vi/%27.$v.%27/0.jpg>"
> > alt="" /></a>';
> >        $n_post = array();
> >        $n_post['ID'] = $post_ID;
> >        $n_post['post_excerpt'] = 'wut';
> >        wp_update_post( $n_post );
> >        delete_post_meta($ins_id, 'yt');
> >        }
> > }
> >
> > add_action('save_post' , 'example');
> >
> > I'm using save_post because according to the Codex, it runs right after a
> > post is saved.
> >
> > Now, this works, but it causes the request for post.php to timeout and I
> > end up with a blank white screen. If I go look at my post, it's there,
> > properly formatted, but I don't want freeze up saving posts for this. Is
> > there a better way to do it?
> >
> > -Matthew

The problem is that you are hooking save_post and performing a
wp_update_post within the callback.  wp_update_post causes the save_post
action to be fired again and thus you are creating a processing loop.

-- 
Matt Martz
matt at sivel.net
http://sivel.net/


More information about the wp-hackers mailing list