WordPress.org

Ready to get started?Download WordPress

Forums

Social bookmarking buttons to add from the theme admin page (3 posts)

  1. Jeremy Herve
    Member
    Posted 4 years ago #

    Hi!

    I am developing a theme with social bookmarking buttons added to single.php like as follows:

    <a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php the_title(); ?>" title="Share on Facebook.">
     						<img src="<?php bloginfo('template_directory'); ?>/images/facebook.png" alt="Share on Facebook" id="sharethis-last" />
     						</a>

    I plan to add more, and I would like the user to be able to choose which buttons to display in his theme from the theme admin page, with checkboxes.

    How can I achieve such result?

    Thanks for your help!

  2. Jeremy Herve
    Member
    Posted 4 years ago #

    OK, that's where I stand after a bit more research:

    In single.php:

    <?
     						if ($mytheme_facebook_enable == "false") { ?>
     						<? } else { ?>
     						<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php the_title(); ?>" title="Share on Facebook.">
     						<img src="<?php bloginfo('template_directory'); ?>/images/facebook.png" alt="Share on Facebook" id="sharethis-last" />
     						</a>
     						<? } ?>
    
     						<?
     						if ($mytheme_twitter_enable == "false") { ?>
     						<? } else { ?>
     						<a href="http://twitter.com/home/?status=<?php the_title(); ?> : <?php the_permalink(); ?>" title="Tweet this!">
     						<img src="<?php bloginfo('template_directory'); ?>/images/twitter.png" alt="Tweet this!" />
     						</a>
     						<? } ?>
    
     						<?
     						if ($mytheme_delicious_enable == "false") { ?>
     						<? } else { ?>
     						<a href="http://del.icio.us/post?url=<?php the_permalink(); ?>&title=<?php the_title(); ?>" title="Bookmark on Delicious.">
     						<img src="<?php bloginfo('template_directory'); ?>/images/delicious.png" alt="Bookmark on Delicious" />
     						</a>
     						<? } ?>
    
     						<?
     						if ($mytheme_stumbleupon_enable == "false") { ?>
     						<? } else { ?>
     						<a href="http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>&title=<?php the_title(); ?>" title="StumbleUpon.">
     						<img src="<?php bloginfo('template_directory'); ?>/images/stumbleupon.png" alt="StumbleUpon" />
     						</a>
     						<? } ?>

    Then in functions.php I added these functions:

    <?php
    $themename = "My Theme";
    $shortname = "mytheme";
    
    $options = array (
    
    	array(	"name" => "Theme settings",
    			"type" => "title"),
    
    	array(	"type" => "open"),
    
    	array(  "name" => "Enable Facebook",
    			"desc" => "Check Box to enable (Share section)",
                "id" => $shortname."_facebook_enable",
                "type" => "checkbox",
                "std" => "false"),
    
            array(  "name" => "Enable Twitter",
    			"desc" => "Check Box to enable (Share section)",
                "id" => $shortname."_twitter_enable",
                "type" => "checkbox",
                "std" => "false"),
    
            array(  "name" => "Enable Delicious",
    			"desc" => "Check Box to enable (Share section)",
                "id" => $shortname."_delicious_enable",
                "type" => "checkbox",
                "std" => "false"),	
    
            array(  "name" => "Enable Stumbleupon",
    			"desc" => "Check Box to enable (Share section)",
                "id" => $shortname."_stumbleupon_enable",
                "type" => "checkbox",
                "std" => "false"),
    	array(	"type" => "close")
    
    );
    
    function mytheme_add_admin() {
    
        global $themename, $shortname, $options;
    
        if ( $_GET['page'] == basename(__FILE__) ) {
    
            if ( 'save' == $_REQUEST['action'] ) {
    
                    foreach ($options as $value) {
                        update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
    
                    foreach ($options as $value) {
                        if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }
    
                    header("Location: themes.php?page=functions.php&saved=true");
                    die;
    
            } else if( 'reset' == $_REQUEST['action'] ) {
    
                foreach ($options as $value) {
                    delete_option( $value['id'] ); }
    
                header("Location: themes.php?page=functions.php&reset=true");
                die;
    
            }
        }
    
        add_theme_page($themename." Options", "".$themename." Options", 'edit_themes', basename(__FILE__), 'mytheme_admin');
    
    }
    
    function mytheme_admin() {
    
        global $themename, $shortname, $options;
    
        if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings saved.</strong></p></div>';
        if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings reset.</strong></p></div>';
    
    ?>
    <div class="wrap">
    <h2><?php echo $themename; ?> settings</h2>
    
    <form method="post">
    
    <?php foreach ($options as $value) { 
    
    	switch ( $value['type'] ) {
    
    		case "open":
    		?>
            <table width="100%" border="0" style="padding:10px;">
    
    		<?php break;
    
    		case "close":
    		?>
    
            </table><br />
    
    		<?php break;
    
    		case "title":
    		?>
    		<table width="100%" border="0" style="padding:5px 10px;"><tr>
            	<td colspan="2"><h3><?php echo $value['name']; ?></h3></td>
            </tr>
    
    		<?php
            break;
    
    		case "checkbox":
    		?>
                <tr>
                <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
                    <td width="80%"><? if(get_settings($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = ""; } ?>
                            <input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
                            </td>
                </tr>
    
                <tr>
                    <td><small><?php echo $value['desc']; ?></small></td>
               </tr><tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td></tr>
    
            <?php 		break;
    
    }
    }
    ?>
    
    <!--</table>-->
    
    <p class="submit">
    <input name="save" type="submit" value="Save changes" />
    <input type="hidden" name="action" value="save" />
    </p>
    </form>
    <form method="post">
    <p class="submit">
    <input name="reset" type="submit" value="Reset" />
    <input type="hidden" name="action" value="reset" />
    </p>
    </form>
    
    <?php } // End custom theme admin page
    
    add_action('admin_menu', 'mytheme_add_admin'); ?>

    Now all my bookmarks are displayed, even if only one checkbox is checked.
    Any idea why?

  3. Jeremy Herve
    Member
    Posted 4 years ago #

    Hi!

    I am still stucked with that issue...

    Here is what I have done so far:

    In my functions.php
    http://pastebin.com/f37ea5d17

    In single.php
    http://pastebin.com/f3d9d923d

    But I must miss something: everything is displayed in single.php, checkboxes activated or not; and when I try to modify checkboxes in the admin options panel, I get an error message:
    "You do not have sufficient permissions to access this page."

    Do you have any idea how to work this out?

    Thanks for your help!

Topic Closed

This topic has been closed to new replies.

About this Topic