Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have RTL website in URDU language. lang="ur" is hard coded in main body i.e. <body lang="ur" <?php body_class(); ?>>. Now I want to replace attribute with some PHP code that will show or hide it on specific conditions.

I want that on page editing screen, there will radio buttons. When we select "Remove", remove lang="ur" from current page & otherwise leave it remain.

share|improve this question
    
Can you post the body tag from your template? –  birgire Oct 8 '13 at 8:27
    
@birgire this code is from theme's header.php: <body lang="ur" <?php body_class(); ?>> –  John Doe Oct 8 '13 at 9:38
    
You already went through this issue wordpress.stackexchange.com/questions/116884/…. The difference is you want a radio button instead of writing a custom field value, consider using the Advanced Custom Fields plugin for this case. –  P-S Oct 9 '13 at 8:36

1 Answer 1

up vote 0 down vote accepted

It looks like this attribute is hardcoded into your body tag.

You could adjust it with your own body_lang() function like this (untested) :

<body 
   <?php ( function_exists( 'body_lang' ) ) ? body_lang() : '' ;?> 
   <?php body_class(); ?>
>

where you can define the following, in your functions.php file:

/**
 * Display the language attribute part for the body element.
 *
 * @param string $lang.
 * @return void.
 */

if( ! function_exists( 'body_lang' ) )
{
    function body_lang( $lang = '' )
    {
         if( function_exists( 'get_body_lang' ) )
             echo get_body_lang( $lang );
    }
} // end if function exists

and

/**
 * Retrieve the language part for the body element.
 *
 * @param string $lang.
 * @return string $lang.
 */

if( ! function_exists( 'get_body_lang' ) )
{
    function get_body_lang( $lang = '' )
    {
        // default language
        $lang = ( ! empty( $lang ) ) ? $lang : 'ur';        

        // add your own logic here:
        if( is_single() )
            $lang = 'de';

        elseif( is_page() )
            $lang = 'en';

        $attr = sprintf( 'lang="%s"', $lang );

        return apply_filters( 'body_lang', $attr );
    }

} // end if function exists

where the filter body_lang gives you further control.

To remove it, according to your own special logic, you can for example use:

/**
 * Remove the language part according to a custom logic
 *
 * @param string $attr.
 * @return string $attr.
 */

 function my_body_lang( $attr )
 {
     // add your own logic here:
     if( is_single() )
     { 

         // get the post meta value for 'language'. 
         // It can be 'Yes', 'No' or empty
         $show = get_post_meta( get_the_ID(), 'language', TRUE );

         // Let's hide it if it's empty or 'No'
         if( empty( $show ) || "No" === $show )
             $attr = '';

     }

     return $attr;
 }

add_filter( 'body_lang', 'my_body_lang' );

Hopefully this points you towards a solution.

share|improve this answer
    
Thank you for response. I got idea from you answer. But there is something going wrong. Please check updated question. –  John Doe Oct 8 '13 at 10:52
    
I finally tested this, and it works now on my install ;-) I think you can use my solution with your new idea, but maybe your new idea deserves a new question. –  birgire Oct 8 '13 at 11:06
    
I updated the answer to check for the post meta value 'language'. This value can be Yes, No or empty, according to the code you pasted above. If it's No or Empty then the language attribute is not disiplayed. –  birgire Oct 8 '13 at 11:34
    
Thank you for you contribution. –  John Doe Oct 9 '13 at 7:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.