Posted By

erikportin on 05/24/11


Tagged


Versions (?)

Make shortcode for dynamics links for widgets in wordpress


 / Published in: PHP
 

  1. <?php
  2. /* Short code [a href="pageId" class"clasName"]link[/a] */
  3.  
  4.  
  5. /*If you check “add paragraphs automatically” on the widget, WordPress will apply the autop filter — the one that turns your line breaks into paragraph and break tags. If a shortcode is on its own line, it would normally get wrapped in a paragraph tag. The first line prevents that from happening.
  6.  
  7. thanks to http://sillybean.net/2010/02/using-shortcodes-everywhere/
  8. */
  9.  
  10. add_filter( 'widget_text', 'shortcode_unautop');
  11.  
  12. //Add shortcode functionality to widgets
  13. add_filter('widget_text', 'do_shortcode');
  14.  
  15.  
  16. function a_func($atts, $content) {
  17. extract(shortcode_atts(array(
  18. 'href' => ' ',
  19. 'class' => ' ',
  20. ), $atts));
  21.  
  22. $theLink = '<a class="'. $class .'" href="'. get_permalink($href) .'">'. $content .'</a>';
  23.  
  24. return "{$theLink}";
  25. }
  26.  
  27. add_shortcode('a', 'a_func');
  28.  
  29. ?>

Report this snippet  

You need to login to post a comment.