<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: codingfix</title>
    <description>The latest articles on DEV Community by codingfix (@codingfix1).</description>
    <link>https://dev.to/codingfix1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F40200%2F10f35908-14a8-4e6b-b13d-0cffafaae636.jpg</url>
      <title>DEV Community: codingfix</title>
      <link>https://dev.to/codingfix1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codingfix1"/>
    <language>en</language>
    <item>
      <title>Announcing Language Switcher for Transposh plugin</title>
      <dc:creator>codingfix</dc:creator>
      <pubDate>Mon, 22 Feb 2021 12:54:54 +0000</pubDate>
      <link>https://dev.to/codingfix1/announcing-language-switcher-for-transposh-plugin-53df</link>
      <guid>https://dev.to/codingfix1/announcing-language-switcher-for-transposh-plugin-53df</guid>
      <description>&lt;p&gt;As many other developers, when I discovered Transposh plugin I immediately fell in love with it! It allows automatic translations out of the box but it also gives you a granular control on the translated text, allowing you to edit every single phrase.&lt;br&gt;
Okay, you already know that so it's not necessary for me to repeat here why we all love Transposh so much.&lt;/p&gt;

&lt;p&gt;But I have to confess something: I was not happy with the language switcher widget. I develop small websites and usually I need to use from 2 to 4 different languages. Building non-WordPress websites, I used to put just some flag in the main navigation menu and  wished I could do the same using WordPress and Transposh.&lt;/p&gt;

&lt;h2&gt;
  
  
  The artisan way
&lt;/h2&gt;

&lt;p&gt;At first, to get that result, I used a couple of useful plugins and just a bit of javascript.&lt;/p&gt;

&lt;p&gt;I won't waste your time here to talk about this: if you're interested you can find a detailed description &lt;a href="https://dev.to/codingfix1/how-to-use-a-custom-language-switcher-with-transposh-to-build-a-multi-language-wordpress-website-4hom"&gt;here&lt;/a&gt; and &lt;a href="https://www.codingfix.com/how-to-use-a-custom-language-switcher-with-transposh-to-bulid-a-multi-language-wordpress-website/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The WordPress way
&lt;/h2&gt;

&lt;p&gt;The "artisan way" was absolutely boring to me: for each new website I had to repeat every step just to get 2 or 3 flags in my menu. I wanted to get my flags just installing a plugin and maybe adjusting some settings... But that plugin didn't exist, so I finally decided I had to go beyond my limits, meet the challenge head-on and create my own plugin.&lt;/p&gt;

&lt;p&gt;Today I'm proud to present Language Switcher for Transposh. It's not magic, it doesn't do miracles but it gets the job done.&lt;/p&gt;

&lt;p&gt;I'm very grateful to Ofer, who invited me to present my little creature in his &lt;a href="http://transposh.org/en/"&gt;blog&lt;/a&gt;: thank you, Ofer, for your kindness, I really appreciate this opportunity to let Language Switcher for Transposh be known.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, what Language Switcher for Transposh actually does?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;it reads Transposh settings and gets the list of languages used in the current website&lt;/li&gt;
&lt;li&gt;it reads all menu locations available in the current theme and allows you to choose where the language switcher will show up through simple checkboxes&lt;/li&gt;
&lt;li&gt;it allows you to add at the end of the chosen menu(s) a series of flags or a dropdown menu to select the language; Administrators, Authors and Editors will see an Edit translation button too  which will allow them to activate the Transposh Translation Editor&lt;/li&gt;
&lt;li&gt;if you choose to use only flags, it allows you to choose between Transposh flags or flags provided by Language Switcher for Transposh itself&lt;/li&gt;
&lt;li&gt;if you choose to use a dropdown you can choose if using a select or an unordered list to build your dropdown: I added this option because unordered list give you much more options to customize their look and feel than a select&lt;/li&gt;
&lt;li&gt;if you use an unordered list as dropdown, you can choose if the list items will show flag only, text only or both flags and text&lt;/li&gt;
&lt;li&gt;it allows you set additional classes for your language switcher menu items: this allows you to make it look accordingly to your theme style using the same class your theme is using for navigation menu items&lt;/li&gt;
&lt;li&gt;it allows you to totally customize your language switcher using a css editor with syntax highlighting: current stylesheet is loaded in the editor and you can just modify it and then save it or you can even create a totally new css file. with a custom name (it defaults to custom.css)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What about the future?
&lt;/h2&gt;

&lt;p&gt;I already have a TODO list to add more features and maybe to create even a premium version, but I think that Language Switcher for Transposh will make your life easier in this first release already. Or at least, this is what I very much hope!&lt;/p&gt;

&lt;p&gt;You can find &lt;a href="https://wordpress.org/plugins/language-switcher-for-transposh"&gt;Language Switcher for Transposh&lt;/a&gt; in WordPress.org website (or just searching for "transposh" in the admin dashboard of your WordPress installation): give it a try and feel free to contact me for any issues you can get into. And obviously, if you like it, don't forget to give it some star (I still didn't learn how to put in the dashboard those annoying invitations to rating LOL).&lt;/p&gt;

&lt;p&gt;Thank you all for reading.&lt;/p&gt;

&lt;p&gt;Good coding!&lt;/p&gt;

&lt;p&gt;Sincerely,&lt;br&gt;
Marco Gasi by Codingfix&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>wordpress</category>
    </item>
    <item>
      <title>How to develop locally a Laravel app using Laragon</title>
      <dc:creator>codingfix</dc:creator>
      <pubDate>Tue, 24 Mar 2020 19:25:56 +0000</pubDate>
      <link>https://dev.to/codingfix1/how-to-develop-locally-a-laravel-app-using-laragon-1om9</link>
      <guid>https://dev.to/codingfix1/how-to-develop-locally-a-laravel-app-using-laragon-1om9</guid>
      <description>&lt;p&gt;In this article I'll show you how to develop locally a new Laravel app using &lt;strong&gt;Laragon&lt;/strong&gt;.&lt;br&gt;
In the far 2017 I wrote an article about how to setup Laravel Homestead: the process is not as quick and easy as you can wish but following each step you can end up with a working development environment for Laravel. At that time, Laragon was already 2 years old but I never heard anything about it. And other 2 years should go by before I see the Laragon light.&lt;br&gt;
As you can read in &lt;a href="https://laragon.org/docs/"&gt;Laragon repo&lt;/a&gt;,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Laragon is a portable, isolated, fast &amp;amp; powerful universal development environment for PHP, Node.js, Python, Java, Go, Ruby.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In just a few clicks it will install in your Windows* machine everything you need to develop web applications in your local environment: Apache, Nginx, MySql and more (for a full detailed list refer to &lt;a href="https://laragon.org/docs/"&gt;Laragon page&lt;/a&gt;, please). In other words, just forget WAMP, XAMPP or any other tool you've used till now: Laragon is incredibly fast and offers a range of tools and features that let you focus on develop your apps, doing all the dirty work for you. And you'll can access it using a pretty url like &lt;em&gt;your-app-name.test&lt;/em&gt; not the horrible localhost/your-app-name.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Unfortunately, Laragon is available only on Windows. if you use a Linux box and you wish to take a look at this wonderful tool, you can use one of &lt;a href="https://www.lifewire.com/tools-to-run-windows-programs-in-linux-4120713"&gt;these packages&lt;/a&gt; to run Windows software on Linux&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One of the features I love more is the Quick app command: with just a click you can get your new application up and running, ready to be developed by you. Today, I want to show how easy it can be start a new Laravel application using Laragon, and how to add Bootstrap to it.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1: install Laragon
&lt;/h2&gt;

&lt;p&gt;Go to &lt;a href="https://laragon.org/download/"&gt;Laragon download page&lt;/a&gt;, download the last version then just run the installer: &lt;em&gt;Next&lt;/em&gt;, &lt;em&gt;Next&lt;/em&gt;, &lt;em&gt;Next&lt;/em&gt;...&lt;br&gt;
Laragon starts instantly and I recommend to set it up to run when Windows starts.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: create a Laravel application
&lt;/h2&gt;

&lt;p&gt;Just a right click on the Laragon window or on the Laragon tray icon to open a rich menu: just click on Quick app-&amp;gt;Laravel, type the name of your new application (in this article I'll use "laraone") and let Laragon work for you.&lt;br&gt;
A command prompt will be open and you'll see the process going on. If you have installed Laragon with the defaults, a new folder with your app name will be created in C:\laragon\www directory and everything will be installed there.&lt;br&gt;
When the process is completed you should see something like this in your command prompt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;***** NOTE: Now, you can use pretty url for your awesome project :) *****
-------------------------------------------------------
(Laragon) Project path: C:/laragon/www/test
(Laragon) Pretty url: http://test.test
-------------------------------------------------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes, in Windows the command prompt seems to stuck, with a blinking cursor in a black desert: if this is the case click on the Laravel tray icon and this should just fix the issue. Otherwise reload Laragon.&lt;/p&gt;

&lt;p&gt;If you go to C:\laragon\etc\apache2\sites-enabled folder, you will see that Laragon has created a new configuration file called auto.laraone.test.conf. In this file is defined the Virtual Host for your new application. You can take a look just to see how Laragon works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: install node_modules
&lt;/h2&gt;

&lt;p&gt;Now Laravel is already installed and ready to start but I want to use Bootstrap and to install it I need to install npm first.&lt;br&gt;
The Windows command prompt should already be in your new application directory: if this is not the case, move there manually or just open a new command prompt in C:\laragon\www\laraone directory and type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --no-bin-links
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This installs node_modules in your application&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: fix package.json
&lt;/h2&gt;

&lt;p&gt;Now we have to correctly set the path to cross-env.js in package.json file.&lt;br&gt;
Open the file package.json and replace the constant "cross-env" with the actual path to cross-env.js: in my last installation cross-env.js was located in node_modules/cross-enc/src/bin.&lt;br&gt;
The  final result should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"private"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npm run development"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"development"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node node_modules/cross-env/src/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"watch"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npm run development -- --watch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"watch-poll"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npm run watch -- --watch-poll"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"hot"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node node_modules/cross-env/src/bin/cross-env.js NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"prod"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npm run production"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"production"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node node_modules/cross-env/src/bin/cross-env.js NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"devDependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"axios"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^0.19"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"cross-env"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^5.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"laravel-mix"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^4.0.7"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"lodash"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^4.17.13"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"resolve-url-loader"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.3.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"sass"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^1.15.2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"sass-loader"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^7.1.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"vue-template-compiler"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.6.11"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: installing Bootstrap
&lt;/h2&gt;

&lt;p&gt;To install Bootstrap we just need to type some more commands in the command prompt:&lt;/p&gt;

&lt;p&gt;a) &lt;code&gt;composer require laravel/ui&lt;/code&gt;: it can takes some time&lt;br&gt;
b) &lt;code&gt;php artisan ui bootstrap&lt;/code&gt;: this will actually install bootstrap&lt;br&gt;
c) as required, run &lt;code&gt;npm install &amp;amp;&amp;amp; npm run dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's all! Now Bootstrap is installed and all its stuff is in C:\laragon\www\laraone\resources\sass\app.scss and in C:\laragon\www\laraone\public\css\app.css.&lt;br&gt;
Reload Laragon once ore time and type laraone.test in your browser.&lt;br&gt;
Ready to go to write your code!&lt;/p&gt;

&lt;p&gt;More about Laragon and its magic in a next article. Because this is important to notice: Laragon is not only Laravel: Wordpress, Joomla, Drupal, PrestaSHop, CakePhp... And it's not only about Php, not only about MySql, not only about... Okay, in a next article :)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
    </item>
    <item>
      <title>How to use a custom language switcher with Transposh to build a multi-language Wordpress website</title>
      <dc:creator>codingfix</dc:creator>
      <pubDate>Sun, 08 Mar 2020 12:01:33 +0000</pubDate>
      <link>https://dev.to/codingfix1/how-to-use-a-custom-language-switcher-with-transposh-to-build-a-multi-language-wordpress-website-4hom</link>
      <guid>https://dev.to/codingfix1/how-to-use-a-custom-language-switcher-with-transposh-to-build-a-multi-language-wordpress-website-4hom</guid>
      <description>&lt;p&gt;In this article you’ll learn how to use a custom language switcher with Transposh to build a multi-language website with WordPress.&lt;/p&gt;

&lt;p&gt;When it comes to build a multi-language website, WordPress offers a lot of different plugins: WPML, Polyglot and so on.&lt;/p&gt;

&lt;p&gt;All these plugins have a free version with relevant limitations: if you want a fully functional plugin you must buy the Premium version.&lt;/p&gt;

&lt;p&gt;But this is not true for &lt;a href="https://wordpress.org/plugins/transposh-translation-filter-for-wordpress/"&gt;Transposh&lt;/a&gt;: Transposh is totally free, forever, without any limitation (in number of languages, number of websites or anything else). And it works great:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;install Transposh and activate it&lt;/li&gt;
&lt;li&gt;set your default language in Worpdress Settings-&amp;gt;General section&lt;/li&gt;
&lt;li&gt;choose the languages you want to use in Transposh-&amp;gt;Languages&lt;/li&gt;
&lt;li&gt;In Transposh-&amp;gt;Settings set who can edit translations (tipically they will be Administrators and Editors)&lt;/li&gt;
&lt;li&gt;Enable url rewriting&lt;/li&gt;
&lt;li&gt;Set WordPress Settings-&amp;gt;Permalink to Post name (/%postname%/)&lt;/li&gt;
&lt;li&gt;that’s all. Your text will be automatically translated in the selected language&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition, Transposh let you fix any translation error in a quick and easy way! So I’ll show you How to use a custom language switcher with Transposh to build a multi-language website with WordPress.&lt;/p&gt;

&lt;p&gt;But… Always there is a “but” &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2EAkP1Kt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f642.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2EAkP1Kt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f642.png" alt="🙂"&gt;&lt;/a&gt; First, to use the full widget you must upgrade to fully version: no problem, is free.  Go to Transposh section of the dashboard and choose Settings, then check the “Allow upgrading to full version from &lt;a href="http://transposh.org"&gt;http://transposh.org&lt;/a&gt;, which has no limit on languages used and includes a full set of widgets” checkbox and click Save changes button. After some time you’ll be notified there is an update available for Transposh: update and you are ok.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SGKQKyUV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/flag_widget2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SGKQKyUV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/flag_widget2.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once Transposh has been updated to the full version, you finally get the widget and… As usually, it happens that the widget probably doesn’t integrate in your website design, requiring a lot of work to add custom CSS, trying to style it in a coherent way with the rest of your styles. And last but not least, English language is represented by the United States flag! Don’t know why, after all english is english and it was born in UK. So, what to do? Well, we’re going to build our custom language switcher.&lt;/p&gt;

&lt;h2&gt;
  
  
  What exactly we need?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--blu6z-ko--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/flag_widget3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--blu6z-ko--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/flag_widget3.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s see how Transposh works. When you click on the flag of a non-default language, Transposh reloads the page and changes its own aspect. As you can see, a checkbox appears immediately under the flags: checking it will reloads the page in Edit mode. Depending on Transposh settings (leave them to default if you don’t have any specific issue with this), every text in the page will have a small yellow button appended: clicking on that button will show a popup to edit the translation or just approve them (approved translations will have a green button instead of a yellow one)&lt;/p&gt;

&lt;p&gt;So, summarizing, first we have to provide our user an easy and possibly beautiful way to switch between languages; secondly, we have to provide to administrators and editors a quick way to put the website in edit mode to fix translations’ errors. To do this will follow these steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;we’ll set some WordPress setting&lt;/li&gt;
&lt;li&gt;we’ll upload to our WordPress installation the flags’ images we need&lt;/li&gt;
&lt;li&gt;we’ll install a couple of plugins&lt;/li&gt;
&lt;li&gt;we’ll write a bit of code&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Custom language switcher step by step
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: WordPress settings
&lt;/h3&gt;

&lt;p&gt;First, as I said before, go to Settings-&amp;gt;Permalink and set it to Post name (%postname%).&lt;/p&gt;

&lt;p&gt;Then, go o Appearance-&amp;gt;Menus, open Screen options clicking on the top-right tab and check the CSS Classes checkbox as shown in the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jiUbhlnm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/enable_menu_class-1024x439.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jiUbhlnm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/enable_menu_class-1024x439.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: the flags
&lt;/h3&gt;

&lt;p&gt;First we need to get our flag icons. Just google a bit and you’ll find a lot of free flag icons sets: choose your favorite one download it and upload the flags to your website using the Media section of WordPress dashboard. In thus article I’ll use three flags, english, spanish (because I live in Spain) and italian (because I’m from Italy), but obviously feel free to use the flags you really need to use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: some plugins
&lt;/h3&gt;

&lt;p&gt;In order to use our flags we’re going to use a couple of useful plugins (even these ones are totally free).&lt;/p&gt;

&lt;h4&gt;
  
  
  Plugin 1: Menu Icon by ThemeIsle
&lt;/h4&gt;

&lt;p&gt;This small adorable plugin (&lt;a href="https://wordpress.org/plugins/menu-icons/"&gt;https://wordpress.org/plugins/menu-icons/&lt;/a&gt;) allows you yo set any icon or image you want to associate it to a menu item. The plugin allows to choose if the menu item text must be visible or hidden, so we can add to our menu the 2 flags quickly and easily.&lt;/p&gt;

&lt;p&gt;That’s all for now.&lt;/p&gt;

&lt;h4&gt;
  
  
  Plugin 2: If Menu
&lt;/h4&gt;

&lt;p&gt;Now we have to add another menu item to switch on or off the option to edit the translation if we are not to happy with it. This function works out of the box with Transposh widget but since we’re not using it we’ll have to implement it manually.&lt;/p&gt;

&lt;p&gt;Technically, the &lt;a href="https://wordpress.org/plugins/if-menu/"&gt;If Menu&lt;/a&gt; plugin is not strictly necessary: I use it because I don’t want visitors can change the website text as they want and I suppose that the majority of you will do the same. If Menu plugin just do this: it allows to add a conditional logic so you can show or hide single menu items depending on the user’s role.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 4: building the menu
&lt;/h4&gt;

&lt;p&gt;Okay, now we’re ready to build our menu. Go to Appearance-&amp;gt;Menus.&lt;/p&gt;

&lt;h4&gt;
  
  
  Settings for Menu Icon
&lt;/h4&gt;

&lt;p&gt;When you are in Menus section, you’ll see a new element in Add menu items left side bar: Menu Icon Settings. Open it, uncheck the pre-selected option Dashicons (we don’t need it) and check the Images option like in the image to the left.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UuhuXFgV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/menuiconsettings1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UuhuXFgV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/menuiconsettings1.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then switch to the Current Menu tab and set Hide Label to Yes. Finally, just Save Settings!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NRUlg5dq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/menuiconsettings2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NRUlg5dq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/menuiconsettings2.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we have completed these preliminary tasks, we can start to add menu items to our menu.&lt;/p&gt;

&lt;h4&gt;
  
  
  Adding the language switcher’s menu items
&lt;/h4&gt;

&lt;p&gt;We’ll add 3 menu items with the flags to allow users to switch between our three supported languages. Then we’ll add a fourth item to enable/disable translation editing (for this menu item we’ll use the conditional logic offered by If Menu plugin. For each menu item we’ll use Custom links.&lt;/p&gt;

&lt;p&gt;The image below shows you how to set the menu item for your default language (in our case it will be english). Choose Custom links as Menu item type, type the base url of your website as URL and ‘en’ (without quotes) as Link text.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--doO8ieru--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/customlink1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--doO8ieru--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/customlink1.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yes, it’s true! I’m implementing multilanguage feature for this blog writing this article: thrilling, isn’t it?&lt;/p&gt;

&lt;p&gt;Okay, now the menu item should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cLNEJxgx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/menuitem1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cLNEJxgx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/menuitem1.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do you see? There is a row which says Icon: Select. Click on Select to upload your flag (or to select it from you Media library if you had already uploaded it).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Yn60rbKq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/menuitem2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Yn60rbKq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/menuitem2.jpg" alt=""&gt;&lt;/a&gt;Do you see the Union Jack?&lt;/p&gt;

&lt;p&gt;Now, in the input box CSS Classes (optional) type ‘no_translate’ (again, without quotes).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WBl8paXV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/menuitem3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WBl8paXV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/menuitem3.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do the same for each language you want to use. The only thing you have to change is the URL in your Custom link: add ‘/it’ for italian, ‘/es’ for spanish and so on…&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--678qtZz6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/menuitem4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--678qtZz6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/menuitem4.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Save you menu and take a look at your website: it is already a multi-language website!!!&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating the “Edit mode” menu item
&lt;/h4&gt;

&lt;p&gt;But automatic translations are often poor, so we need a way to change and correct them. To do this, we’ll add a new Custom link to our menu: I called it Edit on/off. We won’t put anything in URL field and we’ll have to check the ‘ Enable visibility rules ‘ checkbox to make this item visible only to Administrators and to Editors. In addition, we have to add the class ‘.edit-translation’ which we’ll use to handle the click event and enable the edit mode.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://codingfix.com/wp-content/uploads/2020/03/menuitem5-1.jpg"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UbF0YLhv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2020/03/menuitem5-1.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Notice:&lt;/strong&gt; this menu item will appear only when you browse the website in a secondary language: Transposh is smart enough to know that there is no translation to fix for the default language!&lt;/p&gt;

&lt;p&gt;But this item has no URL, so it won’t do anything… To make it works we have to write a bit of javascript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: a bit of coding
&lt;/h3&gt;

&lt;p&gt;So we have our main menu with 3 flags (that already work fine: just give it a try) and the Edit on/off item to enable the manual editing of the translated text; this last menu item will be visible only if the user will be logged as Administrator or Editor.&lt;/p&gt;

&lt;p&gt;But in order to make this menu item work correctly we need to add some javascript. Run your editor code of choice and create a new file; then write in this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;urlParam&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;RegExp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s1"&gt;?&amp;amp;]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;=([^&amp;amp;#]*)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.edit-translation&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;currentOrigin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;currentPath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;param&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;urlParam&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tpedit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;newUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;newUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentOrigin&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currentPath&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;?tpedit=1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;href&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newUrl&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;newUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentOrigin&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currentPath&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;href&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newUrl&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newUrl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Quick explanation: when the user clicks on our menu item ‘Edit on/off’, first we prevent the default event, that is reload the current page. Then the code uses window.location.origin and window.location.pathname to get current base url and current path.&lt;/p&gt;

&lt;p&gt;For instance, if we are in &lt;a href="https://codingfix.com/how-to-use-custom-flags-languages-switcher-with-transposh/"&gt;https://codingfix.com/how-to-use-custom-flags-languages-switcher-with-transposh/&lt;/a&gt; currentOrigin will be &lt;a href="https://codingfix.com"&gt;https://codingfix.com&lt;/a&gt; whereas currentPath will be how-to-use-custom-flags-languages-switcher-with-transposh/. But if we were in &lt;a href="https://codingfix.com/es/how-to-use-custom-flags-languages-switcher-with-transposh/"&gt;https://codingfix.com/es/how-to-use-custom-flags-languages-switcher-with-transposh/&lt;/a&gt;, currentPath would be es/how-to-use-custom-flags-languages-switcher-with-transposh/.&lt;/p&gt;

&lt;p&gt;Once we have the components of the current url we check if the string ‘tpedit’ is present in the url as a parameter. This parameter is used by Transposh to enable edit mode for translations: when you use the widget and check the Edit translation checkbox the page is reloaded with the param ‘tpedit=1’ appended to the url. So we have to check if we are in edit mode: if we are, we create a new url using only currentOrigin and currentPath and we redirect the browser to this url (that is we reload the page removing the param ‘tpedit’); otherwise we build the new url appending the param ‘tpedit=1’ and reload the page in edit mode.&lt;/p&gt;

&lt;p&gt;Now save the file with the name you prefer: I’ll use the name ‘myscript.js’ because I’ve no time to waste thinking to a nicer name &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2EAkP1Kt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f642.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2EAkP1Kt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f642.png" alt="🙂"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have to upload this file into our theme folder, preferably in a subfolder called ‘js’. By the way, I strongly recommend you to use a child theme because any change you do to the theme files will be lost when the theme will be updated (if you don’t know what is a child theme you can read this: &lt;a href="https://www.wpbeginner.com/beginners-guide/wordpress-child-theme-pros-cons/"&gt;https://www.wpbeginner.com/beginners-guide/wordpress-child-theme-pros-cons/&lt;/a&gt; To easily create a child theme from your current theme you can use the plugin &lt;a href="https://wordpress.org/plugins/child-theme-configurator/"&gt;Child Theme Configurator&lt;/a&gt;).&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 6: loading our script
&lt;/h4&gt;

&lt;p&gt;Now we have to modify the file functions.php in order to load our script. The code to be used is slightly different depending on wheter you use a child theme or not.&lt;/p&gt;

&lt;p&gt;If you are using a child theme (recommended) you have to add to you functions.php the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;load_custom_javascript&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nf"&gt;wp_enqueue_script&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'myscript'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//script name&lt;/span&gt;
        &lt;span class="nf"&gt;get_stylesheet_directory_uri&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'/js/myscript.js'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//complete path to the file&lt;/span&gt;
        &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'jquery'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;//array of dependencies&lt;/span&gt;
        &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//version number&lt;/span&gt;
        &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="c1"&gt;//put script in footer&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;add_action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'wp_enqueue_scripts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'load_custom_javascript'&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you love to live dangerously and you’re not using a child theme, just replace get_stylesheet_directory_uri() with get_template_directory_uri().&lt;/p&gt;

&lt;h3&gt;
  
  
  One step more…
&lt;/h3&gt;

&lt;p&gt;Currently, when the user clicks on a flag to change the language, our code will load the home page in the selected language. What about if we want to reload the current page allowing our visitor to continue browsing without interruption?&lt;/p&gt;

&lt;p&gt;To achieve this we have to make some changes to both our menu and javascript code.&lt;/p&gt;

&lt;h4&gt;
  
  
  a) The menu
&lt;/h4&gt;

&lt;p&gt;In the Wordpress dashboard go to Appearence→Menus and expand the menu item we use for the default language. First, replace the URL with the hashtag symbol #: we’ll write some javascript code to set the new url the user will be brought to. Then we have to  add a couple of classes to manage the click() event; in the CSS class box, where we had added the no_translate class,  add these 2 classes: flag and flag_en. Actually, we could skip avoiding the latter for the default language, but… Okay, I love the symmetry, so I add that class to the default language item too 🙂 &lt;/p&gt;

&lt;p&gt;Do the same for any other language switcher item you have in your menu. In our example they were Spanish and Italian: to the former add the classes flag and flag_es,  for the latter add the classes flag and flag_it.&lt;/p&gt;

&lt;p&gt;Don’t forget to replace each URL with the hashtag symbol.&lt;/p&gt;

&lt;p&gt;With the menu we have done. Let’s going on!&lt;/p&gt;

&lt;h4&gt;
  
  
  b) The javascript
&lt;/h4&gt;

&lt;p&gt;Now we have to add an event handler to our script in order to manage the click() event on our language switchers.&lt;/p&gt;

&lt;p&gt;Here there is the fully commented code you have to add to your script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;      &lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.flag&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//first we prevent the default click event&lt;/span&gt;
        &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="c1"&gt;//next we declare the variable selectedLang and we give it a value accordingly to its CSS class&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;selectedLang&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;hasClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;flag_es&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;selectedLang&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;es&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;hasClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;flag_it&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;selectedLang&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;it&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// the variable newUrl will hold the url the user will be redirected to&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;newUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;//in the supportedLang we put supported languages except the default language: this because for the default language no prefix is added to the URL by Transposh. We also add a final slash but don't worry: I'll explain why soon :)&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;supportedLang&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;es/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;it/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="c1"&gt;//in currentUrl variable will put the full url of the current page (e.g. https://mydomain.com/it/how-to-cook-fried-eggs)&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;currentUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;//window.location.origin give us the base url, in our example htpps://mydomain.com&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;currentOrigin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;//window.location.pathname gives us the rest of the currentUrl, i.e. /it/how-to-cook-fried-eggs&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;currentPath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;//the second and the third character of the currentPath will but we want event the fourth to be sure it be a slash. Why? Okay, suppose we are in our default language and we are reading an article called "estimation-of-non-liear-equation-system". The currentUrl would be https://mydomain.com/estimation-of-non-liear-equation-system and the currentPath would be /estimation-of-non-liear-equation-system; so, if we used only the second and the third character to get current language we'd get 'es' while we are in the English section of the website! So we take even the fourth character and then we'll use this value to see if it is in the supported languages array&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;currentLang&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentPath&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;//then we want to save in a variable the current page without any language prefix&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;currentPage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentLang&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;supportedLang&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;currentPath&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentPath&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;currentPath&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;//finally we can build the url the user will be redirected to checking currentLang and selectedLang values; this code is quite self-explanatory&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentLang&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;supportedLang&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;selectedLang&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;supportedLang&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="nx"&gt;newUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentOrigin&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;selectedLang&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currentPage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentLang&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;supportedLang&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;selectedLang&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;supportedLang&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;newUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentOrigin&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currentPage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentLang&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;supportedLang&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;selectedLang&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;supportedLang&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;newUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentOrigin&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;selectedLang&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currentPage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentLang&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;supportedLang&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;selectedLang&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;supportedLang&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;newUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentUrl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newUrl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, when the user will click to a flag to change the website language, current page will be reloaded in the selected language instead of redirect the user to the home page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finally we’ve got it!
&lt;/h2&gt;

&lt;p&gt;Okay, that’s all folks! Now you can start to fix automatic translations. Me too, I fear…&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://codingfix.com/how-to-use-a-custom-language-switcher-with-transposh/"&gt;How to use a custom language switcher with Transposh&lt;/a&gt; appeared first on &lt;a href="https://codingfix.com"&gt;codingfix&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>wordpress</category>
    </item>
    <item>
      <title>Installing phpDocumentor issues solved!</title>
      <dc:creator>codingfix</dc:creator>
      <pubDate>Tue, 31 Jul 2018 13:36:46 +0000</pubDate>
      <link>https://dev.to/codingfix1/installing-phpdocumentor-issues-solved-c8o</link>
      <guid>https://dev.to/codingfix1/installing-phpdocumentor-issues-solved-c8o</guid>
      <description>&lt;p&gt;Installing phpDocumentor should be easy. At least, I read here and there in the Internet, besides of course in the official website,  that you have many options to install it, one easier of the previous one!&lt;/p&gt;

&lt;p&gt;So I was absolutely confident that in a bunch of minutes I'd have been able to run phpDocumentor and create the documentation for my new Php class.&lt;/p&gt;

&lt;p&gt;Do you know? I was wrong. This is what happened.&lt;/p&gt;

&lt;h2&gt;
  
  
  phpDocumentor.phar
&lt;/h2&gt;

&lt;p&gt;The first thing I tried it has been to use the .phar file. I know the first way they suggest is to use PEAR, but I have had a positive experience about using .phar files just a day before: I wanted to have php-cs-fixer setup and running in Atom and after several attempts I ended up using php-cs-fixer.phar file. So, when ti came the moment to install phpDocumentor I told myself: "why should I have to struggle with the command line, dependencies issues and the rest when I can just download a single, wonderful file and put in my php directory?" Done! Once I had phpDocumentor.phar installed, I have opened the command prompt directly in my class folder and typed&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;phpdoc &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;"."&lt;/span&gt; &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="s2"&gt;"./docs"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;BUUUZZZZ!!!&lt;/p&gt;

&lt;p&gt;Could not open input file: "phpDocumentor.phar"&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing phpDocumentor with Composer
&lt;/h2&gt;

&lt;p&gt;Wooops, what's this? Started googling a bit, but with no success, so I sadly abandoned the .phar file and I went on with Composer. worst than before!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;phpdoc is not recognized as an internal or external command
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To Google or not to Google? No, I don't want to waste time to understand why something is told to work doesn't work (not talking about Composer itself, which is a great tool I use in several occasions, just talking about installing phpDocumentor via Composer). Filed attempt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing phpDocumentor with PEAR
&lt;/h2&gt;

&lt;p&gt;I always had good experience installing php extensions through PEAR so I thought it would have worked even installing phpDocumentor. I typed the following command and I eagerly awaited the response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pear channel-discover pear.phpdoc.org
pear &lt;span class="nb"&gt;install &lt;/span&gt;phpdoc/phpDocumentor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wow, everything seemed to go fine! Once the process has finished, I have done another attempt to create my documentation typing the usual command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;phpdoc &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;"."&lt;/span&gt; &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="s2"&gt;"./docs"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Aaarrghhh!!! The command prompt window has been filled with a lot of warnings and errors related to Smarty templates!&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;I will spare you the details. I just say that thirty/forty minutes of googling gave me the solution: downloading the version 2.9.0 directly from Github. I went to &lt;a href="https://github.com/phpDocumentor/phpDocumentor2/releases/tag/v2.9.0"&gt;https://github.com/phpDocumentor/phpDocumentor2/releases/tag/v2.9.0&lt;/a&gt; and clicked to the compressed file with the name phpDocumentor-2.9.0.tgz to download it and... Don't ask me why, maybe that day I was terribly unlucky, but the download was terribly slow, it stopped several times and once it has been completed, the archive was corrupted!&lt;/p&gt;

&lt;h2&gt;
  
  
  The true solution!
&lt;/h2&gt;

&lt;p&gt;Fortunately I have found another website which allows to download that file: &lt;a href="https://fossies.org/linux/www/phpDocumentor-2.9.0.tgz/"&gt;https://fossies.org/linux/www/phpDocumentor-2.9.0.tgz/&lt;/a&gt;. Finally, I've got the file. I put it in a temporary folder, opened the command prompt and simply typed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pear &lt;span class="nb"&gt;install &lt;/span&gt;phpDocumentor-2.9.0.tgz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A small adjustement to the file phpdoc.bat is still missing. Go in your php folder (In my Windows 10 partition I have installed xampp, so my php is located in C:\xampp\php folder) and open phpdoc.bat with a text editor. It should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;@echo off
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s2"&gt;"%PHPBIN%"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt; &lt;span class="nb"&gt;set &lt;/span&gt;&lt;span class="nv"&gt;PHPBIN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;php.exe
&lt;span class="k"&gt;if &lt;/span&gt;not exist &lt;span class="s2"&gt;"%PHPBIN%"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s2"&gt;"%PHP_PEAR_PHP_BIN%"&lt;/span&gt; neq &lt;span class="s2"&gt;""&lt;/span&gt; goto USE_PEAR_PATH
GOTO RUN
:USE_PEAR_PATH
&lt;span class="nb"&gt;set &lt;/span&gt;&lt;span class="nv"&gt;PHPBIN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;%PHP_PEAR_PHP_BIN%
:RUN
&lt;span class="s2"&gt;"%PHPBIN%"&lt;/span&gt; &lt;span class="s2"&gt;"%PHP_PEAR_BIN_DIR%&lt;/span&gt;&lt;span class="se"&gt;\p&lt;/span&gt;&lt;span class="s2"&gt;hpdoc"&lt;/span&gt; %&lt;span class="k"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just change the last line replacing "%PHP_PEAR_BIN_DIR%\phpdoc"  with the plain full path to your phpdoc file. In my case, the resulting phpdoc.bat file looks liek this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;@echo off
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s2"&gt;"%PHPBIN%"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt; &lt;span class="nb"&gt;set &lt;/span&gt;&lt;span class="nv"&gt;PHPBIN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;php.exe
&lt;span class="k"&gt;if &lt;/span&gt;not exist &lt;span class="s2"&gt;"%PHPBIN%"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s2"&gt;"%PHP_PEAR_PHP_BIN%"&lt;/span&gt; neq &lt;span class="s2"&gt;""&lt;/span&gt; goto USE_PEAR_PATH
GOTO RUN
:USE_PEAR_PATH
&lt;span class="nb"&gt;set &lt;/span&gt;&lt;span class="nv"&gt;PHPBIN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;%PHP_PEAR_PHP_BIN%
:RUN
&lt;span class="s2"&gt;"%PHPBIN%"&lt;/span&gt; &lt;span class="s2"&gt;"C:&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;ampp&lt;/span&gt;&lt;span class="se"&gt;\p&lt;/span&gt;&lt;span class="s2"&gt;hp&lt;/span&gt;&lt;span class="se"&gt;\p&lt;/span&gt;&lt;span class="s2"&gt;hpdoc"&lt;/span&gt; %&lt;span class="k"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all, now it works! And I've got my full documentation with my favourite template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;phpdoc &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;"."&lt;/span&gt; &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="s2"&gt;"./docs"&lt;/span&gt; &lt;span class="nt"&gt;--template&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"responsive-twig"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have found useful this article, please share it through your social networks. If you want to leave me a comment, I'll be happy to hear from you!&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://codingfix.com/installing-phpdocumentor-issues-solved/"&gt;Installing phpDocumentor issues solved!&lt;/a&gt; appeared first on &lt;a href="https://codingfix.com"&gt;codingfix&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Improving page speed: optimizing images</title>
      <dc:creator>codingfix</dc:creator>
      <pubDate>Thu, 12 Jul 2018 19:00:03 +0000</pubDate>
      <link>https://dev.to/codingfix1/improving-page-speed-optimizing-images-367e</link>
      <guid>https://dev.to/codingfix1/improving-page-speed-optimizing-images-367e</guid>
      <description>&lt;p&gt;Optimizing images for your web page images is a very important and strategically crucial task to improve the average page speed. Image size affects your page speed in several ways and we have to take care of different techniques to optimize page.&lt;/p&gt;

&lt;h2&gt;Resize your images&lt;/h2&gt;

&lt;p&gt;First, we have to take care about images' size, that is their width and height. If you're lazy, like I &lt;a href="http://codingfix.com/wp-content/uploads/2018/07/respic.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcodingfix.com%2Fwp-content%2Fuploads%2F2018%2F07%2Frespic.png" alt="Resize pictures"&gt;&lt;/a&gt;am ;), you're likely to let css do the job for you: you just upload your image and then you write a bunch of css rules to resize it (or maybe you still even set width and height as attributes of the &amp;lt;img&amp;gt; tag!!!). This is a huge mistake: hundreds of kilobytes can be saved serving scaled images. A tool like &lt;a href="https://gtmetrix.com" rel="noopener noreferrer"&gt;GTMetrix&lt;/a&gt; will sign this as a high priority task to improve page speed.&lt;/p&gt;

&lt;p&gt;So if the image that is really displayed in the web page has a width of 300 px and a height of 120 px, don't use your original image by 1200 px x 480 px: resize it instead and use the one of the requested size. If the same image must be displayed in different sizes, upload to your server one image per each size, maybe appending a suffix which specifies width and height to the image name. And be sure to link the right image to the 'src' attribute of your &amp;lt;img&amp;gt; tags.&lt;/p&gt;

&lt;p&gt;The golden rule here is: &lt;strong&gt;don't use CSS to resize your images!&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;Combine your images&lt;/h2&gt;

&lt;p&gt;Another useful technique to use in order to optimize the loading time of our images is to use sprites. This technique has born in video games development to create animations and it can be proficiently be used in web development too.&lt;/p&gt;

&lt;p&gt;Let's say you're building a multi-language website. Probably you'll want to put somewhere in your page 2, 3 or more flags the user can click on to change language accordingly. The best way to do it is to build a unique image which holds all your flags you want to use and use css to make visible only one flag per time. Let's do it with an example.&lt;/p&gt;

&lt;p&gt;Suppose to have 3 flags images like these ones:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="http://codingfix.com/wp-content/uploads/2018/07/flag_en.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcodingfix.com%2Fwp-content%2Fuploads%2F2018%2F07%2Fflag_en.png" alt="Union Jack"&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="http://codingfix.com/wp-content/uploads/2018/07/flag_es.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcodingfix.com%2Fwp-content%2Fuploads%2F2018%2F07%2Fflag_es.png" alt="Rojigualda"&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="http://codingfix.com/wp-content/uploads/2018/07/flag_it.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcodingfix.com%2Fwp-content%2Fuploads%2F2018%2F07%2Fflag_it.png" alt="Tricolore"&gt;&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;The wrong way&lt;/h3&gt;

&lt;p&gt;We could be tempted to write something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"flags"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"lang active"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"es"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"images/flags/flag_es.png"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"32"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"32"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"lang"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"images/flags/flag_en.png"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"32"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"32"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"lang"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"it"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"images/flags/flag_it.png"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"32"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"32"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;The right way&lt;/h3&gt;

&lt;p&gt;Instead to use the three images separately nested in the three &amp;lt;anchor&amp;gt; tags which presumably will trigger the language change event, we're going to use our image editor of choice to combine the three flags in only one image file. Each flag is 32 px x 32 px so we can build an image with a width of 32 px and a height of 96 px. We'll get something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://codingfix.com/wp-content/uploads/2018/07/flags.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcodingfix.com%2Fwp-content%2Fuploads%2F2018%2F07%2Fflags.png" alt="Flags sprite"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can use the following html structure for our language menu:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"flags"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"lang active"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"es"&lt;/span&gt;&lt;span class="err"&gt;&amp;amp;&lt;/span&gt;&lt;span class="na"&gt;gt&lt;/span&gt;&lt;span class="err"&gt;;&amp;amp;&lt;/span&gt;&lt;span class="na"&gt;lt&lt;/span&gt;&lt;span class="err"&gt;;/&lt;/span&gt;&lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"lang"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="err"&gt;&amp;amp;&lt;/span&gt;&lt;span class="na"&gt;gt&lt;/span&gt;&lt;span class="err"&gt;;&amp;amp;&lt;/span&gt;&lt;span class="na"&gt;lt&lt;/span&gt;&lt;span class="err"&gt;;/&lt;/span&gt;&lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"lang"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"it"&lt;/span&gt;&lt;span class="err"&gt;&amp;amp;&lt;/span&gt;&lt;span class="na"&gt;gt&lt;/span&gt;&lt;span class="err"&gt;;&amp;amp;&lt;/span&gt;&lt;span class="na"&gt;lt&lt;/span&gt;&lt;span class="err"&gt;;/&lt;/span&gt;&lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;;
    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below you find the CSS rules for this piece of markup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="nf"&gt;#flags&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;list-style-type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt; &lt;span class="cp"&gt;!important&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin-bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="cp"&gt;!important&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="nf"&gt;#flags&lt;/span&gt; &lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;inline&lt;/span&gt; &lt;span class="cp"&gt;!important&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="nf"&gt;#flags&lt;/span&gt; &lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inline-block&lt;/span&gt; &lt;span class="cp"&gt;!important&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt; &lt;span class="cp"&gt;!important&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt; &lt;span class="cp"&gt;!important&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;overflow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;hidden&lt;/span&gt; &lt;span class="cp"&gt;!important&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;text-align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt; &lt;span class="cp"&gt;!important&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="nf"&gt;#flags&lt;/span&gt; &lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="nf"&gt;#es&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url(../images/flags/flags.png)&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="nb"&gt;no-repeat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="nf"&gt;#flags&lt;/span&gt; &lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="nf"&gt;#en&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url(../images/flags/flags.png)&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;32px&lt;/span&gt; &lt;span class="nb"&gt;no-repeat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="nf"&gt;#flags&lt;/span&gt; &lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="nf"&gt;#it&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url(../images/flags/flags.png)&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;64px&lt;/span&gt; &lt;span class="nb"&gt;no-repeat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do you notice the last three rules? There we set the top property of the background image: 0 for the Spanish flag, 32 for English one and 64 for the Italian one. The result will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://codingfix.com/wp-content/uploads/2018/07/flags-1.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcodingfix.com%2Fwp-content%2Fuploads%2F2018%2F07%2Fflags-1.png" alt="Language Flags"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, I have highlighted the active language with an orange circle (using the javascript to change the active item on click event, but I'll let this for another article about how to create a multi-language web pages using a couple of fantastic plugins for jQuery).&lt;/p&gt;

&lt;p&gt;What is the advantage of using sprites? As GTMetrix explains,&lt;/p&gt;

&lt;blockquote&gt;Combining images into as few files as possible using CSS sprites reduces the number of round-trips and delays in downloading other resources, reduces request overhead, and can reduce the total number of bytes downloaded by a web page.&lt;/blockquote&gt;

&lt;h2&gt;Compress your images&lt;/h2&gt;

&lt;h3&gt;Option 1&lt;/h3&gt;

&lt;p&gt;Be whatever it be the techniques you're using to optimize your images, it's almost sure that PageSpeed Insights (or GTMetrix or whatever other tool you're using to measure your page performances) will tell you "&lt;strong&gt;Optimize images&lt;/strong&gt;". The good news is that both GTMetrix and PageSpeed Insights allow you to download optimized images from their servers. PageSpeed Insights allows this even in a more comfortable manner: it optimizes your images for you, minifries your javascripts and Css and serves them in a compressed file ready to download: isn't it great?&lt;/p&gt;

&lt;p&gt;&lt;span&gt;&lt;em&gt;About minified javascript and css used in prioritizing content process, you can learn more reading &lt;a href="http://codingfix.com/improving-page-speed-prioritizing-content/" rel="noopener noreferrer"&gt;this article&lt;/a&gt;.&lt;/em&gt;&lt;/span&gt;&lt;/p&gt;

&lt;h3&gt;Option 2&lt;/h3&gt;

&lt;p&gt;You have even other options to compress and optimize your images, just to do your tests using already optimized content. The first option you have is to use online tools. For instance, &lt;a href="https://compressor.io/" rel="noopener noreferrer"&gt;compressor.io&lt;/a&gt; it's a fantastic tool which allow you to compress your images lossless and save hundreds of Kb, as they claim in their home page. If you use Wordpress, you can live without &lt;a href="https://wordpress.org/plugins/wp-smushit/" rel="noopener noreferrer"&gt;SmushIt plugin&lt;/a&gt; which optimizes your images while you are uploading them!&lt;/p&gt;

&lt;h3&gt;Option 3&lt;/h3&gt;

&lt;p&gt;You can also use a standalone software. The best one I have tried is certainly &lt;a href="http://luci.criosweb.ro/riot/" rel="noopener noreferrer"&gt;Riot&lt;/a&gt; (acronym which stands for Radical Image Optimization Tool): it is for Windows only, but you can easily run it from your Linux box using Wine. It does the magic! It can compress a 4500 x 3000 image of 19.4 Mb to a 5.09 Mb without resampling the image and without any visible loss of quality. If you resize the image to a more reasonable size in terms of width and height, let's say to a 1000 of width and 668 of height the new file weight will be 173.9 Kb!&lt;/p&gt;

&lt;p&gt;I hope you have found useful this article. If you have some comment or some question, feel free to post a comment or to contact me through the &lt;a href="http://codingfix.com/contacts/" rel="noopener noreferrer"&gt;Contact page&lt;/a&gt;. And if you really feel generous, don't forget to share this post on your social networks! Thank you!&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://codingfix.com/improving-page-speed-optimizing-images/" rel="noopener noreferrer"&gt;Improving page speed: optimizing images&lt;/a&gt; appeared first on &lt;a href="https://codingfix.com" rel="noopener noreferrer"&gt;codingfix&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
    </item>
    <item>
      <title>Improving page speed: prioritizing content</title>
      <dc:creator>codingfix</dc:creator>
      <pubDate>Sun, 01 Jul 2018 20:04:58 +0000</pubDate>
      <link>https://dev.to/codingfix1/improving-page-speed-prioritizing-content-49h3</link>
      <guid>https://dev.to/codingfix1/improving-page-speed-prioritizing-content-49h3</guid>
      <description>&lt;p&gt;Every web developer knows how important it is to optimize his websites' performances to offer the best user experience to our visitors. And probably almost every web developer knows what Google &lt;a href="https://developers.google.com/speed/pagespeed/insights/"&gt;PageSpeed Insights&lt;/a&gt; is and how many aspects of our website we have to monitor and adjust to get the maximum loading speed from our server. This is the first of a series of articles where I want to explain you in detail how to improve your page performance. In this first article we'll see how to increase the page loading speed prioritizing content.&lt;/p&gt;

&lt;p&gt;When you test your web page with PageSpeed Insigths, you're likely to get the following suggestion:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Reduce the size of the above-the-fold content&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What does this mean? You can read &lt;a href="https://developers.google.com/speed/docs/insights/PrioritizeVisibleContent"&gt;this page&lt;/a&gt; to know what exactly Google means, but essentially it means that you have to change the way your html is rendered. In other words, you have to load first what the user see first and defer the download of the any other resource. If you want a step-by-step how-to, go on reading.&lt;/p&gt;

&lt;h2&gt;Identifying the above-the-fold content&lt;/h2&gt;

&lt;p&gt;The first thing we have to do is to identify the section of your page which is rendered first. Personally, I do this using a resolution of 1920 x 1080 in order to be sure to involve in optimizing process the major part of the users (if what I mean is not clear, I explain: in a monitor has a resolution of 1366 x 768, as the majority of notebook has, it will display a certain section of your page; but if the monitor resolution is higher, the section of rendered page will be larger so if you have optimized your page at a low resolution, in larger monitors will be present non optimized elements and this will slow down the rendering speed).&lt;/p&gt;

&lt;p&gt;So, set your monitor resolution to the maximum value available, open your browser and navigate to your web page. Once the page is fully loaded, press F12 to open the developer tools and undock it.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;In &lt;strong&gt;Chrome&lt;/strong&gt; to undock the developer tools you just have to click the 3 dots at the top right corner and click the option shown in the below image&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codingfix.com/wp-content/uploads/2017/12/Screenshot-06_30_18-11_23_23.jpg"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9k5tg21z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2017/12/Screenshot-06_30_18-11_23_23.jpg" alt="Undocking developer tools" width="205" height="60"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;Firefox&lt;/strong&gt; the 3 dots are horizontally aligned and the menu show you 3 options: 'Dock to bottom', 'Dock to Left', 'Dock to right' and 'Separate window'&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;Microsoft Edge&lt;/strong&gt; you can just click on the icon&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codingfix.com/wp-content/uploads/2017/12/Screenshot-06_30_18-15_56_03.jpg"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vo7Xfgwj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://codingfix.com/wp-content/uploads/2017/12/Screenshot-06_30_18-15_56_03.jpg" alt="Undocking developer tools" width="162" height="22"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Once developer tools are undocked, you can see the full page section rendered by the browser and identify all that parts that are immediately available. The next step is to give the browser all the necessary information to render that content the right way, that is the css rules that must be applied.&lt;/p&gt;

&lt;h2&gt;Identifying the necessary css rules&lt;/h2&gt;

&lt;p&gt;Be sure to set the Elements tab active in Google Developer Tools window (the same in Edge, whereas in Firefox the right tab to use here is called 'Inspector'). This tab is splitted into 2 panels: at the left side you see the html markup of your document and at the rioght side the css rules that apply to the selected element. Just select each visible element of the page and be sure to copy all the rules that apply and paste them in a new css file. It doesn't matter the name you give this file, it's just a temporary file we use to collect all css rules we need to render the above the fold content.&lt;/p&gt;

&lt;p&gt;Keep in mind that if in the visible section of the page you are using some icon (such as FontAwesome icons or Foundation icons) you have to get even the rules required to display those icons!&lt;/p&gt;

&lt;h2&gt;Minifying css in the head section&lt;/h2&gt;

&lt;p&gt;Once you have all the rules required to render the above the fold content, you need to minify them, that is delete any superfluous space or blank line you have in your temporary css file. To do this you can use a plugin of yout code editor of choice, a stand-alone program or even some online service. Personally, I have used this Css minifier: it's very easy to use and it doesn't require any installation. Just copy all the content of your temporary css file (Ctrl + A, Ctrl + C) and paste it in the left side box (Ctrl + V) called "Input CSS", then click the blue Minify button and in the right side box the minified css will appear almost instantly. Copy it and paste it in the head section of your page, immediately after the last meta tag, wrapping it with the &amp;lt;style&amp;gt; tag.&lt;/p&gt;

&lt;h2&gt;Deferring the download of other resources&lt;/h2&gt;

&lt;p&gt;Now we have to deal with the rest of the css we use in our web page. First, create a new css file and call it &lt;em&gt;style.css&lt;/em&gt;. In this file we're going to past all and every css rule used in our web page. To be sure our new file preserve the right sort order of teh css rules, we'll use the sequence of the link to css files we have in our web page. If you use Bootstrap, you're likey to have the link to bootsrap before the links to any other css file, isn't it? Good, open your bootstrap.min.css, copy the whole content and paste it in your styles.css. Do the same with any other stylesheet you are using and be sure to delete the link to that file from your &amp;lt;head&amp;gt; section. If some of you css files is not minified yet, use the css minifier to minify its content and paste in your styles.css the minified version.&lt;/p&gt;

&lt;p&gt;Once done, you should end up with a huge, unreadable css file: what to do with that? We'll write a small piece of Javascript to ensure that file will be downloaded only after the entire document has been loaded. Let's do it.&lt;/p&gt;

&lt;p&gt;In the footer of your web page, immediately before the &amp;lt;/body&amp;gt; closing tag, add this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text/javascript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;downloadCssAtOnload&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;link&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text/css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;stylesheet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;css/styles.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;load&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;downloadCssAtOnload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;attachEvent&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;attachEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;onload&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;downloadCssAtOnload&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;downloadCssAtOnload&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's analyze it. First we have the function downloadCssAtOnload(). This function is simple indeed: it just create a &amp;lt;script&amp;gt; element and sets all its attribute to the desired values, then it appends it to the document. Practically, when this function will be called, the following lines of code will be written in the &amp;lt;head&amp;gt; section of our document:&lt;/p&gt;

&lt;pre&gt;&amp;lt;link type="text/css" rel="stylesheet" href="css/styles.css"&amp;gt;&lt;/pre&gt;

&lt;p&gt;The rest of the code just sets how to call the function downloadCssAtOnload() when the document has been loaded: accordingly to the user's browser features, we'll use the window.addEventListener() method, the window.attachEvent() method or the window.onload() method.&lt;/p&gt;

&lt;h2&gt;What about the javascript?&lt;/h2&gt;

&lt;p&gt;Good point! You can do exactly the same with your javascript. First create a new javascript file which holds all the javascript your web page uses (don't forget to &lt;a href="https://javascript-minifier.com/"&gt;minify&lt;/a&gt; it). Then remove alll script elements from your document footer except our small snippet above. Finally add similar code to defer the loading of the javascript. The final result should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text/javascript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;downloadCssAtOnload&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;link&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text/css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;stylesheet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;css/styles.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;downloadJSAtOnload&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;script&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;js/javascript.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;load&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;downloadJSAtOnload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;load&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;downloadCssAtOnload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;attachEvent&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;attachEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;onload&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;downloadJSAtOnload&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;attachEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;onload&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;downloadCssAtOnload&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;downloadJSAtOnload&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;downloadCssAtOnload&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The new code will append following line in the footer of your document:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;js/javascript.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all. Now retry to test your page with PageSpeed Insights and you should be able to see a relevant improvement of your score.&lt;/p&gt;

&lt;h2&gt;What else?&lt;/h2&gt;

&lt;p&gt;To further improve our page performance we have still a lot of work to do but for the moment just take a break. In the next articles of this mini-series we'll see how to optimize our images and how to setup the .htaccess.&lt;/p&gt;

&lt;p&gt;If you have some comment, feel free to write it down here: I'll be happy to hear your thoughts :)&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://codingfix.com/improving-page-speed-prioritizing-content/"&gt;Improving page speed: prioritizing content&lt;/a&gt; appeared first on &lt;a href="https://codingfix.com"&gt;codingfix&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
    </item>
  </channel>
</rss>
