Posts Tagged ‘WordPress’
Fillerama Shortcode
Tuesday, September 13th, 2011Filling designs/themes/templates/etc with text was a pain in the ass. Copy and pasting Lorem Ipsum 1000 times was getting a tad old. Enter Fillerama.
Now, you’re still doing the old copy/paste, but now you’re doing it with flare!
You can see a working demo of the shortcode lower down on the page, or you can continue reading the steps below.
…..but I’m working with WordPress, is there an easier way?
You’re already off to a great start!
Step 1. Download the shortcode from Github or copy and paste the code on the right. (The Github link is below)
Step 2. Open up functions.php and paste that code in.
Step 3. Goto the WordPress editor for whatever page/post you’re working on and add the [fillerama] shortcode.
Available options are [fillerama paragraphs=15 show=dexter lists=yes headers=yes]. No options are required.
Available shows are:
- doctorwho
- dexter
- futurama
- holygrail
- simpsons
- starwars
Things To Come
There are a few more things I’d like to add to the shortcode that aren’t included right now.
Ability to choose different showsSet the number of paragraphs- Add lists
- Random styling (strong, em, underline, etc)
- Clean it up
<?php
/*
file_get_contents in a file_get_contents-less world
*/
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/*
Fillerama
*/
function fillerama_shortcode($atts){
/*
Get the attributes
*/
extract( shortcode_atts( array(
'show' => 'futurama',
'paragraphs' => '5',
'show_headers' => 'yes',
'show_lists' => 'no'
), $atts ) );
/*
Grab the information from fillerama
*/
$response = file_get_contents_curl('http://api.chrisvalleskey.com/fillerama/get.php?count=100&format=json&show=' . $show);
/*
No response? Quit it.
*/
if(!$response){
return;
}
/*
Format the response and break it up until variables
*/
$response = json_decode($response);
$text = $response->db;
$headers = $response->headers;
$text_count = count($text);
$header_count = count($headers);
$random_limit = 10;
/*
Loop through the paragraphs
*/
for($i = 0; $i <= $paragraphs; $i++){
/*
Headers
*/
if($show_headers == 'yes'){
$head_tag = $i < 4 ? 'h' . ($i + 1) : 'h5';
$return .= "<$head_tag>" . $headers[rand(0, $header_count)]->header . "</$head_tag>";
}
/*
Render out the text
*/
for($a = 0; $a <= rand(0, $random_limit); $a++){
$text_return .= $text[rand(0, $text_count)]->quote . ' ';
/*
Randomly Input a break
*/
if($a == rand(0, $paragraphs)){
$text_return .= '</p><p>';
}
}
$return .= '<p>' . $text_return . '</p>';
$text_return = null;
}
return $return;
}
add_shortcode('fillerama', 'fillerama_shortcode');Demo
See-Through
I'm really more an apartment person. I love Halloween. The one time of year when everyone wears a mask … not just me.
The Lion Sleeps Tonight
I'm a sociopath; there's not much he can do for me.
I'm generally confused most of the time. I love Halloween. The one time of year when everyone wears a mask … not just me. You all right, Dexter?
Dexter
Cops, another community I'm not part of. Finding a needle in a haystack isn't hard when every straw is computerized. I'm not the monster he wants me to be. So I'm neither man nor beast. I'm something new entirely. With my own set of rules. I'm Dexter. Boo. You all right, Dexter? He taught me a code. To survive. Pretend. You pretend the feelings are there, for the world, for the people around you. Who knows? Maybe one day they will be.
If I Had a Hammer
I love Halloween. The one time of year when everyone wears a mask … not just me. Tonight's the night. And it's going to happen again and again. It has to happen.
Crocodile
Tell him time is of the essence.
Turning Biminese
I'm partial to air conditioning. You all right, Dexter? I feel like a jigsaw puzzle missing a piece. And I'm not even sure what the picture should be. Only you could make those words cute.
Github Shortcode
Saturday, September 3rd, 2011I wanted a really easy way to include my Github files in posts without having to copy and paste over the code.
Usage
Grab the code from below and throw it in your functions.php file.
Copy the link to the raw version of the Github file and add it to the [github] shortcode. The only required attribute is file. lang is optional — it allows you to force the language styling.
ie. [github file="https://raw.github.com/gist/1191517/ee7382153d40e4a07be9a5d8fc24ed373c574bcd/github_shortcode.php" lang="php"]
This will wrap your code in a <pre> tag that makes it easy to apply Google Prettify.
Code
<?php
/*
file_get_contents in a file_get_contents-less world
*/
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/*
Github formatter
*/
function github_shortcode($atts, $content = null) {
if($atts['file']){
$lang = $atts['lang'] ? "lang-$atts[lang]" : "lang-html";
$content = '<div class="github"><pre class="prettyprint linenums">';
$get_content = file_get_contents_curl($atts['file']);
$formatted_content = htmlspecialchars($get_content);
$content .= $formatted_content;
$content .= '</pre><p class="view_raw"><a href="' . $atts['file'] . '" target="_blank">View Raw</a></p></div>';
return $content;
}
}
add_shortcode('github', 'github_shortcode');Bootstrap for WordPress
Saturday, September 3rd, 2011A few weeks ago the Twitter team released Bootstrap.
I immediately thought that this would be a great opportunity to take what they’ve created and turn it into a WordPress theme for people to use as a starting point.
It’s currently a WIP, but you can view the code on Github: https://github.com/envex/Bootstrap-WordPress
Simply install it like you would any other WordPress theme and your on your way.
Bootstrap is a toolkit from Twitter designed to kickstart development of webapps and sites.
It includes base CSS and HTML for typography, forms, buttons, tables, grids, navigation, and more.
WPCoder Love
Wednesday, August 3rd, 2011About a month ago I had a project that had a feature that allowed users to love posts. Instead of creating a one-off solution, Dan suggested that I create a plugin that we could use in future projects.
WordPress Custom Widget
Friday, July 22nd, 2011Sometimes to keep a site easy to update for a client your best bet is to create a simple widget. Just like the custom post type template, I thought it would be easiest to create something that I can re-use over and over.
How to Use
Step 1.
Copy the custom_widget.php file and place it in your WordPress theme folder. Assuming WordPress is installed in the root of your server the file will be place in /wp-content/themes/yourtheme/
Step 2.
Open up functions.php and include the following piece of code somewhere in the file. include TEMPLATEPATH . '/custom_widget.php';
Step 3.
Edit the custom_widget.php file to suit your needs.
A few things to consider
- The name of your class must also be the name of your first function.
- Don’t forget to add your variables to the
widget(),form()andupdate()functions.
<?php
/*
Custom Widget Template
by Matt Vickers - http://envexlabs.com
Usage
Step 1: Change any values you may need to change
Step 2: Include this file in your functions.php file. ex: include TEMPLATEPATH . '/custom_widget.php';
Step 3: Impress your friends
*/
class Custom_Widget extends WP_Widget {
function Custom_Widget() {
$widget_ops = array('classname' => 'custom_widget', 'description' => 'Just a custom widget. Move along.' );
$this->WP_Widget('custom_widget', 'Custom Widget', $widget_ops);
}
function widget($args, $instance) {
/*
Your variables from form() are automatically added to the $instance variable
just use the array values when echoing out the HTML
*/
echo '<h3>' . $instance['title'] . '</h3>';
echo '<p>' . $instance['text'] . '</p>';
}
function form($instance) {
/*
If you're going to add inputs to your widget, just use the code below as a template
of what to do to add an input.
1. Add to $instance
2. Create a new variable
3. Add the HTML
*/
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '') );
$title = strip_tags($instance['title']);
$entry_title = strip_tags($instance['text']); ?>
<p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('text'); ?>">Text: <textarea class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo attribute_escape($entry_title); ?></textarea></label></p>
<?php
}
/*
Nothing to edit below here
*/
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['text'] = strip_tags($new_instance['text']);
return $instance;
}
}
register_widget('Custom_Widget');WordPress Custom Post Types
Tuesday, July 5th, 2011I find that 95%* of the projects I work on now require a custom post type. After writing the same file over and over, I decided to throw up a template that I could use for any project.
To use the custom post template in your WordPress theme, simply follow the instructions below and you’ll be golden!
How to Use
Step 1.
Copy the custom_post_types.php file and place it in your WordPress theme folder. Assuming WordPress is installed in the root of your server the file will be place in /wp-content/themes/yourtheme/
Step 2.
Open up functions.php and include the following piece of code somewhere in the file. include TEMPLATEPATH . '/custom_post_types.php';
Step 3.
Edit the custom_post_types.php file to suit your needs.
<?php
/*
Setup and add our custom post types
*/
function create_post_types(){
/*
Products
*/
register_post_type( 'my_products',
array(
'labels' => array(
'name' => 'Products',
'singular_name' => 'Product',
'add_new' => 'Add New Product',
'add_new_item' => 'Add New Product',
'edit_item' => 'Edit Product',
'new_item' => 'New Product'
),
'public' => true,
'rewrite' => array(
'slug' => 'products'
),
'supports' => array(
'title',
'editor',
'excerpt',
'page-attributes',
'revisions',
'thumbnail',
)
)
);
}
function create_custom_meta(){
/*
Products
*/
add_meta_box("my_products", "Product Information", "my_products_meta", "my_products", "normal", "low");
function my_products_meta(){
global $post;
$my_model_number = get_post_meta($post->ID, 'my_model_number', true);
echo '<p><label>Model Number(s): </label><input type="text" name="my_model_number" value="' . $my_model_number . '" /><br /><small><em>For awesome people</em></small></p>';
}
/*
Save all our info!!
*/
function save_details($post_id){
global $post;
/*
Save Product Info
*/
if(isset($_POST['post_type']) && ($_POST['post_type'] == "edisson_products")) {
foreach($_POST as $k => $v){
update_post_meta($post_id, $k, $v);
}
}
}
add_action("save_post", "save_details");
}
add_action('init', 'create_post_types');
add_action('admin_menu', 'create_custom_meta');
/*
Create the Product Types
*/
add_action( 'init', 'create_product_type_taxonomies', 0 );
function create_product_type_taxonomies() {
$labels = array(
'name' => _x( 'Product Types', 'products' ),
'singular_name' => _x( 'Product Type', 'product' ),
'search_items' => __( 'Search Product Types' ),
'all_items' => __( 'All Product Types' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Product Type' ),
'update_item' => __( 'Update Product Type' ),
'add_new_item' => __( 'Add New Product Type' ),
'new_item_name' => __( 'New Product Type Name' ),
);
register_taxonomy( 'product_type', array( 'my_products' ), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'product' ),
));
}