Custom Widget in WordPress


/ Published in: PHP
Save to your folder(s)

The code below is used to create a custom widget area in a WP template


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /* the following goes into the functions.php file */
  4. if (function_exists('register_sidebar')) {
  5.  
  6. register_sidebar(array(
  7. 'name' => 'My Custom Widget',
  8. 'id' => 'my_custom_widget',
  9. 'description' => 'Widget area for home page center left column',
  10. 'before_widget' => '<div id="widget-id" class="widget %2$s">',
  11. 'after_widget' => '</div>',
  12. 'before_title' => '<h2>',
  13. 'after_title' => '</h2>'
  14. ));
  15.  
  16. }
  17.  
  18. ?>
  19.  
  20. <?php
  21.  
  22. /* this goes into your template file where you want the widget to appear */
  23.  
  24. ?>
  25.  
  26. <?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('my_custom_widget')) : else : ?>
  27. <div class="widget">
  28. <h2>Widget Ready</h2>
  29. <p>This my_custom_widget is widget ready! Add one in the admin panel.</p>
  30. </div>
  31. <?php endif; ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.