Gizmo: Great Quotes – Daily

by on March 1, 2011

Gizmos are shortcodes that pull content from external sites and services to enhance your own original content. They're intended chiefly for utilization as sidebar Text Widgets … but, you can use many of them anywhere in your posts and pages.

Do you think that incorporating snippets of external content could enhance your articles, tutorials, and news. You bet! So … regarding other people's content, I've got a question for you:

R U Down wit OPC?

Today's Gizmo offers daily quotes in your choice of six categories: Friendship, Funny, Inspirational, Life, Love, and Motivational. Great-Quotes.com offers over 2 million easily searchable famous quotes, sayings, proverbs, & movie quotes. Plus, they generously offer their quote of the day content to blogs and other web sites.

But like many others, the visual format they provide doesn't offer design options. That's where gizmos come to the rescue. They extract the content so that you can format it to match your blog. We do, however, retain the author's backlink in the original format.

Update: As of May 1, 2011, this provider now offers some configuration options for external implementation of the quotes. However, the following code is still a viable alternative.

The following are two sample outputs using this gizmo:

Here's the shortcode for the first one (using the default styles):
[gizmo_great_quotes inspirational]

And, this is the second one (implementing custom styles):
[gizmo_great_quotes type='friendship' style_container='font-size:16px; color:#003366; font-weight:bold; line-height:133%;' style_author='float:right; font-size:12px; color:#000000;']

The available types are:

Friendship
Funny
Inspirational
Life
Love
Motivational

Plus, we can put it anywhere on the page like this:
 
Friendship Quote

“.

Page Not Found

Sorry, the page you're looking for never existed or has gone missing. Give us a shout through our contact form, or perhaps continue browsing through our authors.


Recommended

Like us on Facebook for an inspiring quote every day!

ody>

 
To use this gizmo, put the following code in your theme's functions.php file.

Code: PHP (plus WordPress)Gizmo: Great Quotes - Daily

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
add_shortcode('gizmo_great_quotes', 'gizmo_great_quotes_shortcode_handler');
 
function gizmo_great_quotes_shortcode_handler($atts)
{
    // use a style of ' ' (one blank space) for no style
 
    if (sizeof($atts) == 1)
    {
        return gizmo_great_quotes($atts['0']);
    }
 
    $parms = shortcode_atts(array(
        'type' => 'inspirational',
        'style_container' => 'padding:4px;',
        'style_content' => '',
        'style_author' => 'float:right;font-style:italic;'),
        $atts);
 
    $type = $parms['type'];
    $style_container = $parms['style_container'];
    $style_content = $parms['style_content'];
    $style_author = $parms['style_author'];
 
    if ($style_content != '') {$style_container = $style_content;}
    if ($style_container == '') {$style_container = ' ';}
    if ($style_author == '') {$style_author = ' ';}
 
    return gizmo_great_quotes($type, $style_container, $style_author);
}

As you can see from the source code, there are default values for each of the attributes. You can even omit the type, in which case it will default to inspirational. If you only want to change the type, you don't need to use the attribute name.

Finally, you can use either style_container or style_content for the primary CSS. The later is an alias of the former.

Code: PHP (plus WordPress)Function: gizmo_great_quotes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function gizmo_great_quotes(
        $type='inspirational',
        $style_container='',
        $style_author='')
{
    // valid types: Friendship, Funny, Inspirational, Life, Love, Motivational
    // style_container = valid CSS (no quotes)
    // style_author = valid CSS (no quotes)
 
    // get the type (translation table is better than simply ucfirst())
    $type = strtolower($type);
    $xlate = array
    (
        'friendship' => 'Friendship',
        'friends' => 'Friendship',
        'funny' => 'Funny',
        'humor' => 'Funny',
        'inspirational' => 'Inspirational',
        'inspiration' => 'Inspirational',
        'inspire' => 'Inspirational',
        'life' => 'Life',
        'wisdom' => 'Life',
        'Wisdom' => 'Life',
        'love' => 'Love',
        'romance' => 'Love',
        'motivational' => 'Motivational',
        'motivation' => 'Motivational',
        'motivate' => 'Motivational',
    );
    $xlate_type = strtr($type, $xlate);
 
    // failsafe (if not an accepted type, default to 'Inspirational')
    $valid_types = array('Friendship', 'Funny', 'Inspirational', 'Life', 'Love', 'Motivational');
    if (!in_array($xlate_type, $valid_types)) {$xlate_type = 'Inspirational';}
 
    // grab the quote content
    $category = $xlate_type;
    if ($xlate_type == 'Friendship') {$category = 'Friends+Or+Friendship';}
    $service_url = "http://services.great-quotes.com/dailyquote?category=$category;action=daily_quote";
    $quotes = file_get_contents($service_url);
 
    // strip linefeeds
    $quotes = trim($quotes);
    $quotes = str_replace("\n", '', $quotes);
    $quotes = str_replace("\r", '', $quotes);
 
    // repair bible quotes
    $quotes = str_replace('Bible,', 'Bible', $quotes);
 
    // strip HTML comment
    $quotes = str_replace('<!-- C -->', '', $quotes);
 
    // repair occasional missing period
    $pos = strpos($quotes, '<br />');
    if (($quotes{$pos - 1} != '.') && ($quotes{$pos - 1} != ' '))
    {
        $quotes = substr($quotes, 0, $pos) . '.' . substr($quotes, $pos, strlen($quotes) - $pos - 1);
    }
    $quotes = str_replace('].', ']', $quotes);
 
    // insert closing quotation mark
    $quotes = str_replace('<br />', '&#8221;<br />', $quotes);
 
    // author style
    if ($style_author == '')
    {
        $style_author = 'float:right;font-style:italic;';
    }
    $quotes = str_replace('<b>', '<div style="' . $style_author . '">-- ', $quotes);
    $quotes = str_replace('</b', '</div>', $quotes);
    $quotes = str_replace('>>', '>', $quotes);
    $quotes = '&#8220;' . $quotes;
 
    // attach the credit link
    $style_link = 'color:#4B4B4B;font-weight:normal;font-size:10px;';
    $quotes .= '<br/><div style="text-align:center;"><span style="' . $style_link . '">More </span>';
    $quotes .= '<a style="' . $style_link . '" href="http://www.great-quotes.com/" target="_blank">Famous Quotes</a></div>';
 
    // construct the container
    if ($style_container == '') {$style_container = 'padding:4px;';}
    $quotes = '<div style="' . $style_container . '">' . $quotes . '</div>';
 
    // clean-up
    $quotes = str_replace('.&#8221;.&#8221;', '.&#8221;', $quotes);
    $quotes = str_replace('<br />', '<p style="margin-top:-10px;">&nbsp;</p>', $quotes);
 
    return $quotes;
}
 
/**********************************************************************
     Copyright © 2011 Gizmo Digital Fusion (http://wpCodeSnippets.info)
     you can redistribute and/or modify this code under the terms of the
     GNU GPL v2: http://www.gnu.org/licenses/gpl-2.0.html
    **********************************************************************/
}

The code above provides the workhorse functionality for our shortcode. My first impulse was to simply use the PHP function ucfirst() for the user-provided type parameter. But, using a translation table instead (lines 11-34) allows us to accept variations … for convenience.

Next, lines 36-40 load content from the server. A good portion of the remaining code is correcting errors in the provided quote and offsetting shortcode formatting issues.

In lines 64-72, we insert the author style. And, in lines 74-77, we append a link back to the provider's web site.

Share This Article: “Gizmo: Great Quotes – Daily”

(Also Available: Press CTRL+D to Bookmark this Page)

Comments

Share Your Thoughts  5 Responses to “Gizmo: Great Quotes – Daily”
  1. 1
    randi says:

    wpCodeSnippets.info is one of my favorite websites now !

  2. 2
    uph says:

    wpcodesnippets.info is full of interesting posts!

  3. 3
  4. 4
    blog says:

    Not all companies are made equal, plus some are a lot more
    expensive compared to others. It is very compact to carry and easy to
    operate as well. It is a fact that 10 billion farmed animals die each year to become meat products
    for the American public.

    I absolutely love your website.. Pleasant colors & theme.
    Did you develop this web site yourself? Please reply
    back as I’m looking to create my very own website and want
    to find out where you got this from or exactly what the theme is named.
    Thank you!

    My site blog

  5. 5

    When someone writes an post he/she retains the idea of a user in his/her mid that
    hoow a uuser can be aware of it. Thus that’s why this post is
    outstdanding. Thanks!

    Alsoo visit my blog … love quotes for her tumblr

Share Your Thoughts

(Some editor features are restricted unless you're logged in.)

(When replying to a specific comment, your browser may require Shift+Enter instead of just Enter.)


(get a gravatar)


Notify me of followup comments via e-mail. You can also subscribe without commenting.