wp_enqueue_script<\/a>.<\/p>\nWp_enqueue_script m\u00e1 jako prvn\u00ed argument takzvan\u00fd $handle, co\u017e je ozna\u010den\u00ed scriptu, kter\u00fd vkl\u00e1d\u00e1te. Nap\u0159\u00edklad jQuery m\u00e1 handle p\u0159ekvapiv\u011b „jquery“.<\/p>\n
Vlo\u017een\u00ed js souboru pak m\u016f\u017ee vypadat t\u0159eba takto:<\/p>\n
wp_enqueue_script('soubor', plugin_dir_url( __FILE__ ) . 'js\/soubor.js');<\/pre>\nV origin\u00e1ln\u011b pojmenovan\u00e9m souboru soubor.js budeme m\u00edt nap\u0159\u00edklad n\u011bco takov\u00e9ho:<\/p>\n
\njQuery(document).read(function($) {\n\tjQuery('#tlacitko').on('click', function() {\n\t\talert('You have clicked the button!');\n\t});\n jQuery('#druhytlacitko').on('click', function() {\n\t\talert('Not again, please!');\n\t});\n});\n<\/pre>\nP\u0159eklad \u0159et\u011bzce v js souboru<\/h3>\n
A nyn\u00ed p\u0159ich\u00e1z\u00ed na \u0159adu wp_localize_script. Nejprve si p\u0159iprav\u00edme v php souboru, kde vkl\u00e1d\u00e1me soubor.js pomoc\u00ed enqueue, pole s texty.<\/p>\n
\n$translate = array(\n 'message' => __('You have clicked the button!','textdomain'),\n 'second_message' => __('Not again, please!','textdomain')\n);\n<\/pre>\nN\u00e1sledn\u011b pou\u017eijeme v\u00fd\u0161e zm\u00edn\u011bnou funkci:<\/p>\n
\nwp_localize_script('soubor', 'script_strings', $translate);\n<\/pre>\nVysv\u011btlen\u00ed:<\/strong><\/p>\nVzpome\u0148te si na $handle z enqueue, o kter\u00e9m jsem psal v\u00fd\u0161e. To je prvn\u00ed argument, tady je to „soubor“. D\u00edky tomu v\u00ed WordPress, \u017ee toto konkr\u00e9tn\u00ed vol\u00e1n\u00ed funkce se vztahuje ke konkr\u00e9tn\u00edmu js souboru.<\/p>\n
D\u016fle\u017eit\u00e9: enqueue mus\u00ed b\u00fdt pou\u017eito ve scriptu d\u0159\u00edve, ne\u017e localize!<\/strong><\/p>\nDruh\u00fd argument funkce je objekt, do kter\u00e9 vlo\u017e\u00edte v\u0161echny \u0159etezce a jeho pou\u017eit\u00ed si uk\u00e1\u017eeme za chvilku.<\/p>\n
No a jako t\u0159et\u00ed m\u00e1me pole s texty. Kl\u00ed\u010d polo\u017eek budeme pou\u017e\u00edvat v js souboru a hodnota je uzav\u0159en\u00e1 ve funkci__(), kter\u00e1 n\u00e1m umo\u017e\u0148uje \u0159et\u011bzec p\u0159elo\u017eit, tak jak jsme zvykl\u00ed.<\/p>\n
Zp\u011bt k js souboru<\/strong><\/p>\nVytvo\u0159en\u00fd soubor uprav\u00edme takto:<\/p>\n
\njQuery(document).read(function($) {\n\tjQuery('#tlacitko').on('click', function() {\n\t\talert(script_strings.message);\n\t});\n jQuery('#druhytlacitko').on('click', function() {\n\t\talert(script_strings.second_message);\n\t});\n});\n<\/pre>\nA m\u00e1me javascript lokalizovan\u00fd. V\u0161echny \u0159et\u011bzce m\u00e1me p\u0159ipraveny v „script_strings“ a p\u0159ipojen\u00edm pomoc\u00ed te\u010dky, vypisujeme jednotliv\u00e9 texty, tak jak pot\u0159ebujeme.<\/p>\n","protected":false},"excerpt":{"rendered":"
Javascript se ve WodPressu pou\u017e\u00edv\u00e1 st\u00e1le v\u00edce a s t\u00edm tak\u00e9 vyvst\u00e1v\u00e1 ot\u00e1zka, jak\u00fdm zp\u016fsobem lokalizovat \u0159et\u011bzce v js souborech. V \u010dl\u00e1nku v\u00e1m uk\u00e1\u017ei, jak na to. Lokalizace je jednou ze siln\u00fdch str\u00e1nek WordPressu a d\u00edky tomu, jak je elegantn\u011b \u0159e\u0161en\u00e1, m\u016f\u017eete p\u0159ekl\u00e1dat pluginy a \u0161ablony do r\u016fzn\u00fdch jazyk\u016f. Samoz\u0159ejm\u011b, pouze za p\u0159edpokladu, \u017ee program\u00e1tor<\/p>\n","protected":false},"author":1,"featured_media":2405,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[12,30],"tags":[],"class_list":["post-2404","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-navody","category-wordpress"],"_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/2404"}],"collection":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/comments?post=2404"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/2404\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media\/2405"}],"wp:attachment":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media?parent=2404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=2404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=2404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}