Easy Content Templates WordPress Plugin

Easy Content Templates is a plugin for WordPress that lets you define content templates to quickly apply to new or existing posts and pages. By default, templates are private to the author/creator only. Templates can be shared to other users by checking the “Make this Template available for others” check box. Other users can’t edit your template, they can only use it.

Easy Content Templates WordPress Plugin

Plugin Links

Frequently Asked Questions

How do I create templates?

In your WordPress admin, usually under the Comments section, there will be a Templates section (much like Posts and Pages). Hover over it and click Add New.

How do I insert them into my posts or pages?

In your write screen look to the upper right and there’s a box there. Select a template from the drop-down and click “Load Template”.

Nothing shows up in the drop-down list, what do I do?

First you need to define your templates, go to the Templates section of your WordPress admin and create your templates there.

Can I define multiple templates?

Sure you can! ;) Templates are actually Custom Post Types so you can make a lot of templates.

What does the “Share this Template with others.” check box do?

If you define a template and check that box, it means that you are allowing users to use your template. That is, they will be able to create posts or pages using that template. This does not mean that they will see your template in the administration area or they will be able to edit it.

When I create a post from a template, how do i show the post date?

Just use the [postdate] shortcode, it will display the date the post was published. You can control the output format by using the format parameter like this: [postdate format="M j, Y"] Format options are similar with PHP’s date function.

Been playing around with SQLite in a software I’ve been developing and one thing I realized is that MySql is painfully and ridiculously slower than SQLite. In the same setup, my app can rebuild the entire database structure in SQLite within almost no time, whereas in MySql it takes almost a minute to do the same task. But note that SQLite is only for small to medium sized apps that stores local data.

How to Use WP3.5 Media Manager In Your Plugin or Theme

Note: This is a restoration of a tutorial I did a few months back before my blog went down.

It has been a few days already since WordPress 3.5 was released. There are a lot of simple changes since the previous version. One thing i noticed immediately and kind of my favorite for this release is the shape of the buttons.

Wordpress-3.5-Elvin-Button-Shapes

Cool huh? The next thing i noticed is probably the biggest thing in this release. The new media manager screen (it’s the featured image above). I wanted to use it in the theme i was working on for selecting slides. So here’s how i did it.

Prerequisites

  • You must already know what you’re doing with PHP, Javascript, jQuery and CSS.
  • Also you must already know what you’re doing when making a theme or a plugin depending on what you’re trying to do.

Step 1: Add the necessary scripts and styles

First we need to have the necessary Javascript and CSS files loaded when our admin screen loads. For that we use the code below. This should go to the functions.php for themes and the PHP plugin file for plugins.

<?php
function enqueue_media_stuff(){
    wp_enqueue_media();
}
add_action('init', 'enqueue_media_stuff');
?>

Step 2: Prepare the HTML

I’ll be using a simple button here, you should already know how to make custom admin screens and where to put the following code.

<a href="#" id="btn_browse_files">Browse Files</a>

Step 3: The meat of this tutorial

But first let me explain how i code Javascript when using jQuery (which is always). I use a function that is executed immediately. Inside that function i can freely use the $ (jQuery object passed as a parameter under the name $) without any conflicts since this is localized. Of course like any other techniques and practices there are pros and cons to this as well, but i don’t really encounter the cons as much as i enjoy using the pros to this style. Here’s the basic Javascript i always write.

(function($){
    // local doc object
    var doc = {
        ready: function(){
            some_object.init();
        }
    },
    // an object that serves a function
    some_object = {
        init: function(){
            // initialization codes here
        }
    };
    $(document).ready(doc.ready);
})(jQuery);

Yeah so that’s it, i don’t know how it looks to readers that never used that style before but looks minimal to me. Specially if the site you’re developing has lots of Javascript features.

We’ll now go back to the tutorial. Here’s the code that i’ll try to explain below.

(function($){
    var doc = {
        ready: function(){
            // initialize only if our button is in the page
            if($('#btn_browse_files').length > 0){
                slider.init();
            }
        }
    },
    slider = {
        // the following 2 objects would be our backup containers
        // as we will be replacing the default media handlers
        media_send_attachment: null,
        media_close_window: null,
        init: function(){
            // bind the button's click the browse_clicked handler
            $('#btn_browse_files').click(slider.browse_clicked);
        },
        browse_clicked: function(event){
            // cancel the event so we won't be navigated to href="#"
            event.preventDefault();

            // backup editor objects first
            slider.media_send_attachment = wp.media.editor.send.attachment;
            slider.media_close_window = wp.media.editor.remove;

            // override the objects with our own
            wp.media.editor.send.attachment = slider.media_accept;
            wp.media.editor.remove = slider.media_close;

            // open up the media manager window
            wp.media.editor.open();
        },
        media_accept: function(props, attachment){
            // this function is called when the media manager sends in media info
            // when the user clicks the "Insert into Post" button
            // this may be called multiple times (one for each selected file) 
            // you might be interested in the following:
            // alert(attachment.id); // this stands for the id of the media attachment passed
            // alert(attachment.url); // this is the url of the media attachment passed
            // for now let's log it the console
            // not you can do anything Javascript-ly possible here
            console.log(props);
            console.log(attachment);
        },
        media_close: function(id){
            // this function is called when the media manager wants to close
            // (either close button or after sending the selected items)

            // restore editor objects from backup
            wp.media.editor.send.attachment = slider.media_send_attachment;
            wp.media.editor.remove = slider.media_close_window;

            // nullify the backup objects to free up some memory
            slider.media_send_attachment= null;
            slider.media_close_window= null;

            // trigger the actual remove
            wp.media.editor.remove(id);
        }
    };
    $(document).ready(doc.ready);
})(jQuery);

Using the code above, the wrapper function is executed immediately after the browser loads the script. The wrapper function initializes objects, doc and slider then it adds a handler to the document.ready event to doc’s ready function. The browser will execute that handler when the page is “ready” or when the whole webpage is fully loaded.

Inside the doc.ready function, it checks (using jQuery) if the button we want to bind with exists. If it does it calls slider.init. Inside slider.init, it binds the Html A button’s click event to the slider.browse_clicked handler. So when the user clicks the Html A button slider.browse_clicked is called.

Once inside slider.browse_clicked, it stores the default media handlers in backup variables that is setup at initialization (slider.media_send_attachment and slider.media_close_window), replaces them with the ones inside the slider object (slider.media_accept and slider.media_close), and then opens up the media manager dialog window.

When the user is in the media manager, WordPress handles everything that happens there except for the two things that we just replaced.

Possibility 1: Selects and inserts media attachments

If the user selects media files and clicks “Insert into Post”, slider.media_accept gets called for each media file the user selects and then slider.media_close gets called. Inside slider.media_close, we restore the default media handlers and calls the original close handler to close the dialog. And the whole process is done.

Possibility 2: Just closes the media manager box

If the user decided to do something else and closes the media manager dialog, slider.media_close is called immediately doing exactly what happened in Possibility 1 (but slider.media_accept is skipped).

End of Tutorial

That’s it, you now have the knowledge to use the new WP 3.5 Media Manager in your WordPress themes and plugins. Congratulations!

Please let me know how you find this tutorial through comments below.

Easy Content Templates WordPress Plugin

Update: I put up a restoration post about Easy Content Templates (just click this line)!

For the people already using Easy Content Templates and trying to access this page for support or questions. I apologize for being offline for a few months. I had $ome technical i$$ue$. I’ll be rebuilding this page (and my whole blog actually) over the next few days and hopefully we can get back to the point where you can connect with me regarding Easy Content Templates.

For now here’s a picture of a baby giraffe!

Easy Content Templates Baby Giraffe