admin管理员组

文章数量:1430563

I have two cases for is_rtl, one doesn't work the other does, but don't know why.

Case1: doesn't work

i define a variable to hold a value depending on is_rtl, then var_dump it within the admin_footer hook, then the result becomes LTR

<?php
/*
Plugin Name: Test
*/

class A{
    public function __construct(){
        $option_name = is_rtl() ? 'RTL ': 'LTR ';
        add_action('admin_footer', function() use($option_name){
            echo '<pre dir=ltr>';
              var_dump($option_name);
            echo '</pre><br>';
        });

    }
}


$a = new A();

Case2: works

I define the variable inside the callable hooked to admin_footer, so the result becomes RTL

    <?php
/*
Plugin Name: Test
*/

class A{
    public function __construct(){

        add_action('admin_footer', function(){
            $option_name = is_rtl() ? 'RTL ': 'LTR ';
            echo '<pre dir=ltr>';
              var_dump($option_name);
            echo '</pre><br>';
        });

    }
}


$a = new A();

So why this happens because i need to load options depending on the page direction, but can't get it to work.

I have two cases for is_rtl, one doesn't work the other does, but don't know why.

Case1: doesn't work

i define a variable to hold a value depending on is_rtl, then var_dump it within the admin_footer hook, then the result becomes LTR

<?php
/*
Plugin Name: Test
*/

class A{
    public function __construct(){
        $option_name = is_rtl() ? 'RTL ': 'LTR ';
        add_action('admin_footer', function() use($option_name){
            echo '<pre dir=ltr>';
              var_dump($option_name);
            echo '</pre><br>';
        });

    }
}


$a = new A();

Case2: works

I define the variable inside the callable hooked to admin_footer, so the result becomes RTL

    <?php
/*
Plugin Name: Test
*/

class A{
    public function __construct(){

        add_action('admin_footer', function(){
            $option_name = is_rtl() ? 'RTL ': 'LTR ';
            echo '<pre dir=ltr>';
              var_dump($option_name);
            echo '</pre><br>';
        });

    }
}


$a = new A();

So why this happens because i need to load options depending on the page direction, but can't get it to work.

Share Improve this question asked Jun 4, 2019 at 2:26 MakiomarMakiomar 1517 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Case 1 - is_rtl() is not working because it has been called directly. To use such contditional tags in WordPress, query should already be run. So such function should be used inside the function hooked to appropriate hook. Since it is called directly, its value will be always false.

See https://developer.wordpress/themes/basics/conditional-tags/#where-to-use-conditional-tags

Case 2 - It is working because you can using this tag inside the function hooked to admin_footer. So, is_rtl() will give you appropriate value.

So, if admin_footer is not working for you then, you need to check which hook is appropriate for you and use is_rtl() inside the function hooked properly.

本文标签: pluginsisrtl returns false while it should return true