[wp-hackers] basic bilingual plugin: need help with two issues

Austin Matzko if.website at gmail.com
Sat Apr 18 13:07:07 GMT 2009


On Sun, Apr 12, 2009 at 3:02 AM, Stephanie Booth <bunnylists at gmail.com> wrote:
> 2. adding lang="" attribute to the post/hentry div
>
> I'd like the plugin to automatically add the correct lang attribute to
> the div containing the post. I've got some code but I'm a bit stuck,
> again:

I'm assuming you want to do this just on singular (i.e. individual
posts or pages) pages; anything else will be almost impossible to do
with output buffering.  Instead, you might tell people to put an
action hook at the right place in their templates, such as

do_action('bb_lang_attribute', get_the_ID());

so that you can more robustly do the lookup with a callback function
on that hook.

> function bb_insert_lang_attribute($buffer) {
>        $search='/(class="(.)*hentry(.)*")/';
>        $replace=\\1 . ' lang="' . bb_get_the_language() . '"';
>        return preg_replace($search, $replace, $buffer);
> }

I think you want something like this:

function bb_insert_lang_attribute($buffer) {
       $search = '/(class=("|\')[^"\']*hentry[^"\']*\2)/';
       $replace = '$1 lang=$2' . bb_get_the_language() . '$2';
       return preg_replace($search, $replace, $buffer);
}

> function bb_got_hentry()
> {
> // needs to check for hentry presence
>        return false;

Instead of having the hentry logic here, I would put it in the
bb_get_the_language() function: use WP_Query's get_queried_object()
method to get the current object, and do whatever logic check for that
particular post or page at that point.


More information about the wp-hackers mailing list