MasonWolf
asked on
Register a control for a Wordpress Widget
I've built a simple Wordpress plugin for users of ee to show off their ranks. However, though the plugin works, I also want to add a sidebar widget. I've built the widget, and my blog recognizes it as a widget, but for some reason it doesn't display the configuration options I included. A person can put it up as a widget, but there's no way to tell it which profile to screenscrape data for.
Anyway, you can see how it's supposed to work. The source code is below, but to download the complete zip folder with the graphics, just go to http://hiremasonwolf.com/ee_badge.zip. To use it, just unzip the folder into your wordpress plugins folder and activate it from your control panel. Then edit your sidebar.php file (assuming you have one) by adding this line:
<?php ee_badge(4228713); ?>
Type that line exactly and you'll show my stats. You can have it display yours instead by replacing the "4228713" with whatever comes after "M_" in the link to your own profile.
<?php
/*
Plugin Name: Experts-Exchange Badge
Plugin URI: http://hiremasonwolf.com
Description: Have your experts-exchange ranking displayed for all your blog visitors in a nice little graphic. Requires a width of 230 px.
Author: MasonWolf
Version: 0.9
Author URI: http://hiremasonwolf.com/
*/
function ee_badge($profile)
{
$page = file_get_contents("https://www.experts-exchange.com/M_$profile.html");
preg_match('@<span>([0-9]{ 1,3}(?:,[0 -9]{3})*)< /span>\s+E xpert Points@', $page, $points);
preg_match('@<span>([0-9]{ 2}/[0-9]{2 }/[0-9]{4} )</span>\s +Registrat ion Date@', $page, $date);
preg_match('@<span>([[:aln um:]]+)</s pan>\s+Log in Name@', $page, $name);
if((int)str_replace(",","" ,$points[1 ]) < 50000) $bg_img = "rookie1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 150000) $bg_img = "master1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 300000) $bg_img = "guru1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 500000) $bg_img = "wizard1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 1000000) $bg_img = "sage1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 10000000) $bg_img = "genius1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 25000000) $bg_img = "savant1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 50000000) $bg_img = "elite1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 100000000) $bg_img = "technocrat1.png";
else $bg_img = "legend1.png";
?>
<div style="background:url('<?= str_replace(ABSPATH, '', dirname(__FILE__)).'/'.$bg _img;?>') no-repeat top left #EEEEEE; width:230px; height:110px; cursor:pointer; border:ridge orange 3px" align="center" onClick="window.open('https://www.experts-exchange.com/M_4228713.html')">< strong style="font-size:9px; color:#999; font-family:Verdana, Arial, Helvetica, sans-serif">EXPERTS-EXCHAN GE.COM CERTIFIED</strong><div align="center" style="width:120px; margin-left:85px; margin-top:32px; margin-right:25px"><span style="font-family:'Times New Roman'; color: #ff6633;font-size: 14px; display: block; font-weight: bold; white-space: normal;"><?=$name[1];?></s pan><span style="font-family:'Times New Roman'; font-weight:bold; color:#666666; font-size:14px"><?=$points [1];?> POINTS</span></div>
<div style="margin-top:10px"><s pan style="display: block; font-family:'Times New Roman'; font-weight:bold; color:#999999; font-size:10px; margin-bottom:0">MEMBER SINCE <?=$date[1];?></span></div >
</div>
<?php
}
function widget_eebadge_init() {
if (!function_exists('registe r_sidebar_ widget')) {
return;
}
function widget_eebadge($args) {
extract($args);
echo $before_widget . $before_title . $after_title;
$profile = get_option('widget_eebadge ');
$page = file_get_contents("https://www.experts-exchange.com/M_$profile.html");
preg_match('@<span>([0-9]{ 1,3}(?:,[0 -9]{3})*)< /span>\s+E xpert Points@', $page, $points);
preg_match('@<span>([0-9]{ 2}/[0-9]{2 }/[0-9]{4} )</span>\s +Registrat ion Date@', $page, $date);
preg_match('@<span>([[:aln um:]]+)</s pan>\s+Log in Name@', $page, $name);
if((int)str_replace(",","" ,$points[1 ]) < 50000) $bg_img = "rookie1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 150000) $bg_img = "master1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 300000) $bg_img = "guru1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 500000) $bg_img = "wizard1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 1000000) $bg_img = "sage1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 10000000) $bg_img = "genius1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 25000000) $bg_img = "savant1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 50000000) $bg_img = "elite1.png";
elseif((int)str_replace(", ","",$poin ts[1]) < 100000000) $bg_img = "technocrat1.png";
else $bg_img = "legend1.png";
?>
<div style="background:url('<?= str_replace(ABSPATH, '', dirname(__FILE__)).'/'.$bg _img;?>?>' ) no-repeat top left #EEEEEE; width:230px; height:110px; cursor:pointer; border:ridge orange 3px" align="center" onClick="window.open('https://www.experts-exchange.com/M_4228713.html')">< strong style="font-size:9px; color:#999; font-family:Verdana, Arial, Helvetica, sans-serif">EXPERTS-EXCHAN GE.COM CERTIFIED</strong><div align="center" style="width:120px; margin-left:85px; margin-top:32px; margin-right:25px"><span style="font-family:'Times New Roman'; color: #ff6633;font-size: 14px; display: block; font-weight: bold; white-space: normal;"><?=$name[1];?></s pan><span style="font-family:'Times New Roman'; font-weight:bold; color:#666666; font-size:14px"><?=$points [1];?> POINTS</span></div>
<div style="margin-top:10px"><s pan style="display: block; font-family:'Times New Roman'; font-weight:bold; color:#999999; font-size:10px; margin-bottom:0">MEMBER SINCE <?=$date[1];?></span></div >
</div>
<?php echo $after_widget;
}
function widget_eebadge_options()
{
$profile = get_option('widget_eebadge ');
$profile = $profile[0];
if(isset($_POST['profile'] ))
{
if(ereg('[0-9]{4,7}',$_POS T['profile ']))
{
update_options('widget_eeb adge', array($_POST['profile']));
$profile = $_POST['profile'];
}
else
{
echo "<p style='font-weight:bold;'> <span style='color:red'>Invalid link:</span> The profile link provided should be between 4 and 7 digits and may only contain numbers. Please enter a valid code</p>";
}
}
echo "<p>Complete the link to your EE profile:<br>https://www.experts-exchange.com/M_<input type='text' name='profile' value='$profile' />.html</p>";
}
register_sidebar_widget('E xperts Exchange Badge','widget_eebadge');
register_widget_control('S elect Profile','widget_eebadge_o ptions', 250, 50);
}
add_action('plugins_loaded ', 'widget_eebadge_init');
?>
Anyway, you can see how it's supposed to work. The source code is below, but to download the complete zip folder with the graphics, just go to http://hiremasonwolf.com/ee_badge.zip. To use it, just unzip the folder into your wordpress plugins folder and activate it from your control panel. Then edit your sidebar.php file (assuming you have one) by adding this line:
<?php ee_badge(4228713); ?>
Type that line exactly and you'll show my stats. You can have it display yours instead by replacing the "4228713" with whatever comes after "M_" in the link to your own profile.
<?php
/*
Plugin Name: Experts-Exchange Badge
Plugin URI: http://hiremasonwolf.com
Description: Have your experts-exchange ranking displayed for all your blog visitors in a nice little graphic. Requires a width of 230 px.
Author: MasonWolf
Version: 0.9
Author URI: http://hiremasonwolf.com/
*/
function ee_badge($profile)
{
$page = file_get_contents("https://www.experts-exchange.com/M_$profile.html");
preg_match('@<span>([0-9]{
preg_match('@<span>([0-9]{
preg_match('@<span>([[:aln
if((int)str_replace(",",""
elseif((int)str_replace(",
elseif((int)str_replace(",
elseif((int)str_replace(",
elseif((int)str_replace(",
elseif((int)str_replace(",
elseif((int)str_replace(",
elseif((int)str_replace(",
elseif((int)str_replace(",
else $bg_img = "legend1.png";
?>
<div style="background:url('<?=
<div style="margin-top:10px"><s
</div>
<?php
}
function widget_eebadge_init() {
if (!function_exists('registe
return;
}
function widget_eebadge($args) {
extract($args);
echo $before_widget . $before_title . $after_title;
$profile = get_option('widget_eebadge
$page = file_get_contents("https://www.experts-exchange.com/M_$profile.html");
preg_match('@<span>([0-9]{
preg_match('@<span>([0-9]{
preg_match('@<span>([[:aln
if((int)str_replace(",",""
elseif((int)str_replace(",
elseif((int)str_replace(",
elseif((int)str_replace(",
elseif((int)str_replace(",
elseif((int)str_replace(",
elseif((int)str_replace(",
elseif((int)str_replace(",
elseif((int)str_replace(",
else $bg_img = "legend1.png";
?>
<div style="background:url('<?=
<div style="margin-top:10px"><s
</div>
<?php echo $after_widget;
}
function widget_eebadge_options()
{
$profile = get_option('widget_eebadge
$profile = $profile[0];
if(isset($_POST['profile']
{
if(ereg('[0-9]{4,7}',$_POS
{
update_options('widget_eeb
$profile = $_POST['profile'];
}
else
{
echo "<p style='font-weight:bold;'>
}
}
echo "<p>Complete the link to your EE profile:<br>https://www.experts-exchange.com/M_<input type='text' name='profile' value='$profile' />.html</p>";
}
register_sidebar_widget('E
register_widget_control('S
}
add_action('plugins_loaded
?>
The important bit on http://automattic.com/code/widgets/plugins/ is ...
"Dont leave out $before_widget, $after_widget, $before_title, or $after_title by accident. They are required for compatibility with various themes."
"Dont leave out $before_widget, $after_widget, $before_title, or $after_title by accident. They are required for compatibility with various themes."
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I don't know. I don't have/use Word Press. But the idea is good.
Well done on fixing it. Not sure if I helped.
Well done on fixing it. Not sure if I helped.
ASKER
Why did it close with RQuadling's final comment showing as the solution? It isn't! My own comment was the correct solution. I'm of course not asking for points from my own question, but if someone searches on this issue, it seems like it could be confusing to see the wrong comment labeled as the solution. The title was specific enough that it seems pretty likely someone struggling with this issue would find this question if they search the site.
I take no credit here.
ASKER
Wow! Thanks for the newsletter mention!
I went ahead and wrote a blog post about the plugin which includes instructions for using it. That link is here: http://hiremasonwolf.com/?p=31. Please also use that link if you want to provide feedback or request additional features. I'm already considering making the badge available in a range of sizes, as not all wordpress sidebars were created equal.
I went ahead and wrote a blog post about the plugin which includes instructions for using it. That link is here: http://hiremasonwolf.com/?p=31. Please also use that link if you want to provide feedback or request additional features. I'm already considering making the badge available in a range of sizes, as not all wordpress sidebars were created equal.
It COULD be possible to construct the base image dynamically and cache it based upon dimensions using PHP and the image functions.
1 - Does register_widget_control exist?
if ( !function_exists('register
return;
2 - Do you need to add $before_widget $before_title $title $after_title $after_widget to the options widget code?
I don't have word press, but I just read http://automattic.com/code/widgets/plugins/ and looked at the code in http://wordpress.org/extend/plugins/about-me-widget/installation/.
Richard