How to add icons to the language nav menu items

How to add icons to the language nav menu items

How to add icons to the language nav menu items
This tutorial is part of our MultilingualPress 2 documentation. In case you are using the newer version 3, please switch to MultilingualPress 3.
By default, our language navigation menus show the language name in its native spelling. This intended: We want to discourage the use of flags for languages. But we do offer the option to use a custom image for each site, and if you want to use that, you can add it to the link text.
The hook for that is mlp_prepare_nav_menu_item_output. You get two variables as input:

$item: The nav menu item, an instance of the class WP_Post with some extra properties.
$translation: An instance of an implementation of the Mlp_Translation_Interface. If we couldn’t find a translation for some reason, this variable is NULL, so you have to test it before you work with it.

The $translation has a method get_icon_url() which is again an object: an instance of the Mlp_Url_Interface. It can be casted to a string to get the URL. Another way is calling the method __toString() directly. If there is an icon, the escaped URL is returned. You get an empty string otherwise.
Here is how you can use it:
/**
* Adds an icon to the menu text
*
* $item is passed as an object handle, so this function can change it directly
* and doesn’t have to return anything.
* We still return a string value to make unit tests easier.
*
* @wp-hook mlp_prepare_nav_menu_item_output
* @param WP_Post $item
* @param Mlp_Translation_Interface|NULL $translation Might be NULL
* if there is no translation for the item or
* the other site is not linked to the current
* site.
* @return string When and why we stopped.
*/
function mlp_add_icons_to_menu( WP_Post $item, $translation = NULL ) {

if ( ! is_object( $translation ) )
return ‘no translation object’;

if ( ! class_implements( $translation, ‘Mlp_Translation_Interface’ ) )
return ‘invalid translation object’;

if ( FALSE !== strpos( $item->title, ‘get_icon_url() returns an instance of
* Mlp_Url_Interface, so we have to cast it to a string
* before we check it with empty().
* @see Mlp_Url_Interface
*/
$icon_url = (string) $translation->get_icon_url();

if ( empty ( $icon_url ) )
return ‘no icon available’;

$item->title = “ $item->title”;

return ‘success’;
}

add_action(
‘mlp_prepare_nav_menu_item_output’, // hook
‘mlp_add_icons_to_menu’, // callback
10, // priority
2 // number of accepted arguments
);
Download as plugin: MultilingualPress Addon: Nav Menu Icons. Activate it as a network plugin, and it will just work without further configuration.
You might have to adjust the image position in your stylesheet. Example:
.mlp-language-nav-item img {
vertical-align: middle;
}
Here is a screenshot of such a changed menu with our built-in flags. Again: please do not use these flags.
Always add an alt attribute, but leave its value empty if you still have text, so users with screen readers don’t have to waste their time with it. The item text is good enough.

Can I use MultilingualPress in a single-site WordPress installation?

Can I use MultilingualPress in a single-site WordPress installation?

Can I use MultilingualPress in a single-site WordPress installation?
You cannot use MultilingualPress in a single-site WordPress installation. For MultilingualPress you need to have a WordPress multisite installation.
Explanation in Detail: Why you can use MultilingualPress only in a WordPress multisite installation
To enable the use of MultilingualPress for a single site, we would have to interfere with the way WordPress saves content. However, there are plugins that accomplish exactly that. But we think this is a bad idea. After deactivating the plugin, you would lose access to your translated content.
The entire concept of MultilingualPress is based on using a corresponding site for each language (in the WordPress network, this means a multisite). Links between sites and between translated content (e.g. posts or terms) are saved in separate plugin tables. This makes it possible for the individual sites only to store the actual non-manipulated content, that is, without injected shortcode, as is (necessarily) the case for some single-site plugins.
If you currently have a single-site installation, you can change this to a multisite. You can find detailed instructions for this here.

Where can I buy MultilingualPress?

Where can I buy MultilingualPress?

Where can I buy MultilingualPress?
This tutorial is part of our MultilingualPress 2 documentation. In case you are using the newer version 3, please switch to MultilingualPress 3.
Our multilingualism plugin MultilingualPress convinced you and you need help setting it up? With MultilingualPress Premium you get first class support from our developers, Mo. till Fr. from 9 – 17 h (UTC + 1).
You can find our MultilingualPress shop at  multilingualpress.org. It’s worth to stop by, we also have a discount promotions from time to time.

By purchasing MultilingualPress Premium Support you acquire a license for any number of domains for one year. The license is only required for the support. The plugin is otherwise freely available on  wordpress.org.
Please, if you have any questions before buying, use our  contact form.

How can I extend my MultilingualPress license?

How can I extend my MultilingualPress license?

How can I extend my MultilingualPress license?
This tutorial is part of our MultilingualPress 2 documentation. In case you are using the newer version 3, please switch to MultilingualPress 3.
If you have purchased MultilingualPress Premium Support on multilingualpress.org, you have to do nothing for the renewal of your license. A subscription applies so that your license will be automatically renewed and you do not have to worry about every year.
Of course you can cancel your subscription at any time in your account towards the end of the billing period.
An overview of your order and subscriptions can be find in your MultilingualPress account on the My Account page, each with a link to the detailed view.
If you have questions before you buy the licenses and subscriptions, please contact us via the contact form. After purchasing you are allowed to contact support.

Requirements – What do I need to get MultilingualPress running smoothly?

Requirements – What do I need to get MultilingualPress running smoothly?

Requirements – What do I need to get MultilingualPress running smoothly?
This tutorial is part of our MultilingualPress 2 documentation. In case you are using the newer version 3, please switch to MultilingualPress 3.

To have MultilingualPress running smoothly on your website, it should meet the following requirements:

WordPress as multi-site, at least Version 4.2.
at least PHP 5.4.0. Newer versions are faster.

MultilingualPress cannot be used in a single-site WordPress installation. You need to have a multi-site installed. See our tutorial How to install multi-site.

How to link the sites of your WordPress multisite with each other

How to link the sites of your WordPress multisite with each other

How to link the sites of your WordPress multisite with each other
This tutorial is part of our MultilingualPress 2 documentation. In case you are using the newer version 3, please switch to MultilingualPress 3.
You need to have a site for each language within your multisite installation in order to translate your website. The next step is to link the sites of your WordPress multisite.  Afterwards you will be able to translate your posts and pages.
Follow these steps to link the sites of your WordPress multisite with each other

Go to Network Admin → Sites. There are all existing sites listed, with the page’s language and all linked sites.
Attention: You can only link sites when you assigned them to a language before.

Edit your desired site, then go to the tab MultilingualPress.
The section Relationships shows all existing sites which you assigned to a language. Choose the sites you want to link and save your settings.
Choose the sites you want to link and save your settings
Go to Network Admin → Sites. There, you will see the newly created links.

How to get a post from another site in the network

How to get a post from another site in the network

How to get a post from another site in the network
This tutorial is part of our MultilingualPress 2 documentation. In case you are using the newer version 3, please switch to MultilingualPress 3.
Let’s say we have a site id and a post id. The source doesn’t matter, it could be a database, user input, whatever. Now we want to get a WP_Post object from that site.
A first idea might look like this:
switch_to_blog( $site_id );
$post = get_post( $post_id );
restore_current_blog();
But this is not necessary, there is already a function in WordPress for that:
function get_blog_post( $blog_id, $post_id ) {
switch_to_blog( $blog_id );
$post = get_post( $post_id );
restore_current_blog();

return $post;
}
Nice, but there is an important, subtle bug in that function: it is using get_post( $post_id ) without a check on the post id. If $post_id is 0, NULL or “”, get_post() will use an existing global post object or id. But the current global post is from the source site, not from the target site! The id on site 1 references a completely different post than on site 2.
WordPress doesn’t care. So we have to do that check:
$post = NULL;

if ( ! empty ( $post_id ) )
$post = get_blog_post( $site_id, $post_id );