Skip to content

Instantly share code, notes, and snippets.

@RevConcept
Last active December 24, 2015 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RevConcept/6782122 to your computer and use it in GitHub Desktop.
Save RevConcept/6782122 to your computer and use it in GitHub Desktop.
Example of the functionality plugin and the functions.php file used for the RSG website.
<?php
/* ==========================================================================
Load jQuery
========================================================================== */
// NOTE: Deregisters the native install of jQuery and loads a specific version from Google.
if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
wp_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
wp_enqueue_script('jquery');
}
/* ==========================================================================
Enque Scripts / Styles
========================================================================== */
// NOTE: Registers and enqueues custom scripts and styles specific to the RSG theme.
function rsg_scripts_method() {
wp_register_script( 'functions', get_template_directory_uri() . '/scripts/functions.js', array('jquery'), '1.0.0', true );
wp_register_script( 'colorbox', get_template_directory_uri() . '/scripts/jquery.colorbox.min.js', array('jquery'), '1.3.12', true );
wp_register_script( 'hoverIntent', get_template_directory_uri() . '/scripts/jquery.hoverIntent.min.js', array('jquery'), true );
wp_register_style( 'colorbox', get_template_directory_uri() . '/css/colorbox.css' );
wp_enqueue_script( 'functions' );
wp_enqueue_script( 'colorbox' );
wp_enqueue_script( 'hoverIntent' );
wp_enqueue_style( 'colorbox' );
}
add_action( 'wp_enqueue_scripts', 'rsg_scripts_method' );
/* ==========================================================================
Add Links Feed
========================================================================== */
// NOTE: Adds RSS feed links to HTML <head>.
add_theme_support( 'automatic-feed-links' );
/* ==========================================================================
Clean Up Head
========================================================================== */
// NOTE: Cleans up some of the extra clutter produced by wp_head()
function removeHeadLinks() {
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
}
add_action('init', 'removeHeadLinks');
remove_action('wp_head', 'wp_generator');
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => __('Sidebar Widgets','html5reset' ),
'id' => 'sidebar-widgets',
'description' => __( 'These are widgets for the sidebar.','html5reset' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}
/* ==========================================================================
Add Theme Support
========================================================================== */
// NOTE: Adds theme support for featured images and defines the post thumbnail size.
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 460, 250, true ); // default Post Thumbnail dimensions
}
/* ==========================================================================
Custom Thumbnail Sizes
========================================================================== */
// NOTE: Adds additional images sizes tot he theme.
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'featured-thumb', 300, 175, true ); //(cropped)
add_image_size( 'member-photo', 140, 140, true ); //(cropped)
add_image_size( 'project-slider', 620, 340, true ); //(cropped)
add_image_size( 'project-zoom', 940 );
add_image_size( 'member-resume', 225, 315, true ); //(cropped)
}
/* ==========================================================================
Remove Admin Menu Items: Based on Editor Role
========================================================================== */
// NOTE: Removes the "Customize" sub-menu item from the menu if the user is NOT and "Administrator Role".
function my_remove_menu_pages() {
if(!current_user_can('add_users')) {
remove_submenu_page('themes.php', 'customize.php');
}
}
add_action( 'admin_menu', 'my_remove_menu_pages' );
/* ==========================================================================
Register Custom Menu
========================================================================== */
// NOTE: Registers a custom menu called "Main Menu" -- this allows the client to now control the theme's menu/navigation.
function register_my_menus() {
register_nav_menus(array('main-menu' => __( 'Main Menu' )));
}
add_action( 'init', 'register_my_menus' );
/* ==========================================================================
Increment wp_nav_menu li and add classes
========================================================================== */
// NOTE: Adds special classes based on menu item positions -- this fixes the alignment due to the "split menu" style where the logo is in the middle.
function custom_nav_class($classes, $item){
// var_dump($item->menu_order);
// $classes[] = "custom-class-".$item->menu_order;
if ( $item->menu_order == 3 ) {
$classes[] = "large-margin";
}
if ( $item->menu_order > 3 ) {
$classes[] = "right-menu";
}
return $classes;
}
add_filter('nav_menu_css_class' , 'custom_nav_class' , 10 , 2);
/* ==========================================================================
Register Footer Widgets: for example of bad practice
========================================================================== */
// NOTE: The RSG theme isn't using this -- it's here for the example of bad practices: hard-coding menus into widgets.
if ( function_exists('register_sidebar') ) {
register_sidebars(3, array('name'=>'Footer %d'));
}
?>
<?php
/**
* Plugin Name: RSG Portable Functions
* Plugin URI: http://richardsolomongroup.com
* Description: Functionality that should stay with this site.
* Author: Chris Perryman
* Author URI: http://revelationconcept.com
* Version: 0.1.0
*/
/* ==========================================================================
Add Capabilities
========================================================================== */
// NOTE: This function extends the "Editor Role" the ability to have full access to the "Gravity Forms" plugin -- usually reserved for an "Adnimistrator Role". I included this in the plugin so the user doesn't lose the ability to access forms down the road.
function add_grav_forms(){
$role = get_role('editor');
$role->add_cap('gform_full_access');
}
add_action('admin_init','add_grav_forms');
// NOTE: This function extends the "Editor Role" the ability to control the site's menus (it adds the "Appearance" menu item) -- usually reserved for an "Adminitrator Role". I included this in the plugin so the user can continue to manage the site's menus whether or not the theme changes etc.
// ALSO: I remove "Customize" from the "Appearancec" menu because I don't want the client to use that feature -- I do that in functions.php, not here -- the reason for this is so if and when the client uses abother theme, they are not limited...I just don't want them messing with "my" theme ;)
function rsg_edit_menus(){
$role = get_role('editor');
$role->add_cap('edit_theme_options');
}
add_action('admin_init', 'rsg_edit_menus');
/* ==========================================================================
Register Custom Post Types
========================================================================== */
// NOTE: This function registers the post type "Projects". This is content crutial to the client's site and they will want to be able to retian this!
/* ===========================================
CPT: Projects
=============================================*/
function projects_custom_init() {
$labels = array(
'name' => 'Projects',
'singular_name' => 'Project',
'add_new' => 'Add New',
'add_new_item' => 'Add New Project',
'edit_item' => 'Edit Project',
'new_item' => 'New Project',
'all_items' => 'All Projects',
'view_item' => 'View Project',
'search_items' => 'Search Projects',
'not_found' => 'No Projects found',
'not_found_in_trash' => 'No Projects found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Projects'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'projects' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'thumbnail', 'excerpt' )
);
register_post_type( 'projects', $args );
}
add_action( 'init', 'projects_custom_init' );
/* ==========================================================================
Rename "Posts" to "News"
========================================================================== */
// NOTE: This function changed the "Posts" menu label to "News". This isn't "curtial" to this site, and could go in functions.php or a plugin -- I added it here to the plugin so the client wouldn't be confused if the title sudenly changed back to "Posts".
function rsg_change_post_menu_label() {
global $menu;
global $submenu;
$menu[5][0] = 'News';
$submenu['edit.php'][5][0] = 'News';
$submenu['edit.php'][10][0] = 'Add News';
$submenu['edit.php'][15][0] = 'Categories'; // Change name for categories
$submenu['edit.php'][16][0] = 'Tags'; // Change name for tags
echo '';
}
function change_post_object_label() {
global $wp_post_types;
$labels = &$wp_post_types['post']->labels;
$labels->name = 'News';
$labels->singular_name = 'News';
$labels->add_new = 'Add News';
$labels->add_new_item = 'Add News';
$labels->edit_item = 'Edit News';
$labels->new_item = 'News';
$labels->view_item = 'View News';
$labels->search_items = 'Search News';
$labels->not_found = 'No News found';
$labels->not_found_in_trash = 'No News found in Trash';
}
add_action( 'init', 'change_post_object_label' );
add_action( 'admin_menu', 'rsg_change_post_menu_label' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment