Skip to content
jocastaneda edited this page May 9, 2016 · 2 revisions

Carelib Framework

The carelib library is a great way to create and extend your themes. By adding this library you can create valid HTML markup and is also microdata friendly.

Installation

Using the carelib library is as easy as including one line of code in your theme's functions.php file.

require_once( trailingslashit( get_template_directory() ) . 'carelib.php' );

Getting Started

The library provides many useful functions, actions, and filters to help extend your theme. The following will guide you through some basic and advanced features the library can offer. There are two naming conventions you will see in the following. The use of prefix and the use of {var} within hooks, filters, and functions. The prefix will always refer to the global variable carelib_prefix and {var} will generally refer to WordPress core usage. For example, body classes are extended and use a naming convention of y{int} where {int} is going to be an integer.

If there is no carelib_prefix set by either a theme or a plugin, the library will use carelib.

Required files

  • style.css
  • index.php ( For Wordpress compatibility )
  • functions.php ( load our library )
  • templates/framework.php ( the fallback template )

Template functions

Carelib has many functions available to aide in theme building as well. Those functions are:

Global

  • carelib_get_site_title
  • carelib_get_site_description
  • carelib_get_skip_link
  • carelib_get_avatar
  • carelib_get_the_widget
  • carelib_get_customizer_link
  • carelib_get_credit_link
  • carelib_get_theme_info
  • carelib_get_site_link
  • carelib_get_wp_link

Entry

  • carelib_get_entry_title
  • carelib_get_entry_published
  • carelib_get_entry_comments_link
  • carelib_get_the_author_posts_link
  • carelib_get_entry_author
  • carelib_get_exceprt_more
  • carelib_get_content
  • carelib_get_content_url

404

  • carelib_get_404_entry_title
  • carelib_get_404_content

Archive

  • carelib_get_single_archive_title
  • carelib_get_single_author_title
  • carelib_get_single_year_title
  • carelib_get_single_week_title
  • carelib_get_single_day_title
  • carelib_get_single_hour_title
  • carelib_get_single_minute_title

Comments

  • carelib_get_comment_author_link
  • carelib_get_comment_author_url_link

Form

  • carelib_search_form_get_form

CSS Classes

Body

  • Date based y{int} | m{int} | d{int} | h{int}
  • language ( ltr|rtl )
  • locale - if not same as language
  • parent-theme or child-theme
  • multisite and blog-{id}
  • logged-in or logged-out
  • custom-header
  • display-header-text
  • plural
  • {post_type}-template-{id}
  • {post_type}-template-{template}
  • attachment-{type}
  • paged-{paged}
  • layout-{name}

Post

  • author-{nicename}
  • author-{author-id}
  • protected
  • has-excerpt
  • has-more-link
  • has-pages

Comment

  • ping
  • role-{role}
  • has-avatar

Available actions

  • prefix_archive_header
  • prefix_dashboard_content
  • prefix_dashboard_menu_items
  • prefix_dashboard_notices
  • prefix_dashboard_sidebar
  • prefix_dashboard_top
  • prefix_entry_footer
  • prefix_entry_footer_meta
  • prefix_entry_header
  • prefix_entry_header_meta
  • prefix_framework
  • prefix_get_template_part_{slug}
  • prefix_register_layouts

Available Filters

  • carelib_uri_path
  • context
  • get_avatar_comment_types
  • prefix_404_content
  • prefix_404_entry_title
  • prefix_allow_layout_control
  • prefix_attachment_image_gallery
  • prefix_attr_{slug}
  • prefix_breadcrumbs_plugins
  • prefix_breadcrumb_options
  • prefix_breadcrumb_tags
  • prefix_carelib_attachment_image
  • prefix_child_textdomain
  • prefix_comment_template_hierarchy
  • prefix_credit_link
  • prefix_display_comments
  • prefix_enable_suffix
  • prefix_entry_author
  • prefix_entry_author_defaults
  • prefix_entry_comments_link
  • prefix_entry_comments_link_defaults
  • prefix_entry_published
  • prefix_entry_published_defaults
  • prefix_entry_terms
  • prefix_entry_title
  • prefix_entry_title_defaults
  • prefix_forced_post_ids
  • prefix_forced_post_types
  • prefix_forced_templates
  • prefix_get_theme_layout
  • prefix_has_entry_footer
  • prefix_has_entry_footer_meta
  • prefix_has_entry_header
  • prefix_has_entry_header_meta
  • prefix_image
  • prefix_image_defaults
  • prefix_index_template
  • prefix_is_full_content
  • prefix_layout_args
  • prefix_layout_meta_key
  • prefix_parent_textdomain
  • prefix_post_navigation_defaults
  • prefix_primary_sidebar_id
  • prefix_read_more_text
  • prefix_search_button_label
  • prefix_search_button_text
  • prefix_search_form_label
  • prefix_search_text
  • prefix_sidebar_args
  • prefix_sidebar_defaults
  • prefix_site_description
  • prefix_site_description_defaults
  • prefix_site_title
  • prefix_site_title_defaults
  • prefix_skip_link
  • prefix_skip_link_defaults
  • prefix_style_meta_key
  • prefix_theme_info
  • prefix_tiny_mce_formats

Usage

As you can see carelib provides many template tags, actions and several filters. The following will demonstrate not only usage but some intermediate usage that can be extended by the theme, child theme or even a plugin.

Registering Layouts

You can register one template at a time by using carelib_register_layout() and passing it a name and an associative array. This is done by creating your action hook. Carelib creates the hook prefix_register_layouts. You are also able to register your layouts on your after_setup_theme callback function. Be sure to set one layout as the default.

carelib_register_layout( 'sample-layout', array(
	'label'            => __( 'the label', 'domain' ), // required
	'image'            => '%s/path/to/image', // required
	'default'          => false, // required
	'is_global_layout' => false, // optional
	'is_post_layout'   => false, // optional
	'is_user_layout'   => false, // optional
	'post_types'       => array(), // optional
	) );

Or you can register multiple layouts using carelib_register_layouts() and using an associative array with the keys as the names.

$layouts = array(
	'layout-1' => array(
		'label => __( 'Layout 1' ),
		'image'   => '%s/path/to/image',
		'default' => true,
		),
	'layout-2' => array(
		'label'          => __( 'Layout 2' ),
		'image'          => '%s/path/to/image',
		'is_post_layout' => true
		)
	);
carelib_register_layouts( $array );

Using filters

Carelib provides many filters from using minified scripts and styles to changing specific classes in HTML elements. For example, if you wanted to change the available breadcrumb your theme provides support for you could use the following:

add_filter( 'prefix_breadcrumbs_plugins', 'your_new_list' );
function your_new_list ( $array ) {
	return array( 'bread-navtx', 'breadcrumb_trail', 'another_options', 'last_possible_option' );
}

Using actions

Carelib also provides some action hooks. For example using the carelib_get_template_part hook. In the following example, we create an array, and load a top menu using carelib_get_template_part. In our framework.php file we could have:

// Create an array of data, pass that to our menu template part
$data = array( '0' => 'key one', '1' => 'key two' );
carelib_get_template_part( $data, 'top', 'menu' );

From there if a child theme, or plugin wanted to add more, remove, or even execute further functions, they could hook to that like the following:

add_action( 'prefix_get_template_part_top', 'your_action_here', 10, 3 );
function your_action_here ( $data, $slug, $name ) {
	if ( $data[0] == 'key one' ) {
		printf( '<p>%s</p>', __( 'Wow! I am from the $data passed' ) );
	}
	if ( $slug == 'top' ) {
		printf( '<p>%s</p>', __( 'Wow! This was from the $slug' ) );
	}
	if ( $name == 'menu' ) {
		printf( '<p>%s</p>', __( 'Wow! I am from $name' ) );
	}
}