The Right Way to Include a Plugin in a WordPress Theme : alexking.org

The Right Way to Include a Plugin in a WordPress Theme : alexking.org.

Here’s the clean/correct way to add a plugin to your theme.

Advertisements

WordPress Sitemap Generator Plugin

This plugin will create a sitemap for your WordPress site (http://example.com/sitemap.xml). This was originally created for a multi-site setup, but it works fine on a single site install as well. There is no ‘User Interface’ to speak of with this plugin, just drop in place.

You may also download this code via it’s github repository:

First, create a file named sitemap-generator.php in your mu-plugins directory, (or your plugins directory if your using a single install WordPress setup) and past the below into it.

<?php
/*
Plugin Name: Sitemap Generator
Plugin URI: http://technology.mattrude.com/2011/10/07/wordpress-sitemap-generator-plugin/
Description: Automatic generate standard XML sitemap (http://example.com/sitemap.xml) that supports the protocol including Google, Yahoo, MSN, Ask.com, and others. No files stored on your disk, the sitemap.xml file is generate as needed, like your feeds.
Version: 1.0
Author: Matt Rude
Author URI: http://mattrude.com/
*/

function sitemap_flush_rules() {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
}

add_action('init', 'sitemap_flush_rules');

function xml_feed_rewrite($wp_rewrite) {
        $feed_rules = array(
                '.*sitemap.xml$' => 'index.php?feed=sitemap'
        );

        $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}

add_filter('generate_rewrite_rules', 'xml_feed_rewrite');

function do_feed_sitemap() {
        $template_dir = dirname(__FILE__) . '/templates';
        load_template( $template_dir . '/feed-sitemap.php' );
}

add_action('do_feed_sitemap', 'do_feed_sitemap', 10, 1);

?>

Next, create a new directory named templates and past the below in a file named feed-sitemap.php in it.

<?php
/**
 * XML Sitemap Feed Template for displaying XML Sitemap Posts feed.
 */

//header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);

echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

        <!-- Main Site -->

        <url>
                <loc><?php bloginfo_rss('url') ?></loc>
                <lastmod><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_lastpostmodified('GMT'), false); ?></lastmod>
                <changefreq>daily</changefreq>
                <priority>0.8</priority>
        </url>

        <!-- Site Pages -->

<?php
        $args = array(
                'post_type' => 'page',
                'numberposts' => 100,
                'status' => 'publish',
                'orderby' => 'date',
                'order' => 'DESC'
        );
        $post_ids = get_posts($args);

        if ($post_ids) {
                foreach ($post_ids as $post) { ?>
        <url>
                <loc><?php the_permalink_rss() ?></loc>
                <lastmod><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_post_time('Y-m-d H:i:s', true), false); ?></lastmod>
                <changefreq>monthly</changefreq>
                <priority>0.7</priority>
        </url>
<?php
 } }
?>

        <!-- Site Posts -->

<?php
        $args = array(
                'post_type' => 'post',
                'numberposts' => 100,
                'post_status' => 'publish',
                'orderby' => 'date',
                'order' => 'DESC'
        );
        $post_ids = get_posts($args);

        if ($post_ids) {
                foreach ($post_ids as $post) { ?>
        <url>
                <loc><?php the_permalink_rss() ?></loc>
                <lastmod><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_post_time('Y-m-d H:i:s', true), false); ?></lastmod>
                <changefreq>monthly</changefreq>
                <priority>0.5</priority>
<?php
        $args2 = array(
                'post_type' => 'attachment',
                'numberposts' => 200,
                'post_parent' => $post->ID,
                'post_mime_type' => 'image',
                'orderby' => 'date',
                'order' => 'DESC'
        );
        $images = get_posts($args2);
        if ($images) {
                foreach ($images as $post) { ?>
                <image:image>
                        <image:loc><?php echo wp_get_attachment_url(); ?></image:loc>
<?php if ( !empty($post->post_excerpt) ) echo '                 <image:caption>' . esc_html($post->post_excerpt, 1) . '</image:caption>
'; ?>
                        <image:title><?php echo esc_html($post->post_title, 1) ?></image:title>
                </image:image>
<?php } } ?>
        </url>
<?php
                }
        }
?>
</urlset>

Typekit Font’s WordPress Plugin

This small plugin provides Typekit fonts to a website by adding the needed java code to the pages

Place this file in your plugin directory, activate it, then check out Appearance -> Fonts from your WordPress admin.

<?php
/*
Plugin Name: Typekit Fonts
Plugin URI: http://github.com/mattrude/mdr-network
Description: Provides Typekit fonts to a website by adding the needed java code to the pages. See Appearance -> Fonts
Version: 1.0
Author: Matt Rude
Author URI: http://mattrude.com
*/

define('TYPEKITFONTS_TEXTDOMAIN', 'mdr-network');

if (function_exists('load_plugin_textdomain')) {
        load_plugin_textdomain(TYPEKITFONTS_TEXTDOMAIN, false, dirname(__FILE__).'/languages' );
}

function add_fonts_page() {
  global $typekit_font_hook;
  $typekit_font_hook = add_submenu_page( 'themes.php', 'Typekit Fonts Options', __('Fonts', TYPEKITFONTS_TEXTDOMAIN), 'administrator', 'typekitfonts', 'fonts_page' );
}

function register_typekit() {
  add_option('typekit_id');
}

function typekit_font_help($contextual_help, $screen_id, $screen) {
	global $typekit_font_hook;
	if ($screen_id == $typekit_font_hook) {
		$contextual_help = '<p>' . __('Typekit&rsquo;s fonts isn&rsquo;t the easist thing to setup. It will require that you know something about the way your page is displayed to the public. See the following resources for a overview of what your trying to do.', TYPEKITFONTS_TEXTDOMAIN) . '</p><ul>
		<li><a href="http://typekit.zendesk.com/entries/130054-using-typekit-the-basics" target="_blank">' . __('Using Typekit - The Basics', TYPEKITFONTS_TEXTDOMAIN) . '</a></li>
		<li><a href="http://typekit.zendesk.com/entries/121731-using-typekit-with-wordpress" target="_blank">' . __('Using Typekit with WordPress', TYPEKITFONTS_TEXTDOMAIN) . '</a></li>
		<li><a href="http://typekit.zendesk.com/entries/130283-finding-and-using-selectors" target="_blank">' . __('Finding and Using Selectors', TYPEKITFONTS_TEXTDOMAIN) . '</a></li>
		</ul>'; }
	return $contextual_help;
}

add_action( 'contextual_help', 'typekit_font_help', 10, 3 );
add_action( 'admin_menu', 'add_fonts_page' );
add_action( 'admin_init', 'register_typekit' );

function fonts_page() {
  settings_fields( TYPEKITFONTS_TEXTDOMAIN );

  // Update Settings
  if ( isset($_POST['submit']) ) {
    if (!current_user_can('manage_options')) die(__('You cannot edit this screen.', TYPEKITFONTS_TEXTDOMAIN));
    $typekit_id = $_POST['typekit_id'];
    update_option("typekit_id", $typekit_id);
  }

?>
  <style type="text/css">
    div.typekit_box_one {border: 1px solid #CCC;clear: left;float: left;height: 220px;margin-right: 25px;padding: 0px 20px;width: 260px;}
    div.typekit_box_two {border: 1px solid #CCC;float: left;height: 220px;margin-right: 25px;padding: 0px 20px;width: 260px;}
  </style>
  <div class="wrap">
    <div id="icon-themes" class="icon32"><br></div>
    <h2><?php _e('Typekit Fonts', TYPEKITFONTS_TEXTDOMAIN) ?></h2>
    <p><a href="http://typekit.com/"><strong><?php _e('Typekit', TYPEKITFONTS_TEXTDOMAIN); ?></strong></a> <?php _e('offer a service that allows you to select from a range of hundreds of high quality fonts for your WordPress website. The fonts are applied using the font-face standard, so they are standards compliant, fully licensed and accessible. The fonts are served from a global network on redundant servers, so they shouldn\'t slow down your site. It\'s a subscription-based service, if you don\'t already have an account, you will need to register for one.', TYPEKITFONTS_TEXTDOMAIN); ?></p>

    <div class="typekit_box_one">
      <form action="themes.php?page=typekitfonts" method="post">
		<h3><?php _e('Your Typekit ID', TYPEKITFONTS_TEXTDOMAIN); ?></h3>
		<p><?php _e('You can find your Typekit ID on typekit.com under Kit Editor -> Embed Code.', TYPEKITFONTS_TEXTDOMAIN); ?></p>
		<?php $typekit_id = get_option( 'typekit_id' );?>
		<p><input type="text" size="25" name="typekit_id" value="<?php echo $typekit_id; ?>" /></p>
		<p class="submit"><input type="submit" name="submit" value="<?php _e('Update ID', TYPEKITFONTS_TEXTDOMAIN); ?>" /></p>
      </form>
    </div>

    <div class="typekit_box_two">
	<form action="http://typekit.com/ref/wordpress" method="post">
		<h3><?php _e('Sign up for Typekit', TYPEKITFONTS_TEXTDOMAIN); ?></h3>
		<p><?php _e('If you don’t have a Typekit ID yet you can sign up on their site for free and enable it on your site with just a few clicks.', TYPEKITFONTS_TEXTDOMAIN); ?></p>
		<p class="submit">
			<input type="submit" name="submit" value="<?php _e('Sign up in seconds', TYPEKITFONTS_TEXTDOMAIN); ?>" />
		</p>
	</form>
  </div> <?php
}

function typekit_header() {
  $typekit_id = get_option( 'typekit_id' );
  if ( $typekit_id != NULL ) {
    echo '    <!-- Start Typekit Font download script -->
    <script type="text/javascript" src="http://use.typekit.com/'. $typekit_id .'.js"></script>
    <script type="text/javascript">try{Typekit.load();}catch(e){}</script>
    <!-- End Typekit Font download script -->';
  }
}

add_action('wp_head','typekit_header');
?>

WordPress Sitemap Generator

This plugin will create a sitemap for your WordPress site (http://example.com/sitemap.xml). This was setup for a multisite install, but should work fine on a single install.

First, create a file named sitemap-generator.php in your mu-plugins directory, (or your plugins directory if your using a single install WordPress setup) and past the below into it.

After create a new directory named templates and past the below in a file feed-sitemap.php in it.

WordPress Favicon Plugin

One more quick plugin setup for multi site builds of WordPress. Just drop this in your mu-plugins folder. This plugin will first look for a local favicon, if there is none, it will go to Gravatar to see if the Admin_Email as a gravator, if it doesn’t, it will use the default from the main install.  If a user must have a special favicon you may drop it in there files directory, for example blogs.dir/5/files.