isbe
isbe

Reputation: 253

How to call shortcode in onclick href (in Html widget)?

I need to call shortcode [profile-url] when click Submit button.
I tried following codes, but they all don't call the url.

onclick="location.href='<?php echo do_shortcode('[profile-url]'); ?>';"
onclick="javascript:location.href='<?php echo do_shortcode('[profile-url]'); ?>';"
onclick="location.href=<?php echo  do_shortcode('#profile-url')?>;"
onclick="location.href='[profile-url]';"
onclick="location.href=<?php profile-url(); ?>;"

I tested this shortcode [profile-url] using Nav menu plugin and it works well.
It just doesn't work with my Menu code below.
Would you please let me know how to solve this problem?

Menu code in Elementor using Html widget:

<li>
    <a id="fourth-menu" class="menu">
      <h2 class="menu-title menu-title_4th">Myhome</h2>
      <ul id="menu-dropdown-four" class="menu-dropdown">
        <li onclick="location.href=????????????;">Submit</li>
        <li>Progress</li>
      </ul>
    </a>
</li>

[profile-url] in Function.php (works well):
    add_shortcode( 'profile-url' , 'infosubmission_url' );
    function infosubmission_url(){
      $user = wp_get_current_user();
     
      if( !$user->ID ){
        //user not logged in, redirect to login package
        return ( 'www.myweb.com/project-login-required/' );
      }
     
      //User is logged in, return dynamic URL
      $username = $user->user_login;
      return ( 'www.myweb.com/mypage-infosubmit/' );
    }

//it allows you to use short code in menue link
add_filter( 'wp_nav_menu_items', 'do_shortcode'); 
add_filter( 'widget_text', 'shortcode_unautop');
add_filter( 'widget_text', 'do_shortcode');
add_filter( 'widget_html', 'do_shortcode');

Thank you.

Upvotes: 0

Views: 1109

Answers (1)

Tony Tin Nguyen
Tony Tin Nguyen

Reputation: 355

Please try this for testing

onclick="javascript:location.href=http://www.uol.com.br/"

and if it's working, please adjust the PHP code.

Update:

    <li>
        <a id="fourth-menu" class="menu">
           <h2 class="menu-title menu-title_4th">Myhome</h2>
           <ul id="menu-dropdown-four" class="menu-dropdown">
           <?php
            $output = '<li onclick="' . do_shortcode('profile-url') .'">Submit</li>';
            echo $ouput;
            ?>
        <li>Progress</li>
      </ul>
    </a>
</li>

Upvotes: 1

Related Questions