2

I'm trying to use add_action to add HTML output to a plugin's front end area, Below is the snippet from plugin

<ul class="dashboard-links">
    <li><a href="<?php echo $dashboard_links['commissions'] ?>"><?php _e( 'Commissions', 'yith-woocommerce-affiliates' ) ?></a></li>
    <li><a href="<?php echo $dashboard_links['clicks'] ?>"><?php _e( 'Clicks', 'yith-woocommerce-affiliates' ) ?></a></li>
    <li><a href="<?php echo $dashboard_links['payments'] ?>"><?php _e( 'Payments', 'yith-woocommerce-affiliates' ) ?></a></li>
    <li><a href="<?php echo $dashboard_links['generate_link'] ?>"><?php _e( 'Generate link', 'yith-woocommerce-affiliates' ) ?></a></li>
    <li><a href="<?php echo $dashboard_links['settings'] ?>"><?php _e( 'Settings', 'yith-woocommerce-affiliates' ) ?></a></li>
    <?php do_action( 'yith_wcaf_after_dashboard_links', $dashboard_links ) ?>
</ul>

I'm trying to extend it by adding the snippet written below in functions.php

function add_link_to_aff() {
    $html_output =  "<li><a href='#'><?php _e( 'Test', 'yith-woocommerce-affiliates' ) ?></a></li>";
    return $html_output;  
}
add_action( 'yith_wcaf_after_dashboard_links', 'add_link_to_aff', 10 );

This is the snippet which I'm g, I also tried passing $dashboard_links to the function but it didn't worked.

  • Actions don't use a return value. Why aren't you printing out the HTML immediately? – fuxia Apr 14 '18 at 8:17
  • Do you mean adding in in the plugin's file or doing echo inside function? – Akshat Apr 14 '18 at 8:31
  • 1
    Referring to your add_link_to_aff() function, with the do_action() function, you need to echo $html_output; (and not return it). If it was apply_filters() as in apply_filters( 'yith_wcaf_after_dashboard_links', $dashboard_links ), then you'd do something like return $dashboard_links . $html_output; (and not echo the $html_output). So in your case, do_action = do something after the default dashboard links are printed and not filters the dashboard links. – Sally CJ Apr 14 '18 at 9:00
1

You need to echo your data. Try this instead -

function add_link_to_aff() {
    $html_output =  "<li><a href='#'>" .  __( 'Test', 'yith-woocommerce-affiliates' ) .  "</a></li>";
    echo $html_output;  
}
add_action( 'yith_wcaf_after_dashboard_links', 'add_link_to_aff', 10 );
  • I already tried but this didn't worked. – Akshat Apr 14 '18 at 9:59
  • Just updated my answer. There was a syntax error. Try again. – mukto90 Apr 14 '18 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.