• Skip to primary navigation
  • Skip to main content
  • Skip to footer
  • Store
  • Support
  • Theme Documentation
  • My Account
  • Cart

9seeds

Building Custom WordPress Solutions | Plugin Development

 
  • Custom Development
  • Themes
  • Plugins
  • About
  • Contact
  • Blog

News

Including templates inside a plugin

Posted on December 17, 2012

This past week I was working on a project for a client who needed a custom post type to manage events on his site. Normally for a project like this I would build the CPTs and push a couple templates in to the active theme folder. But in this instance, the client was working on a new theme that would be going live in a few weeks. Rather than making the client jump through hoops and move template files around, I wanted to provide the easiest solution possible. That meant, including all the templates as part of the plugin. Thanks to a very handy filter in WordPress, this wasn’t a problem. Here’s how it ended up.

Here’s the makeup of the plugin:

Plugin Layout

The plugin itself is very standard. One thing I like to do is define the custom post types as separate files to keep the function file clean. I then include the CPTs and the custom metabox library like so:

/** Add custom CPTs and Metaboxes */
require_once( 'cpt-events.php' );
require_once( 'cpt-locations.php' );
require_once( 'metabox/init.php' );

I store the stylesheet in a separate folder for cleanliness as well and encode it like this:

/** Enqueue CSS */
add_action( 'wp_enqueue_scripts', 'prefix_add_my_stylesheet' );

function prefix_add_my_stylesheet() {
	wp_register_style( 'cpt-style', plugins_url( 'css/style.css', __FILE__) );
	wp_enqueue_style( 'cpt-style' );
}

And now the real meat of it. You can see the single-events.php and archive-events.php that make up the two template files I want to use on the front end. What I’m going to do is create a function that tells WordPress to use my templates for the archive and single pages for the event CPT. That code looks like this:

// force use of templates from plugin folder
function cpte_force_template( $template )
{	
    if( is_archive( 'events' ) ) {
        $template = WP_PLUGIN_DIR .'/'. plugin_basename( dirname(__FILE__) ) .'/archive-events.php';
	}
	
	if( is_singular( 'events' ) ) {
        $template = WP_PLUGIN_DIR .'/'. plugin_basename( dirname(__FILE__) ) .'/single-events.php';
	}
	
    return $template;
}
add_filter( 'template_include', 'cpte_force_template' );

As you can see, I’ve set up a filter for template_include. I pass in the $template variable and check to see if the page is either an archive or singular event. If so, I override the default template location with the full path and file name of the file inside my plugin folder.

The end result is a fully encapsulated plugin that takes care of the functionality, templates and stylesheet formatting the client requested all in one easy to manage package. Simply install the plugin and activate it and you’re ready to go!

Continue Reading

john

    More by john

    New plugin: Authorize.net SIM Gateway for WP e-Commerce

    Posted on December 10, 2012

    A while back we released the WP Affiliate Manager plugin which integrates directly in to the WP e-Commerce plugin to allow you to easily track and pay affiliates for driving traffic to your site. We have since been keeping an eye on the WP e-Commerce community and when we saw the opportunity to help with another plugin, we jumped at the chance.

    Today we released a new payment gateway for WP e-Commerce: Authorize.net Server Integration Method, or SIM for short. From the Authorize.net website:

    SIM provides a customizable, secure hosted payment form to make integration easy for Web merchants that do not have an SSL certificate.

    WP e-Commerce has a wide range of payment gateway options available, and we’re happy to help expand their reach by giving users one more option to choose from.

    If you are interested in reading more about how Authorize.net’s SIM works, you can check out the developer center at Authorize.net.

    The plugin is available now and is compatible with the new 3.8.9.x software.

    Continue Reading

    john

      More by john

      Stealing math from Wikipedia

      Posted on November 26, 2012

      WordPress + Jetpack + LaTeX = Awesome

      On my personal WordPress website I wrote about an Arduino device I built to help brew beer.  Being my first foray into electronics, I wanted to document my experiences.  One of my difficulties centered around turning electrical values (voltage, resistance) into real-world values (temperature) using math.

      As part of my documentation, I wanted to re-create some equations that I had found on a Wikipedia page about thermistors.  While in the process of grabbing their images and putting them into the media library, I remembered something…

      One time while on the main Jetpack screen in the WordPress dashboard, I noticed a tile that advertised “Beautiful Math.”  So I looked into it.  Jetpack has a LaTeX module that can accomplish this.  LaTeX is a rather old-school markup/typesetting language that has been very popular in academia, especially in regards to math.

      So Jetpack and LaTeX can do math, but I didn’t really want to learn a new markup language, I just wanted to quickly copy & paste the equations into my post.

      Good Authors Borrow, Great Authors Steal

      So then I wondered how Wikipedia is storing the images or if they’re doing something similar.  Guess what?!?  They use LaTeX too!  Putting the same equation into WordPress is as easy as clicking “Edit” on the Wikipedia page and copying the text between <math> and </math> and pasting it in-between [latex] and [/latex] in WordPress.

      Native vs. Shortcode formatting Tricks

      Jetpack supports using the $latex $ native format instead of the shortcode, but there are a few noted (and undocumented) differences between using the two methods. For instance, if you want to increase the size of your rendered LaTeX text, you can use the s (size) parameter in your LaTeX equation as such:

      $latex \LaTeX&s=4$

      Where size in this example is “4.”  However if you try to do this using the shortcode as such:

      [latex] \LaTeX&s=4[/latex]

      it will render as: [latex] \LaTeX&s=4[/latex] – Not exactly what you’d expect 🙁 The trick is that the following LaTeX parameters need to be fed in as shortcode parameters:

      • s (size)
      • bg (background color)
      • fg (foreground / “text” color)

      So if you want to have “LaTeX” rendered as:

      $latex \LaTeX&bg=ffcccc&fg=cc00ff&s=4$

      You can either do:

      $latex \LaTeX&bg=ffcccc&fg=cc00ff&s=4$

      or  move the size & color parameters into the shortcode tag like this:

      [latex bg=ffcccc fg=cc00ff s=4]\LaTeX[/latex]

      Continue Reading

      justin

        More by justin

        Some handy tools from the Community Summit

        Posted on November 11, 2012

        A few weeks back I had the great privilege of attending the WordPress Community Summit. Rather than renting a hotel room by myself, instead I rented a house with 4 other Community Summit attendees; Ryan Imel, Brad Williams, Brandon Dove and Dre Armeda. Not exactly bad company to keep. We figured staying in the same house would help keep costs down and would also be a lot of fun. What we hadn’t really planned on was some of the great knowledge sharing that took place.

        One afternoon while we were hanging out in the house, we started talking about some of the handy tools we all use. As the conversation went on, Ryan had the quick sense to open up a Google doc and start taking notes. Below is the list of handy tools we came up with.

        Keep in mind, these lists are definitely not all inclusive. We know there are many other text editors and cool apps out there. But these are the ones that are top of mind that day.

        Text editors

        • NetBeans
        • Sublime Text 2
        • Coda

        Mac Apps

        • Alfred – Easily launch applications, sites and perform several other tricks
        • CoBook – Amazingly easy and simple CRM tool
        • Spirited Away – Hides apps after a set amount of idle time
        • Hazel – Simple automation tool for dealing with files
        • Total Terminal – Formally called Visor, a system wide hotkey for terminal
        • Droplr – Easy screenshot/text file uploader for sharing
        • TypeIt4Me – Text expander

        Custom Searches for Alfred

        • http://wordpress.org/extend/plugins/search.php?q={query}
          Search the WordPress plugin repo
        • http://wordpress.org/search/_Reference+{query}
          Search wordpress.org for functions, actions, filters, etc
        • http://demo.studiopress.com/{query}
          Quickly load any StudioPress theme demo site

        If you’ve got another tool to add to the list, drop it in the comments.

        Continue Reading

        john

          More by john

          Converting Blogspot / Blogger to hosted WordPress

          Posted on April 16, 2012

          First Try

          Migrating your Blogspot or Blogger weblog to a hosted WordPress installation should be easy.  But you may run into some problems along the way, so here are some quick ways to make it less painful.

          When I first attempted this, I used the Blogger Importer plugin found in the “Tools” → “Import” menu of a new WordPress installation.  It went OK.  The importer pulled in all of the posts and the comments, but there seemed to be some extra markup that made it’s way into the posts.  All of the post titles now started with “>”:

          >Why Hello There!

          Also, while images in the content seemed to be fine, none of the media was actually imported into WordPress, they were still being hosted on blogspot.com.

          Resetting the WordPress installation to start over

          So now WordPress has a bunch of content that I want to delete so that I can start over and hopefully get it right the next time.  For this I used WordPress Reset.  I was able to use it to remove the (bad) imported content and reset it back to a new installation.  It’s multi-site friendly and only restored the one site and left all the content from the parent and sibling sites alone – although I can’t vouch for what it would do if you ran it on the parent site of a network installation.

          A 2-Step Approach

          Now that I was back to square one it was time for a different approach.  After some reasearch, someone on the WordPress Forums mentioned using WordPress.com to do the Blogger import, then exporting from WordPress.com as a native WordPress export file (WXR / XML), and import that file into your new WordPress installation.

          Importing Blogspot / Blogger to WordPress.com

          So over at WordPress.com, everything is like a normal WordPress installation, except they have turned it up to 11.  They have powerful servers, and a dedicated staff that make amazing additions to the most popular free plugins – one of which is the Blogger import.  On WordPress.com you have an additional option to import a Blogger export file:

          Rather than authorizing the plugin to get everything from the site, we’ll export it to a file. Log into your Blogger account. When you’re logged in and viewing your blog there’s a “Design” link in the upper right menu bar, click that. Once you’re at the Blogger administration page click the “Settings” menu, and then “Other.” You will have an option under “Blog Tools” to “Export blog”:

          Take that file and import it into a new WordPress.com site. You’ll want to make sure that this blog is public (but not necessarily indexed by search engines) as our hosted WordPress installation will need to be able to pull media files from it, but we don’t want it to wind up on Google, as it’s only temporary. It might take a while to fully complete, but WordPress.com should bring in all your Posts, Comments, and Media just fine. Once you’re sure that it’s done, export the WordPress.com site through the “Tools” → “Export” menu, selecting “All Content.”

          Importing WordPress.com’s export into a hosted WordPress site

          Before we begin importing the content from WordPress.com, two things should be changed in the WordPress Importer plugin‘s wordpress-importer.php file, which can be done through the plugin editor.

          Just so we know when the importer runs into issues, change IMPORT_DEBUG to true:

          /** Display verbose errors */
          define( 'IMPORT_DEBUG', true );

          Then to make sure we get everything, find the line that starts with:

          $post_exists = post_exists( $post['post_title'], ...

          This was on line 539 for me.  Comment it out using slashes:

          //$post_exists = post_exists( $post['post_title'], ...

          Then just below that line add:

          $post_exists = false;

          I did this because when WordPress.com imported images from Blogger, it gave them the same name as the post title, So if a post was called “Why Hello There!” and it had 5 pictures in it, all 5 pictures also had the title “Why Hello There!” This was preventing the importer from getting everything. Plus I already reset the installation, so there’s no content to duplicate or overwrite anyway. And I reasonably trust that I want to retrieve everything contained in the WordPress.com export file.

          Run your import and everything should go smoothly. After you’ve verified you have everything in your new hosted WordPress installation, you can delete your WordPress.com site or make it private. You may also want to undo the WordPress Importer changes for future imports.

          Continue Reading

          justin

            More by justin
            • Prev
            • Page 1
            • Interim pages omitted …
            • Page 10
            • Page 11
            • Page 12
            • Page 13
            • Page 14
            • Interim pages omitted …
            • Page 20
            • Next

            Footer

            Get in Touch

            • New Project Inquiry
            • Product Support and General Inquiry
            • Store Purchase Terms and Conditions
            • Store FAQ
            • Cookie Policy
            • Privacy Policy

            Our Services

            • Custom WP Development
            • Theme Store
            • Plugin Store

            WordPress Plugins for Sale

            • Time Tracker
            • Authorize.net SIM Gateway

            WordPress Plugins for Free

            • Simple Calendar
            • WP Chargify
            • Facebook
            • Twitter
            • LinkedIn
            • WordPress
            • GitHub

            Copyright 2025 | 9seeds, LLC

            Like nearly all websites this one uses cookies too. Like most users we think consent banners like these are a dumb solution, but it's what we've got until new laws are passed. We use cookies on our website for remembering your preferences, for example if you're logged in or what is in your cart. We also use 3rd party cookies for analytics so we know what pages on the site are most popular. By clicking “Accept”, you consent to the use of ALL the cookies.
            Do not sell my personal information.
            Cookie SettingsAccept
            Manage consent

            Privacy Overview

            This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience and may even preclude you being able to login to the website.
            Necessary
            Always Enabled
            Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
            CookieDurationDescription
            __stripe_mid1 yearThis cookie is set by Stripe payment gateway. This cookie is used to enable payment on the website without storing any patment information on a server.
            __stripe_sid30 minutesThis cookie is set by Stripe payment gateway. This cookie is used to enable payment on the website without storing any patment information on a server.
            cookielawinfo-checkbox-advertisement1 yearSet by the GDPR Cookie Consent plugin, this cookie is used to record the user consent for the cookies in the "Advertisement" category .
            cookielawinfo-checkbox-analytics1 yearSet by the GDPR Cookie Consent plugin, this cookie is used to record the user consent for the cookies in the "Analytics" category .
            cookielawinfo-checkbox-necessary1 yearSet by the GDPR Cookie Consent plugin, this cookie is used to record the user consent for the cookies in the "Necessary" category .
            cookielawinfo-checkbox-others1 yearSet by the GDPR Cookie Consent plugin, this cookie is used to store the user consent for cookies in the category "Others".
            cookielawinfo-checkbox-performance1 yearSet by the GDPR Cookie Consent plugin, this cookie is used to store the user consent for cookies in the category "Performance".
            Functional
            Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
            Performance
            Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
            Analytics
            Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
            CookieDurationDescription
            _ga2 yearsThe _ga cookie, installed by Google Analytics, calculates visitor, session and campaign data and also keeps track of site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognize unique visitors.
            _gid1 dayInstalled by Google Analytics, _gid cookie stores information on how visitors use a website, while also creating an analytics report of the website's performance. Some of the data that are collected include the number of visitors, their source, and the pages they visit anonymously.
            CONSENT2 yearsYouTube sets this cookie via embedded youtube-videos and registers anonymous statistical data.
            Advertisement
            Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
            CookieDurationDescription
            VISITOR_INFO1_LIVE5 months 27 daysA cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface.
            YSCsessionYSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages.
            yt-remote-connected-devicesneverYouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
            yt-remote-device-idneverYouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
            Others
            Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
            CookieDurationDescription
            cookielawinfo-checkbox-functional1 yearThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
            SAVE & ACCEPT
            Powered by CookieYes Logo