无法在WordPress中本地化脚本

时间:2014-06-18 08:25:50

标签: javascript php wordpress

目的是在不损害WordPress标准的同时获取javascript文件中的模板目录路径。这是我的functions.php文件

<?php

add_action( 'admin_menu', 'register_options_page' );
add_action( 'admin_enqueue_scripts', 'my_scripts_method' ); 



function register_options_page()
{
    $my_hook = add_menu_page( 'Theme Options', 'Theme Options', 'manage_options', 'merry_options', 'get_theme_options', 'dashicons-share-alt', 99 );
    // var_dump($my_hook); die();
}

function my_scripts_method( $hook ) 
{
    if( 'toplevel_page_merry_options' !== $hook ) // Previously at: var_dump + die
        return;

    wp_enqueue_script( 'jquery-ui-tabs' );
    wp_enqueue_script( 'themeoptions-js', get_template_directory_uri().'/framework/themeoptions.js');
    wp_enqueue_style( 'themeoptions-style', get_template_directory_uri().'/framework/themeoptions.css');
    wp_enqueue_style( 'themeoptions-font-awesome-style', get_template_directory_uri().'/framework/styles/font-awesome.css');
    wp_enqueue_style( 'themeoptions-font-awesome-style-min', get_template_directory_uri().'/framework/styles/font-awesome.min.css');

    //Localizing scripts to get PHP code in return

    wp_register_script( 'merrypress-js-localized-scripts', get_template_directory_uri().'/framework/themeoptions.js');
    $translation_array = array( 'some_string' => get_template_directory_uri());
    wp_localize_script( 'get_template_directory_uri', 'localizedScriptObject', $translation_array );


    wp_enqueue_script( 'merrypress-js-localized-scripts' );

    # As best practice, the CSS AND JAVASCRIPT bellow should be enqueued here too
}

function get_theme_options()
{
    # Use THIS, not file_get_contents    
    include_once get_template_directory()."/framework/themeoptions.php";
}

?>

这是themeoptions.js文件中的代码

var gettemplatepath=some_string;

 alert(gettemplatepath) ; // alerts 'Some string to translate'


jQuery(document).ready(function($) {

        //main theme options tabs

        $( "#tabs" ).tabs().addClass( "ui-tabs-vertical ui-helper-clearfix" );
        $( "#tabs li" ).removeClass( "ui-corner-top" ).addClass( "ui-corner-left" );





        //on click function
        $( "#theme-options-save-button" ).click(function() {

            $(".theme-options-savechanges-loader").show();
            var request=$.ajax({
                type: "POST",
                url: ".../commitchanges.php",
                data: { 

                    name: "John",
                    location: "Boston"

                    }
                    });
                    request.done(function( msg ) {
                        alert( "Data Saved: " + msg );
                        $(".theme-options-savechanges-loader").hide();
                        });

                        request.fail(function( msg ) {
                        alert( "Fail: " + msg );
                        $(".theme-options-savechanges-loader").hide();
                        });


            });       //end on click function 







        }); //end document.ready

我在开发者控制台上遇到的错误是:

  

未捕获的ReferenceError:未定义some_string

你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

Localize脚本可用于为脚本提供任何数据。您要本地化的脚本可能是&#39; merrypress-js-localized-scripts&#39;,而不是&#39; get_template_directory_uri&#39;。

wp_localize_script( 'merrypress-js-localized-scripts', 'localizedScriptObject', $translation_array );

现在打开控制台并检查html,看看Wordpress上面创建了另一个脚本&merrypress-js-localized-scripts&#39;看起来像是:

<script type="text/javascript">
/* <![CDATA[ */
var localizedScriptObject = {"some_string":"http:\/\/localhost\/the_wall\/wp-content\/themes\/twentyfourteen"};
/* ]]> */
</script>