Adding A Class="" In PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dillondoyle
Forum Newbie
Posts: 3
Joined: Wed Dec 06, 2006 4:32 pm

Adding A Class="" In PHP

Post by dillondoyle »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


So I am a total noob at php, but I am good with js and design. So I want to use a modified lightbox to allow users to beautifully digg things. The lightbox stuff I can do, but here is where I need help! So I am sure the answer is stupid simple, but I don't know php. So the below script makes a digg badge that checks itself for diggs and updates appropriately. It links to the digg this page. So all I need is in the [syntax="html"]<a href="http://digg.com/diggmewhateverttitels.html" class="variable">
So any help would be greatly appreciated, if you want to see the code in use (minus the lightbox) go to http://dillonmp3.com/dillonblog/[/syntax]

Code: Select all

<?php
/*
Plugin Name: DiggClick
Plugin URI: http://opensolaris.biz/archives/diggclick/
Description: displays a "digg it" button at the beginning of your posts, with a lot of configuration options.
Version: 1.3
Author: Michael Moreno
Author URI: http://opensolaris.biz/
License: Attribution-ShareAlike 2.5
*/
/*
DiggClick is a WordPress plugin that displays a "digg it" button at the beginning of your posts.
The plugin can be also be set to detect incoming links from digg.com to your wordpress post and
automatically display a link back, that will make it easier for people to digg your story. This
detection mechanism is based on the referring URI, which must come from digg.com. DiggClick is
easily configurable from within the admin panel that allows you to select wherever you want your
"digg it" button to be displayed. You can also turn on/off dugg auto detection and admin
notifications on new dugg post.

DiggClick is based on ideas from 
* Digg This (http://www.aviransplace.com/index.php/d ... ss-plugin/) by Aviran Mordo and 
* Digg Button (http://tech.cybernetnews.com/digg-button/) by Ryan Wanger
Also the submit image comes from Digg Button plugin by Ryan Wanger.

*/
/**
 * Settings array
 */
$data = array(
                'dc_home'		=> true,	/* homepage */
                'dc_single'		=> true,	/* posts */
		'dc_page'		=> false,	/* static pages */
		'dc_category'		=> false,	/* categories */
		'dc_date'		=> false,	/* archives */
		'dc_search'		=> false,	/* seach results */
		'dc_diggitnow'		=> false,	/* show the "Digg it" button in front 
							   of posts that haven't been dugg yet
							 */
		'dc_autodetect'		=> true,	/* autodetect dugg posts */
		'dc_notify'		=> false,	/* notify admin */
		'dc_position'		=> "above",	/* button position */
);
add_option('diggclick_options', $data, 'DiggClick Plugin Settings');
$dc_options = get_option('diggclick_options');

$dc_flash = 0;
/**
 * Handle settings change
 */
if (isset($_POST['dc_save'])) {
    $dc_options['dc_home'] = $_POST['dc_home'];
    $dc_options['dc_single'] = $_POST['dc_single'];
    $dc_options['dc_page'] = $_POST['dc_page'];
    $dc_options['dc_category'] = $_POST['dc_category'];
    $dc_options['dc_date'] = $_POST['dc_date'];
    $dc_options['dc_search'] = $_POST['dc_search'];
    $dc_options['dc_diggitnow'] = $_POST['dc_diggitnow'];
    $dc_options['dc_notify'] = $_POST['dc_notify'];
    $dc_options['dc_autodetect'] = $_POST['dc_autodetect'];
    $dc_options['dc_position'] = $_POST['dc_position'];
    update_option('diggclick_options',$dc_options);
    $dc_flash = 1; /* display */
}

/**
 * Add the options page 
 */
function diggclick_options_page() {
    if(function_exists('add_options_page')) {
	add_options_page('DiggClick', 'DiggClick', 8, basename(__FILE__), 'diggclick_options_panel');
    }
}

/**
 * Structure of the options page
 */
function diggclick_options_panel() {
    global $dc_options, $dc_flash, $_POST;
    
    if($dc_flash == 1) {
	?>
	<div id="message" class="updated fade"><p><?php _e('Your settings have been saved');?></p></div>
	<?php
    }
    ?>
    <div class="wrap">
    <legend><?php _e('DiggClick Options'); ?></legend>
    <form action="" method="post">
    <fieldset class="options">
    <h3><?php _e('Visibility'); ?></h3>
    <p><?php _e('Select locations on which the "Digg it" button should be shown:');?></p>    
    <p><label><select name="dc_position">
    <option value="above"<?php if($dc_options['dc_position']=="above"){ echo " selected"; }?>>above</option>
    <option value="under"<?php if($dc_options['dc_position']=="under"){ echo " selected"; }?>>under</option>
    </select>
    <?php _e('button position in relation to the content'); ?></label></p>
    <p><label><input name="dc_home" type="checkbox" value="1" <?php checked('1', $dc_options['dc_home']);?> />
    <?php _e('homepage'); ?></label></p>
    <p><label><input name="dc_single" type="checkbox" value="1" <?php checked('1', $dc_options['dc_single']);?> />
    <?php _e('posts'); ?></label></p>
    <p><label><input name="dc_page" type="checkbox" value="1" <?php checked('1', $dc_options['dc_page']);?> />
    <?php _e('static pages'); ?></label></p>
    <p><label><input name="dc_category" type="checkbox" value="1" <?php checked('1', $dc_options['dc_category']);?> />
    <?php _e('categories'); ?></label></p>
    <p><label><input name="dc_date" type="checkbox" value="1" <?php checked('1', $dc_options['dc_date']);?> />
    <?php _e('archives'); ?></label></p>
    <p><label><input name="dc_search" type="checkbox" value="1" <?php checked('1', $dc_options['dc_search']);?> />
    <?php _e('search results'); ?></label></p>
    
    <h3><?php _e('Misc'); ?></h3>
    <p><label><input name="dc_autodetect" type="checkbox" value="1" <?php checked('1', $dc_options['dc_autodetect']);?> />
    <?php _e('Autodetect posts dugg by users'); ?></label></p>
    <p><label><input name="dc_diggitnow" type="checkbox" value="1" <?php checked('1', $dc_options['dc_diggitnow']);?> />
    <?php _e('Show the "Digg it" button in front of posts that haven\'t been dugg yet'); ?></label></p>
    <p><label><input name="dc_notify" type="checkbox" value="1" <?php checked('1', $dc_options['dc_notify']);?> />
    <?php _e('Notify the admin when someone diggs a post'); ?></label></p>
    </fieldset>
    <p class="submit">
    <input type="hidden" name="dc_save" value="1" />
    <input type="submit" value="Save" />
    </p>
    </form>
    </div>
    <?php
}

/**
 * Here the story begins
 */
if(is_array($dc_options) && in_array(true, $dc_options)) {
    add_filter('the_content', 'digg_hook');
    add_filter('the_excerpt', 'digg_hook');

    /* Check options and attach the "Digg it" button in front of the content if appropriate */
    function digg_hook($content='') {
	global $dc_options;
	
	/* This piece of code comes from an example on codex,
	   don't exactly remember where, sorri */
        if((is_home() && $dc_options['dc_home']) ||
        (is_single() && $dc_options['dc_single']) ||
        (is_page() && $dc_options['dc_page']) ||
        (is_category() && $dc_options['dc_category']) ||
        (is_date() && $dc_options['dc_date']) ||
        (is_search() && $dc_options['dc_search'])
	|| 0) {
	    if($dc_options['dc_position']=="under") {
		$content .= diggclick_go();
	    } else {
		$content = diggclick_go().$content;
	    }
	}
    
        return $content;
    }
}

/**
 * Notify the admin
 */
function diggclick_notify($post_id, $referer) {
    global $wpdb;
    $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$post_id' LIMIT 1");
    
    $body = "Hi,\nyour post ".$post->post_title." got dugg.\n";
    $body .= "A backlink has been automatically added to the post.\n\n";
    $body .= "Post link: ".get_permalink($post->ID)."\n";
    $body .= "Back link: ".$referer."\n";
    
    $subject = "[".get_settings('blogname')."] ".$post->post_title." got dugg!";
    $admin = get_settings('admin_email');
    
    @wp_mail($admin, $subject, $body);
}

/**
 * The exact button generation code
 */
function diggclick_go() {
    global $post, $dc_options;
    $dc_link = get_post_meta($post->ID, 'DiggClick', $single = true);
    $referer = $_SERVER['HTTP_REFERER'];
    $referer_low = strtolower($referer);
    $referer_len = strlen($referer);
    $referer_end = substr($referer, -10);
    
    /* Is the post already dugg? */
    if($dc_link != '') {
	$dc_code = "<div class=\"diggclick\">\n<script type=\"text/javascript\">\n";
	$dc_code .= "//<![CDATA[\ndigg_url='".$dc_link."';\n//]]>\n";
	$dc_code .= "</script>\n<script type=\"text/javascript\" src=\"http://digg.com/api/diggthis.js\">\n</script>\n</div>";
	$dc_code .= "<!--thecontent-->\n";
	
	return $dc_code;
	
    } else {
	/* Is the visit referred by digg.com? */
	if(strpos($referer_low, "digg.com") !== false	
        && strpos($referer_low, "?") == false
	&& strpos($referer_low, "digg.com/users/") == false
	&& strpos($referer_end, "/upcoming") == false
        &&
        (strpos($referer_low, "digg.com")==0 || strpos($referer_low, "http://digg.com")==0 ||
        strpos($referer_low, "www.digg.com")==0 || strpos($referer_low, "http://www.digg.com")==0)
	&& $dc_options['dc_autodetect']==true) {
	
	    /* this removes /who, /blog, and /share from the path */ 
	     
	    if(strpos($referer_end, "/who")) {
		$referer = substr($referer, 0, $referer_len-4);
	    }
	    if(strpos($referer_end, "/blog")) {
		$referer = substr($referer, 0, $referer_len-5);
	    }
	    if(strpos($referer_end, "/share")) {
		$referer = substr($referer, 0, $referer_len-6);
	    }
	
	    /* Mark the post as dugg */
	    add_post_meta($post->ID, 'DiggClick', $referer);
	    
	    /* Notify the admin */
	    if($dc_options['dc_notify']==true)
		diggclick_notify($post->ID, $referer);
	
	    /* Display the button immediately so the first referred user would see it without need to reload */
    	    $dc_code = "<div class=\"diggclick\">\n<script type=\"text/javascript\">\n";
	    $dc_code .= "//<![CDATA[\ndigg_url='".$referer. class='lbOn'"';\n//]]>\n";
	    $dc_code .= "</script>\n<script type=\"text/javascript\" src=\"http://digg.com/api/diggthis.js\">\n</script>\n</div>";
	    $dc_code .= "<!--thecontent-->\n";
	    
	    return $dc_code;
	}
	
	/* Shall I display the "Digg it" button in front of posts that haven't been dugg yet? */
	else if($dc_options['dc_diggitnow']) {
	    $dc_code = "<div class=\"diggclick\">\n";
	    $dc_code .= "<a class="lbOn" href=\"http://digg.com/submit?phase=2&url=".get_permalink($post->ID)."\">\n";
	    $dc_code .= "<img src=\"".get_settings('siteurl')."/wp-content/plugins/diggclick/submit.gif\" alt=\"\" /></a></div>\n";
	    $dc_code .= "<!--thecontent-->\n";
	    
	    return $dc_code;
	}
    }
}

add_action('admin_menu', 'diggclick_options_page');

?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Could you clarify your question please?
dillondoyle
Forum Newbie
Posts: 3
Joined: Wed Dec 06, 2006 4:32 pm

Post by dillondoyle »

I will try. The above code creates a "digg badge" which displays the number of diggs. It is just an image, with a link. I wanted to add a class="whatever" the badge. So it will end up looking like:

Code: Select all

<a href="http://diggme.com" class="whatever"><img src="mydiggbadge.png"></a>
Like I said, I don't know PHP, but it should be hard. IDK
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

from looking at the code the only place that generates a "<a" tag is at the bottom. So you need to change the line that starts:

Code: Select all

$dc_code .= "<a class="lbOn" 

\\ to

            $dc_code .= "<a class=\"" . $some_style_var . "\"
(#10850)
dillondoyle
Forum Newbie
Posts: 3
Joined: Wed Dec 06, 2006 4:32 pm

Post by dillondoyle »

Sweet. That sounds good. It should work, THANK YOU VERY MUCH for your great support!
Post Reply