Add Links Beside Mobile Menu Button

|

On mobile devices the Divi theme collapses the header menu items into a dropdown menu, accessible via the mobile menu button. Sometimes though, it's useful to be able to add a link or two next to the mobile menu button, separate from the mobile menu, as shown:

This effect can be achieved using the following PHP code:

if (!class_exists('DBMobileNavLink')) {
	
	class DBMobileNavLink {
		
		private $text;
		private $url;
		private $allow_html;
		
		static function createWithHtmlAllowed($text, $url) {
			return new self($text, $url, truel);
		}
		
		static function create($text, $url) {
			return new self($text, $url, false);
		}
		
		private function __construct($text, $url, $allow_html=false) {
			$this->text = $text;
			$this->url = $url;
			$this->allow_html = $allow_html;
		}
		
		function registerHooks() {	
			add_action('et_header_top', array($this, 'outputLinkHtml'), 11);
			add_action('wp_head', array($this, 'outputCss'));
		}
		
		function outputLinkHtml(){
			if ($this->visible()) {
				printf(
					'<a href="%1$s" class="db_mobile_nav_link">%2$s</a>',
					esc_attr($this->url),
					$this->allow_html?$this->text:esc_html($this->text)
				);
			}
		}
		
		private function visible() {
			if (is_customize_preview()) return true;
			if (!in_array($this->headerStyle(), array('slide', 'fullscreen'))) return true;
			return false;
		}
		
		private function headerStyle() {
			return function_exists('et_get_option')?et_get_option('header_style', 'left'):'left';
		}

		function outputCss() { ?>
			<style>
			@media only screen and (max-width: 980px) {
				.db_mobile_nav_link {
					position: relative !important;
					display: inline-block;
					float: right;
					margin-right: 1em;
					margin-top: 0.2em;
				}
				.et_pb_menu_hidden .db_mobile_nav_link {
					opacity: 0;
					-webkit-animation: fadeOutBottom 1s cubic-bezier(.77,0,.175,1) 1;
					animation: fadeOutBottom 1s cubic-bezier(.77,0,.175,1) 1
				}
				.et_pb_menu_visible .db_mobile_nav_link {
					z-index: 99;
					opacity: 1;
					-webkit-animation: fadeInBottom 1s cubic-bezier(.77,0,.175,1) 1;
					animation: fadeInBottom 1s cubic-bezier(.77,0,.175,1) 1
				}
			}
			@media only screen and (min-width: 981px) {
				.db_mobile_nav_link {
					display: none;
				}
			}
			</style>
		<?php
		}
	}
}

DBMobileNavLink::create('Link 2', 'https://divibooster.com')->registerHooks();
DBMobileNavLink::create('Link 1', 'https://divibooster.com')->registerHooks();
DBMobileNavLink::createWithHtmlAllowed('<i class="fa fa-sign-out"></i>', 'https://divibooster.com')->registerHooks();

The last three lines are the ones that actually add the links to the page. The first two are plain text links, while the third adds HTML inside the link tag (which in this example will display a font awesome "logout" icon – assuming that font awesome is loaded on the site). They can be repeated to add more links. Note that the links will show in the reverse order from which they are added.

This post may contain referral links which may earn a commission for this site

Divi Booster

Hundreds of new features for Divi
in one easy-to-use plugin

4 Comments

  1. Is there a way to add icons rather than text? I've managed to use hex codes, e.g., 📞 rather than Link 2, as in your example.
    However, I can't add a font-awesome icon such as \1f4de or .

    Is there a way to do this by changing the code slightly?

    Reply
    • Hey Matt, I've just updated the code to allow it to be used to add HTML within the links.

      The last line of the code shows this in use to add a font awesome icon. Note that you need to use the HTML form of the icon, which I think for \1f4de is:

      <i class="fa-solid fa-truck-ramp-box"></i>

      So you'd add the icon like so:

      DBMobileNavLink::createWithHtmlAllowed('<i class="fa-solid fa-truck-ramp-box"></i>', 'https://divibooster.com')->registerHooks();

      (Replacing https://divibooster.com with your link URL).

      And of course, you also need to ensure that font awesome itself is loaded on the site. The code above doesn't do that.

      I hope that helps, but give me a shout if not.

      Reply
  2. One question, how to make the links "Open to New Tab"?

    Also just for additional note, this is done added to Function.php file right?

    Reply
    • Hey Vincent, you'd change this line:

      '<a href="%1$s" class="db_mobile_nav_link">%2$s</a>',
      

      To:

      '<a href="%1$s" target="_blank" class="db_mobile_nav_link">%2$s</a>',
      

      And yes, you can it the functions.php file of a child theme. You can technically add it into Divi's own functions.php file, but that gets overwritten on updates, so isn't recommended. You can also add it using a plugin such as this one:

      https://wordpress.org/plugins/code-snippets/

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

Your email address will not be published. Required fields are marked *.