简体   繁体   English

在外部.js文件中使用全局php数组

[英]using a global php array in an external .js file

I am working on a theme framework for wordpress, and I have run into a small problem. 我正在为wordpress建立主题框架,但遇到了一个小问题。

I have a defined a $globals array that stores the current registered post types plus a few special pages for the theme layout options page. 我有一个定义的$ globals数组,用于存储当前已注册的帖子类型以及主题布局选项页面的一些特殊页面。

$GLOBALS['gc_frame_postTypes'] = array('homepage', 'blog');         
$post_types = get_post_types( '', 'names' ); 
    foreach ( $post_types as $post_type ) {
        if ($post_type != "revision" && $post_type != "nav_menu_item" && $post_type != "attachment")
            $GLOBALS['gc_frame_postTypes'][] = $post_type;
        }

I have this working for my options page, and it correctly creates all the correct options. 我在我的选项页面上使用了此功能,它可以正确创建所有正确的选项。

The issue I am having is when I try to apply javascript to improve the look and function of the page. 我遇到的问题是当我尝试应用JavaScript来改善页面的外观和功能时。

Here is the functioning .js code that does what I want. 这是可以执行我想要的功能的.js代码。 it shows and hides the fields depending on the selected layout. 它根据选定的布局显示和隐藏字段。

jQuery(document).ready(function() {

    // keeps the new sidebar text block blank
    jQuery("#gc_frame_sidebar_name_new").val("") 

    // Homepage layout Function call
    jQuery.fn.layoutSidebars('homepage');

    // page layout Function call
    jQuery.fn.layoutSidebars('page');

    // blog layout Function call
    jQuery.fn.layoutSidebars('blog');

    // post layout Function call
    jQuery.fn.layoutSidebars('post');



    // Homepage layout Function call on change
    jQuery('#gc_frame_homepage_layout').change(function(){
        jQuery.fn.layoutSidebars('homepage');
    });

    // page layout Function call on change
    jQuery('#gc_frame_page_layout').change(function(){
        jQuery.fn.layoutSidebars('page');
    });

    // blog layout Function call on change
    jQuery('#gc_frame_blog_layout').change(function(){
        jQuery.fn.layoutSidebars('blog');
    });

    // post layout Function call on change
    jQuery('#gc_frame_post_layout').change(function(){
        jQuery.fn.layoutSidebars('post');
    });



});



jQuery.fn.layoutSidebars = function(pageType){
    pageLayout = jQuery("#gc_frame_" +pageType+ "_layout").val();
    if (pageLayout == 'fullwidth'){
        jQuery("label[for^='gc_frame_" +pageType+ "_left_sidebar_']").closest('tr').hide();
        jQuery("#gc_frame_" +pageType+ "_left_sidebar_count").val(0);
        jQuery("label[for^='gc_frame_" +pageType+ "_right_sidebar_']").closest('tr').hide();
        jQuery("#gc_frame_" +pageType+ "_right_sidebar_count").val(0);      
    } else if (pageLayout == 'SB-L') {
        jQuery("label[for='gc_frame_" +pageType+ "_left_sidebar_count']").closest('tr').show();
        jQuery("label[for^='gc_frame_" +pageType+ "_right_sidebar_']").closest('tr').hide();
        jQuery("#gc_frame_" +pageType+ "_right_sidebar_count").val(0);
    } else if (pageLayout == 'SB-R') {
        jQuery("label[for^='gc_frame_" +pageType+ "_left_sidebar_']").closest('tr').hide();
        jQuery("#gc_frame_" +pageType+ "_left_sidebar_count").val(0);
        jQuery("label[for='gc_frame_" +pageType+ "_right_sidebar_count']").closest('tr').show();
    } else if (pageLayout == 'SB-LR') {
        jQuery("label[for='gc_frame_" +pageType+ "_left_sidebar_count']").closest('tr').show();
        jQuery("label[for='gc_frame_" +pageType+ "_right_sidebar_count']").closest('tr').show();
    }
}

What I would like to do is take the above global array and get it into the javascript somehow, so that if new custom post types are added they will be affected by the javascript as well. 我想做的是将上述全局数组放入javascript中,这样,如果添加了新的自定义帖子类型,它们也会受到javascript的影响。

I currently have the php and javascript in 2 files, and I do not know how to get the information from the php file to the javascript. 我目前有2个文件包含php和javascript,并且我不知道如何将信息从php文件获取到javascript。

Any help would be great 任何帮助都会很棒

You should use $_SESSION variables to share information within your pages. 您应该使用$_SESSION变量在页面内共享信息。

In your first page, before loading your javascript file: 在您的首页中,在加载javascript文件之前:

session_start();
$_SESSION['globalVar'] = Value;

In your external javascript file (with .php extension) you can get this like: 在您的外部javascript文件(扩展名为.php)中,您可以像这样:

<?php session_start(); //at the top of your JavaScript?>
...
var globalVar = '<?php echo $_SESSION['globalVar']; ?>'
...

You can use wp_localize_script() 您可以使用wp_localize_script()

Here's a good resource: http://pippinsplugins.com/use-wp_localize_script-it-is-awesome/ 这是一个很好的资源: http : //pippinsplugins.com/use-wp_localize_script-it-is-awesome/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM