javascript - Unable to localize scripts in WordPress -



javascript - Unable to localize scripts in WordPress -

purpose template directory path in javascript file while not compromising wordpress standards. here functions.php file

<?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 ) // 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 php code in homecoming 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' ); # best practice, css , javascript bellow should enqueued here } function get_theme_options() { # utilize this, not file_get_contents include_once get_template_directory()."/framework/themeoptions.php"; } ?>

and here code within themeoptions.js file

var gettemplatepath=some_string; alert(gettemplatepath) ; // alerts 'some string 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

the error on developer console is:

uncaught referenceerror: some_string not defined

can help?

localize script can used create info available script. script want localize 'merrypress-js-localized-scripts', not 'get_template_directory_uri'.

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

now open console , inspect html see wordpress has created script above 'merrypress-js-localized-scripts' looks like:

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

javascript php wordpress

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -