Skip to content

Instantly share code, notes, and snippets.

@kantoniak
Created April 18, 2017 09:42
Show Gist options
  • Save kantoniak/6bc0646ac7048c68bb9749808ec13072 to your computer and use it in GitHub Desktop.
Save kantoniak/6bc0646ac7048c68bb9749808ec13072 to your computer and use it in GitHub Desktop.
<?php
namespace kantoniak {
class AboutMe {
// ...
public function __construct() {
// ...
add_action('widgets_init', function() {
register_widget('\kantoniak\AboutMeWidget');
// To pass arguments to widget, see https://wordpress.org/ideas/topic/allow-ability-to-pass-parameters-when-registering-widgets
global $wp_widget_factory;
$widget = $wp_widget_factory->widgets['\kantoniak\AboutMeWidget'];
$widget->setPlugin($this);
});
}
// ...
}
class AboutMeWidget extends \WP_Widget {
private $plugin;
// ...
public function setPlugin($plugin) {
$this->plugin = $plugin;
}
public function widget($args, $instance) {
echo '<ul>';
foreach ($this->plugin->getSocialUrlsFromDatabase() as $slug => $url) {
$name = $this->plugin->getMediaSiteBySlug($slug)['name'];
$url_prefix = $this->plugin->getMediaSiteBySlug($slug)['url_prefix'];
echo '<li class="'. $slug .'"><a href="'. $url_prefix . $url .'" alt="'. $name .'">'. $name .'</a></li>';
}
echo '</ul>';
}
// ...
}
$aboutMe = new AboutMe();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment