Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress主题选项-wp_localize_script();_Php_Jquery_Wordpress_Wordpress Theming_Options - Fatal编程技术网

Php Wordpress主题选项-wp_localize_script();

Php Wordpress主题选项-wp_localize_script();,php,jquery,wordpress,wordpress-theming,options,Php,Jquery,Wordpress,Wordpress Theming,Options,我用它来改变我正在构建的主题中的JS,如下所示: wp_enqueue_script('custom', get_bloginfo('template_directory') . '/js/script.js'); $settings = get_option('bro_options'); wp_localize_script('custom','settings',$settings); 虽然这很好,但它的工作方式是在加载相关JS文件之前编写内联JS,这样您就可以使用某些

我用它来改变我正在构建的主题中的JS,如下所示:

   wp_enqueue_script('custom', get_bloginfo('template_directory') . '/js/script.js');
   $settings = get_option('bro_options');
   wp_localize_script('custom','settings',$settings);
虽然这很好,但它的工作方式是在加载相关JS文件之前编写内联JS,这样您就可以使用某些变量来修改JS及其功能

问题是,我真的不喜欢这个选项以及它输出内联JS的方式。有没有人知道有没有别的办法?我很确定这不是每个人都用他们的主题选项做的,所以一定有其他的方法

任何帮助都将不胜感激,请不要链接到插件


简而言之:我正在寻找使用
wp\u localize\u script()的替代方案settings.js.php
的文件,其中包括
wp config.php
文件

像这样:

<?php

    header("Content-type: application/x-javascript");

    $c = 0;

    while($exists !== true && $notfound !== true) {

        $c++;
        $str = $str . '../';

        if(file_exists($str.'wp-config.php')) {
            $exists = true;
            include($str.'wp-config.php');

            $i = 0;
            $amount = sizeof($bro);

            echo 'var settings={';
            foreach($bro as $key => $value) {
                $i++;
                if($i<$amount) {
                    echo $key . ':"' . $value . '",';
                } else {
                    echo $key . ':"' . $value . '"';
                }
            }
            echo '};';

        }

        if($c === 20) { 
            $notfound = true;
        }

    }

?>
我将把这个文件排在我的
custom.js
文件之前,这样我就可以确定一些设置是否处于打开和关闭状态

if(settings.foo === true) {
    ...
}
所有这些,所以我没有内联JS,可能有一些缓存问题,但我宁愿让它输出所有选项内联

if(settings.foo === true) {
    ...
}