OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I display single quotes instead of the HTML entity when adding onClick events to WordPress menu using nav_menu_link_attributes?

  • Thread starter Thread starter Croco2511
  • Start date Start date
C

Croco2511

Guest
I like to add a class and an onClick event to a single WordPress menu link.

My code is based on an example given by WordPress here:

Code:
    function add_specific_menu_atts( $atts, $item, $args ) {
    $menu_items = array(8071);
    if (in_array($item->ID, $menu_items)) {
        $atts['onClick'] = 'ml(\'show\', \'OUN6T5\', true)';
        $atts['class'] = 'ml-onclick-form button';
    }
    return ($atts);
    }
    add_filter( 'nav_menu_link_attributes', 'add_specific_menu_atts', 10, 3 );

It all works fine so far except for the onClick event (a pop-up showing a newsletter form powered by MailerLite).

This is the output I want to achieve:


<a href="#" onClick="ml('show', 'OUN6T5', true)" class="ml-onclick-form button">Join My Newsletter</a>


But the single quotes in the onClick event are replaced by HTML entites &#039;.

Instead of onClick="ml('show', 'OUN6T5', true)" I get

Code:
    onClick="ml(&#039;show&#039;, &#039;OUN6T5&#039;, true)"

Using html_entity_decode like this didn't help:

Code:
    $atts['onClick'] = html_entity_decode(ml(&#039;show&#039;, &#039;OUN6T5&#039;, true));

I think that I probably need to apply html_entity_decode to return ($atts);, but I don't know how to add html_entity_decode to an array. Maybe I'm also wrong, and another approach would be better. I hope someone can point me into the right direction.
<p>I like to add a class and an onClick event to a single WordPress menu link.</p>
<p>My code is based on an example given by WordPress <a href="https://developer.wordpress.org/reference/hooks/nav_menu_link_attributes/" rel="nofollow noreferrer">here</a>:</p>
<pre><code>
function add_specific_menu_atts( $atts, $item, $args ) {
$menu_items = array(8071);
if (in_array($item->ID, $menu_items)) {
$atts['onClick'] = 'ml(\'show\', \'OUN6T5\', true)';
$atts['class'] = 'ml-onclick-form button';
}
return ($atts);
}
add_filter( 'nav_menu_link_attributes', 'add_specific_menu_atts', 10, 3 );

</code></pre>
<p>It all works fine so far except for the onClick event (a pop-up showing a newsletter form powered by MailerLite).</p>
<p>This is the output I want to achieve:</p>
<pre>
<a href="#" onClick="ml('show', 'OUN6T5', true)" class="ml-onclick-form button">Join My Newsletter</a>
</pre>
<p>But the single quotes in the onClick event are replaced by HTML entites <code>&#039;</code>.</p>
<p>Instead of <code>onClick="ml('show', 'OUN6T5', true)"</code> I get</p>
<pre><code>
onClick="ml(&#039;show&#039;, &#039;OUN6T5&#039;, true)"

</code></pre>
<p>Using html_entity_decode like this didn't help:</p>
<pre><code>
$atts['onClick'] = html_entity_decode(ml(&#039;show&#039;, &#039;OUN6T5&#039;, true));

</code></pre>
<p>I think that I probably need to apply html_entity_decode to <code>return ($atts);</code>, but I don't know how to add html_entity_decode to an array.
Maybe I'm also wrong, and another approach would be better. I hope someone can point me into the right direction.</p>
Continue reading...
 

Latest posts

Top