Skip to content

Instantly share code, notes, and snippets.

@leocristofani
Last active December 15, 2015 21:39
Show Gist options
  • Save leocristofani/5327740 to your computer and use it in GitHub Desktop.
Save leocristofani/5327740 to your computer and use it in GitHub Desktop.
WordPress Theme's functions.php file with useful constants and boilerplate code for registering menus and sidebars.
<?php
/* DEFINE CONSTANTS
========================================== */
define('THEME_DIR', get_stylesheet_directory_uri() );
define('THEME_IMAGES_DIR', THEME_DIR . '/assets/img' );
/* REGISTER MENUS
========================================== */
function register_my_menus()
{
register_nav_menus(
array(
'primary_menu' => 'Primary Menu',
)
);
}
add_action('init', 'register_my_menus');
/* REGISTER SIDEBARS
========================================= */
if( function_exists('register_sidebar') )
{
// Primary Sidebar
register_sidebar(
array(
'name' => 'Primary Sidebar',
'id' => 'primary_sidebar',
'description' => 'Sidebar description.',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h4>',
'after_title' => '</h4>',
)
);
// Single Page Sidebar
register_sidebar(
array(
'name' => 'Single Sidebar',
'id' => 'single_sidebar',
'description' => 'Sidebar description.',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h4>',
'after_title' => '</h4>',
)
);
}
/* ADD THEME SUPPORT FOR POST THUMBNAIL, POST FORMATS
======================================================= */
if(function_exists('add_theme_support'))
{
add_theme_support('post-formats', array('link','quote','gallery'));
add_theme_support('post-thumbnails', array('post'));
}
/* Callback function called for every comment displayed
====================================================== */
function my_comments_callback($comment, $args, $depth)
{
$GLOBALS['comment'] = $comment;
if(get_comment_type() == 'pingback' || get_comment_type() == 'trackback') : ?>
<li id="comment-<?php comment_ID(); ?>" class="pingback">
<article <?php comment_class('clearfix'); ?>>
<header>
<h5>Pingback</h5>
<p><?php edit_comment_link(); ?></p>
</header>
<p><?php comment_author_link(); ?></p>
</article>
</li>
<?php elseif(get_comments_type() == 'comment') : ?>
<li id="comment-<?php comment_ID(); ?>">
<article class="<?php comment_class('clearfix'); ?>">
<header>
<h5><?php comment_author_link(); ?></h5>
<p>
<span>on <?php comment_date(); ?> at <?php comment_time(); ?> - </span>
<?php comment_reply_link(array_merge($args, array('depth' => $depty, 'max-depth' => $args['max-depth']))); ?>
</p>
</header>
<figure class="comment-avatar">
<?php
$avatar_size = 80;
if($comment->comment_parent != 0 )
{
$avatar_size = 64;
}
echo get_avatar($comment, $avatar_size);
?>
</figure>
<?php if($comment->comment_approved == '0' : ?>
<p class="waiting-moderation">Comment waiting moderation</p>
<?php endif; ?>
<?php comment_text(); ?>
</article>
</li>
<?php endif;
}
// CUSTOM COMMENTS FORM
// ===================================
function my_custom_comment_form($defaults)
{
// remove the note on the top of the form
$defaults['comment_notes_before'] = '';
$defaults['id_form'] = 'comment_form';
$defaults['comment_field'] = '<p>' .
'<textarea name="comment" id="comment" cols="30" rows="10"><textarea>' .
'</p>';
return $defaults;
}
function my_custom_default_comment_fields()
{
$commenter = wp_get_current_commenter();
$req = get_option('require_name_email');
$aria_req = $req ? "aria-required=true" : '';
$fields = array(
'author' => '<p>' .
'<input type="text" id="author" name="author" value="' . esc_attr($commenter['comment_author']) . '" ' . $aria_req . '>' .
'<label for="author">Name ' . $req ? '*' : '' . '</label>' .
'</p>',
'email' => '<p>' .
'<input type="text" id="email" name="email" value="' . esc_attr($commenter['comment_author_email']) . '" ' . $aria_req . '>' .
'<label for="email">Email ' . $req ? '*' : '' . '</label>' .
'</p>',
'url' => '<p>' .
'<input type="text" id="url" name="url" value="' . esc_attr($commenter['comment_author_url']) . '">' .
'<label for="url">Website ' . $req ? '*' : '' . '</label>' .
'</p>',
);
return $fields;
}
add_filter('comment_form_defaults', 'my_custom_comment_form');
add_filter('comment_form_default_fields', 'my_custom_default_comment_fields');
/* LOAD CUSTOM WIDGETS
====================== */
require_once('functions/widget-ad.php');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment