percorso del tema nel file javascript
6 risposta
- voti
-
- 2013-03-07
Quello che stai cercando è la funzione wp_localize_script .
Lo usiin questomodo quando accodi lo script
wp_register_script( 'my-script', 'myscript_url' ); wp_enqueue_script( 'my-script' ); $translation_array = array( 'templateUrl' => get_stylesheet_directory_uri() ); //after wp_enqueue_script wp_localize_script( 'my-script', 'object_name', $translation_array );
Neltuo style.js,ci sarà:
var templateUrl = object_name.templateUrl; ...
What you're looking for is wp_localize_script function.
You use it like this when enqueing script
wp_register_script( 'my-script', 'myscript_url' ); wp_enqueue_script( 'my-script' ); $translation_array = array( 'templateUrl' => get_stylesheet_directory_uri() ); //after wp_enqueue_script wp_localize_script( 'my-script', 'object_name', $translation_array );
In your style.js, there is going to be:
var templateUrl = object_name.templateUrl; ...
-
eccezionale.hafunzionato ameraviglia!awesome. worked like a charm!
- 0
- 2016-11-28
- James Hall
-
- 2013-03-07
Questi sonoi duemodi seguentiper aggiungereilpercorso deltemanelfilejavascript.
1) Puoi utilizzare wp_localize_script () suggerito da wordpressneltuofilefunctions.php. Questo creerà un oggetto Javascriptnell'intestazione,che sarà disponibileperi tuoi scriptin fase diesecuzione.
Esempio:
wp_register_script('custom-js',get_stylesheet_directory_uri().'/js/custom.js',array(),NULL,true); wp_enqueue_script('custom-js'); $wnm_custom = array( 'stylesheet_directory_uri' => get_stylesheet_directory_uri() ); wp_localize_script( 'custom-js', 'directory_uri', $wnm_custom );
e puòessere utilizzatonelfilejs come segue:
alert(directory_uri.stylesheet_directory_uri);
2) Puoi creare uno snippet Javascript che salva la directory delmodello uriin una variabilee utilizzarloin seguito come segue: Aggiungi questo codicenelfile header.phpprima delfilejsin cui vuoi usare questopercorso. Esempio:
<script type="text/javascript"> var stylesheet_directory_uri = "<?php echo get_stylesheet_directory_uri(); ?>"; </script>
e puòessere utilizzatonelfilejs come segue:
alert(stylesheet_directory_uri);
These are the following two ways to add theme path in javascript file.
1) You can use wp_localize_script() suggested by wordpress in your functions.php file. This will create a Javascript Object in the header, which will be available to your scripts at runtime.
Example :
wp_register_script('custom-js',get_stylesheet_directory_uri().'/js/custom.js',array(),NULL,true); wp_enqueue_script('custom-js'); $wnm_custom = array( 'stylesheet_directory_uri' => get_stylesheet_directory_uri() ); wp_localize_script( 'custom-js', 'directory_uri', $wnm_custom );
and can use in your js file as following :
alert(directory_uri.stylesheet_directory_uri);
2) You can create a Javascript snippet that saves the template directory uri in a variable, and use it later as following: Add this code in header.php file before the js file in which you want to use this path. Example:
<script type="text/javascript"> var stylesheet_directory_uri = "<?php echo get_stylesheet_directory_uri(); ?>"; </script>
and can use in your js file as following :
alert(stylesheet_directory_uri);
-
wp_localizefunziona!Hoprovato ancheil 2 ° approccio,manon sono riuscito afarlofunzionare.wp_localizefunzionaprobabilmente è unapraticamigliore,no?wp_localize works! I tried the 2nd approach, too, but I didn't manage to make it work. wp_localize works is probably better practice, no?
- 0
- 2013-03-07
- charlenemasters
-
@charlenemastersperfarfunzionareil secondo approccio l'ordine di dichiarazione della variabilee l'accesso èmoltoimportante.@charlenemasters to make the second approach work the order of declaring variable and accessing it is very important.
- 0
- 2013-03-07
- Vinod Dalvi
-
il secondo approccio dovrebbeessere con "echo"perfunzionarethe second approach should be with `echo` in order to work
- 2
- 2016-10-21
- Claudiu Creanga
-
@ClaudiuCreanga Grazie,dovrebbeessereecho: `var stylesheet_directory_uri=" Phpechoget_stylesheet_directory_uri ();?> ";`@ClaudiuCreanga Thanks, should be echo: `var stylesheet_directory_uri = "";`
- 0
- 2018-06-12
- ycc_swe
-
- 2013-03-07
Puoi localizzarei tuoifilejavascript,il cheti dà l'opportunità digenerare un arrayjavascriptpieno di valori definiti da PHP (come la localizzazione o le directory).
Se carichiiltuofilejavascripttramite
wp_enqueue_script
owp_register_script
èfacile da configurare come segue:function localize_vars() { return array( 'stylesheet_directory' => get_stylesheet_directory_uri() ); } wp_enqueue_script( 'my_script', plugins_url( 'my_plugin/my_script.js' ), array( 'jquery' ) ); wp_localize_script( 'my_script', 'my_unique_name', localize_vars() );
Enei tuoifilejavascript,puoi chiamare queste variabili con:
my_unique_name.stylesheet_directory
You can localize your javascript files, wich gives you the opportunity to generate a javascript array filled with PHP defined values (such as localisation or directories).
If you load your javascript file trough
wp_enqueue_script
orwp_register_script
its easy to set up like follows:function localize_vars() { return array( 'stylesheet_directory' => get_stylesheet_directory_uri() ); } wp_enqueue_script( 'my_script', plugins_url( 'my_plugin/my_script.js' ), array( 'jquery' ) ); wp_localize_script( 'my_script', 'my_unique_name', localize_vars() );
And in your javascript files, you can call these variables by:
my_unique_name.stylesheet_directory
-
- 2016-08-12
Hoiniziato a utilizzare questo comodometodoper ottenere la directory deitemi di WordPresse memorizzarla come variabile JavaScriptglobale (tutto da unfilejavascript):
function getThemeDir() { var scripts = document.getElementsByTagName('script'), index = scripts.length - 1, myScript = scripts[index]; return myScript.src.replace(/themes\/(.*?)\/(.*)/g, 'themes/$1'); } themeDir = getThemeDir();
Funzionerà solo se vengono soddisfatte le seguenti condizioni :
1. Questo snippet vieneeseguitotramite unfile JavaScriptesterno,in questomodo:
<script src="/wp-content/themes/themename/assets/app.js"></script>
2. Ilfilejs risiedenella directory deitemi deltuo sito (o sottodirectory).
I started using this convenient little method to get the WordPress theme directory and store it as a global JavaScript variable (all from within a javascript file):
function getThemeDir() { var scripts = document.getElementsByTagName('script'), index = scripts.length - 1, myScript = scripts[index]; return myScript.src.replace(/themes\/(.*?)\/(.*)/g, 'themes/$1'); } themeDir = getThemeDir();
This will only work if the following conditions are met:
1. This snippet is executed via an external JavaScript file - like this:
<script src="/wp-content/themes/themename/assets/app.js"></script>
2. The js file resides within your site's theme directory (or subdirectory).
-
- 2017-01-01
Ecco come l'hofatto.
Posizionailfilejavascripte leimmagininella cartella deltema/asset
Emodificai seguentifile.
Infunctions.php
/* for javascript (only when using child theme) */ wp_enqueue_script('my-script', get_template_directory_uri() . '-child/assets/test.js'); wp_localize_script('my-script', 'myScript', array( 'theme_directory' => get_template_directory_uri() ));
Neltuofilejavascript
var url = myScript.theme_directory + '-child/assets/';
This is how I did it.
Place the javascript file and images in theme-folder/assets
And edit the following files.
In functions.php
/* for javascript (only when using child theme) */ wp_enqueue_script('my-script', get_template_directory_uri() . '-child/assets/test.js'); wp_localize_script('my-script', 'myScript', array( 'theme_directory' => get_template_directory_uri() ));
In your javascript file
var url = myScript.theme_directory + '-child/assets/';
-
- 2016-06-14
Seilfilejavascript viene caricato dalla dashboard di amministrazione,èpossibile utilizzare questafunzionejavascriptper ottenere la radice dellapropriainstallazione di WordPress.
function getHomeUrl() { var href = window.location.href; var index = href.indexOf('/wp-admin'); var homeUrl = href.substring(0, index); return homeUrl; }
Quindi contattailpercorso deltuotema come di seguito.
var myThemePath = getHomeUrl() + '/wp-content/themes/myTheme';
If the javascript file is loaded from the admin dashboard, you can use this javascript function get the root of your WordPress installation.
function getHomeUrl() { var href = window.location.href; var index = href.indexOf('/wp-admin'); var homeUrl = href.substring(0, index); return homeUrl; }
Then just contact the path to your theme like below.
var myThemePath = getHomeUrl() + '/wp-content/themes/myTheme';
Devoincludereilpercorso delmiofile deltema all'interno di unfilejavascript.Comepotreifare a questoproposito?Hogiàprovato:
Questononmi fornisceilpercorso,mainserisce solo
<?php get_stylesheet_directory_uri(); ?>
invece delpercorsoeffettivo.Qualcheidea?Graziein anticipo!