[wp-hackers] How do filters access variables outside of their function?
Anca Mosoiu
anca at anca.tv
Thu Aug 26 20:34:58 UTC 2010
Hi Ken,
Hmm, interesting question. I'm working with the same sample code (what a coincidence!) and I used wp_debug to find the same thing you did. I just changed my function to access the global values for $post_ID and $post, because the values were coming in blank.
The filter is executed inside admin/edit-form-advanced.php, where the values for $post_ID and $post are available (and validated).
Cheers,
Anca.
> Date: Thu, 26 Aug 2010 16:13:33 -0400
> From: Ken <Ken at adcSTUDIO.com>
> Subject: [wp-hackers] How do filters access variables outside of their
> function?
> To: wp-hackers at lists.automattic.com
> Message-ID: <i56htf$i9h$1 at dough.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> For example a function Nacin put together on a thread back in May (found
> on Google):
>
> add_filter('post_updated_messages', 'nacin_book_updated_messages');
>
> function nacin_book_updated_messages( $messages ) {
>
> $messages['book'] = array(
> 1 => sprintf( __('Book updated. <a href="%s">View book</a>',
> 'myplugin_textdomain'), esc_url( get_permalink($post_ID) ) ),
> 2 => $messages['post'][2],
> 3 => $messages['post'][3],
> 4 => __('Book updated.', 'myplugin_textdomain'),
> /* translators: %s: date and time of the revision */
> 5 => isset($_GET['revision']) ? sprintf( __('Book restored to revision
> from
> %s', 'myplugin_textdomain'), wp_post_revision_title( (int)
> $_GET['revision'], false ) ) : false,
> 6 => sprintf( __('Book published. <a href="%s">View book</a>',
> 'myplugin_textdomain'), esc_url( get_permalink($post_ID) ) ),
> 7 => __('Book saved.', 'myplugin_textdomain'),
> 8 => sprintf( __('Book submitted. <a target="_blank" href="%s">Preview
> book</a>', 'myplugin_textdomain'), esc_url( add_query_arg( 'preview',
> 'true', get_permalink($post_ID) ) ) ),
> 9 => sprintf( __('Book scheduled for: <strong>%1$s</strong>. <a
> target="_blank" href="%2$s">Preview book</a>', 'myplugin_textdomain'),
> // translators: Publish box date format, see http://php.net/date
> date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ),
> esc_url( get_permalink($post_ID) ) ),
> 10 => sprintf( __('Book draft updated. <a target="_blank" href="%s">Preview
> book</a>', 'myplugin_textdomain'), esc_url( add_query_arg( 'preview',
> 'true', get_permalink($post_ID) ) ) ),
>
> }
>
> Calls both $post_ID and $post->post_date, both of which thro notices
> with wp_debug turned on.
>
> The thing that has me stumpt is that I didn't think variables outside of
> the function were accessible unless global'd.
>
> Can anyone enlighten me on this? Thanks
>
> ***************************************
More information about the wp-hackers
mailing list