wordpress Year Shortcode

by in , 0

For the functions.php file:

function year_shortcode() {
  $year = date('Y');
  return $year;
}
add_shortcode('year', 'year_shortcode');

Usage

Use [year] in your posts.

wordpress Using Custom Fields

by in , 0

Dump out all custom fields as a list

<?php the_meta(); ?>

Display value of one specific custom field

<?php echo get_post_meta($post->ID, 'mood', true); ?>

"mood" would be ID value of custom field

Display multiple values of same custom field ID

<?php $songs = get_post_meta($post->ID, 'songs', false); ?>
<h3>This post inspired by:</h3>
<ul>
	<?php foreach($songs as $song) {
		echo '<li>'.$song.'</li>';
	} ?>
</ul>

Display custom field only if exists (logic)

<?php 
    $url = get_post_meta($post->ID, 'snippet-reference-URL', true); 

	if ($url) {
	    echo "<p><a href='$url'>Reference URL</a></p>";
	}
?>

Reference URL

wordpress Turn on WordPress Error Reporting

by in , 0

Comment out the top line there, and add the rest to your wp-config.php file to get more detailed error reporting from your WordPress site. Definitely don't do this live, do it for local development and testing.

// define('WP_DEBUG', false);

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
@ini_set('display_errors', 0);

wordpress Turn On More Buttons in the WordPress Visual Editor

by in , 0

These buttons (like one for adding an <hr> tag) aren't there by default in the WordPress visual editor, but you can turn them on pretty easily. This is code for your active theme's functions.php file, or make a functionality plugin.

function add_more_buttons($buttons) {
 $buttons[] = 'hr';
 $buttons[] = 'del';
 $buttons[] = 'sub';
 $buttons[] = 'sup';
 $buttons[] = 'fontselect';
 $buttons[] = 'fontsizeselect';
 $buttons[] = 'cleanup';
 $buttons[] = 'styleselect';
 return $buttons;
}
add_filter("mce_buttons_3", "add_more_buttons");

Reference URL

wordpress Spam Comments with Very Long URL’s

by in , 0

Super long URL's are a sure fire sign the comment is spammy. This will mark comments with URL's (as the author URL, not just in the text) longer than 50 characters as spam, otherwise leave their state the way it is.

<?php

  function rkv_url_spamcheck( $approved , $commentdata ) {
    return ( strlen( $commentdata['comment_author_url'] ) > 50 ) ? 'spam' : $approved;
  }

  add_filter( 'pre_comment_approved', 'rkv_url_spamcheck', 99, 2 );

?>

Reference URL

wordpress Shrink the Admin Bar / Expand on Hover

by in , 0

A mini plugin:

<?php
/*
 * Plugin Name: Mini Admin Bar
 * Plugin URI: http://www.netyou.co.il/
 * Description: Makes the admin bar a small button on the left and expands on hover.
 * Version: 1.0
 * Author: NetYou
 * Author URI: http://www.netyou.co.il/
 * License: MIT
 * Copyright: NetYou
*/
 
add_action('get_header', 'my_filter_head');
function my_filter_head() { remove_action('wp_head', '_admin_bar_bump_cb'); }
 
function my_admin_css() {
        if ( is_user_logged_in() ) {
        ?>
        <style type="text/css">
            #wpadminbar {
                width: 47px;
                min-width: auto;
                overflow: hidden;
                -webkit-transition: .4s width;
                -webkit-transition-delay: 1s;
                -moz-transition: .4s width;
                -moz-transition-delay: 1s;
                -o-transition: .4s width;
                -o-transition-delay: 1s;
                -ms-transition: .4s width;
                -ms-transition-delay: 1s;
                transition: .4s width;
                transition-delay: 1s;
            }
            
            #wpadminbar:hover {
                width: 100%;
                overflow: visible;
                -webkit-transition-delay: 0;
                -moz-transition-delay: 0;
                -o-transition-delay: 0;
                -ms-transition-delay: 0;
                transition-delay: 0;
            }
        </style>
        <?php }
}
add_action('wp_head', 'my_admin_css');

?>

Reference URL

wordpress Show Your Favorite Tweets with WordPress

by in , 0

Just replace the URL in the fetch_rss line below with the RSS feed to your Twitter favorites. In fact this will work with any RSS feed.

<?php
    include_once(ABSPATH . WPINC . '/feed.php');
    $rss = fetch_feed('http://twitter.com/favorites/793830.rss');
    $maxitems = $rss->get_item_quantity(3); 
    $rss_items = $rss->get_items(0, $maxitems);
?>

<ul>
    <?php if ($maxitems == 0) echo '<li>No items.</li>';
    else
    // Loop through each feed item and display each item as a hyperlink.
    foreach ( $rss_items as $item ) : ?>
    <li>
        <a href='<?php echo $item->get_permalink(); ?>'>
            <?php echo $item->get_title(); ?>
        </a>
    </li>
    <?php endforeach; ?>
</ul>