* @copyright Copyright (c) 2008 - 2013, Justin Tadlock
* @link http://themehybrid.com/hybrid-core
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/* Register shortcodes. */
add_action( 'init', 'hybrid_add_shortcodes' );
/**
* Creates new shortcodes for use in any shortcode-ready area. This function uses the add_shortcode()
* function to register new shortcodes with WordPress.
*
* @since 0.8.0
* @access public
* @uses add_shortcode() to create new shortcodes.
* @link http://codex.wordpress.org/Shortcode_API
* @return void
*/
function hybrid_add_shortcodes() {
/* Add theme-specific shortcodes. */
add_shortcode( 'the-year', 'hybrid_the_year_shortcode' );
add_shortcode( 'site-link', 'hybrid_site_link_shortcode' );
add_shortcode( 'wp-link', 'hybrid_wp_link_shortcode' );
add_shortcode( 'theme-link', 'hybrid_theme_link_shortcode' );
add_shortcode( 'child-link', 'hybrid_child_link_shortcode' );
add_shortcode( 'loginout-link', 'hybrid_loginout_link_shortcode' );
add_shortcode( 'query-counter', 'hybrid_query_counter_shortcode' );
add_shortcode( 'nav-menu', 'hybrid_nav_menu_shortcode' );
/* Add entry-specific shortcodes. */
add_shortcode( 'entry-title', 'hybrid_entry_title_shortcode' );
add_shortcode( 'entry-author', 'hybrid_entry_author_shortcode' );
add_shortcode( 'entry-terms', 'hybrid_entry_terms_shortcode' );
add_shortcode( 'entry-comments-link', 'hybrid_entry_comments_link_shortcode' );
add_shortcode( 'entry-published', 'hybrid_entry_published_shortcode' );
add_shortcode( 'entry-edit-link', 'hybrid_entry_edit_link_shortcode' );
add_shortcode( 'entry-shortlink', 'hybrid_entry_shortlink_shortcode' );
add_shortcode( 'entry-permalink', 'hybrid_entry_permalink_shortcode' );
add_shortcode( 'post-format-link', 'hybrid_post_format_link_shortcode' );
/* Add comment-specific shortcodes. */
add_shortcode( 'comment-published', 'hybrid_comment_published_shortcode' );
add_shortcode( 'comment-author', 'hybrid_comment_author_shortcode' );
add_shortcode( 'comment-edit-link', 'hybrid_comment_edit_link_shortcode' );
add_shortcode( 'comment-reply-link', 'hybrid_comment_reply_link_shortcode' );
add_shortcode( 'comment-permalink', 'hybrid_comment_permalink_shortcode' );
}
/**
* Shortcode to display the current year.
*
* @since 0.6.0
* @access public
* @uses date() Gets the current year.
* @return string
*/
function hybrid_the_year_shortcode() {
return date( __( 'Y', 'hybrid-core' ) );
}
/**
* Shortcode to display a link back to the site.
*
* @since 0.6.0
* @access public
* @uses get_bloginfo() Gets information about the install.
* @return string
*/
function hybrid_site_link_shortcode() {
return '' . get_bloginfo( 'name' ) . '';
}
/**
* Shortcode to display a link to WordPress.org.
*
* @since 0.6.0
* @access public
* @return string
*/
function hybrid_wp_link_shortcode() {
return '' . __( 'WordPress', 'hybrid-core' ) . '';
}
/**
* Shortcode to display a link to the parent theme page.
*
* @since 0.6.0
* @access public
* @uses get_theme_data() Gets theme (parent theme) information.
* @return string
*/
function hybrid_theme_link_shortcode() {
$theme = wp_get_theme( get_template() );
return '' . esc_attr( $theme->get( 'Name' ) ) . '';
}
/**
* Shortcode to display a link to the child theme's page.
*
* @since 0.6.0
* @access public
* @uses get_theme_data() Gets theme (child theme) information.
* @return string
*/
function hybrid_child_link_shortcode() {
$theme = wp_get_theme();
return '' . esc_html( $theme->get( 'Name' ) ) . '';
}
/**
* Shortcode to display a login link or logout link.
*
* @since 0.6.0
* @access public
* @uses is_user_logged_in() Checks if the current user is logged into the site.
* @uses wp_logout_url() Creates a logout URL.
* @uses wp_login_url() Creates a login URL.
* @return string
*/
function hybrid_loginout_link_shortcode() {
if ( is_user_logged_in() )
$out = '' . __( 'Log out', 'hybrid-core' ) . '';
else
$out = '' . __( 'Log in', 'hybrid-core' ) . '';
return $out;
}
/**
* Displays query count and load time if the current user can edit themes.
*
* @since 0.6.0
* @access public
* @uses current_user_can() Checks if the current user can edit themes.
* @return string
*/
function hybrid_query_counter_shortcode() {
if ( current_user_can( 'edit_theme_options' ) )
return sprintf( __( 'This page loaded in %1$s seconds with %2$s database queries.', 'hybrid-core' ), timer_stop( 0, 3 ), get_num_queries() );
return '';
}
/**
* Displays a nav menu that has been created from the Menus screen in the admin.
*
* @since 0.8.0
* @access public
* @uses wp_nav_menu() Displays the nav menu.
* @return string
*/
function hybrid_nav_menu_shortcode( $attr ) {
$attr = shortcode_atts(
array(
'menu' => '',
'container' => 'div',
'container_id' => '',
'container_class' => 'nav-menu',
'menu_id' => '',
'menu_class' => '',
'link_before' => '',
'link_after' => '',
'before' => '',
'after' => '',
'fallback_cb' => 'wp_page_menu',
'walker' => ''
),
$attr,
'nav-menu'
);
$attr['echo'] = false;
return wp_nav_menu( $attr );
}
/**
* Displays the edit link for an individual post.
*
* @since 0.7.0
* @access public
* @param array $attr
* @return string
*/
function hybrid_entry_edit_link_shortcode( $attr ) {
$post_type = get_post_type_object( get_post_type() );
if ( !current_user_can( $post_type->cap->edit_post, get_the_ID() ) )
return '';
$attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr, 'entry-edit-link' );
return $attr['before'] . '' . __( 'Edit', 'hybrid-core' ) . '' . $attr['after'];
}
/**
* Displays the published date of an individual post.
*
* @since 0.7.0
* @access public
* @param array $attr
* @return string
*/
function hybrid_entry_published_shortcode( $attr ) {
$attr = shortcode_atts(
array(
'before' => '',
'after' => '',
'format' => get_option( 'date_format' ),
'human_time' => ''
),
$attr,
'entry-published'
);
/* If $human_time is passed in, allow for '%s ago' where '%s' is the return value of human_time_diff(). */
if ( !empty( $attr['human_time'] ) )
$time = sprintf( $attr['human_time'], human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) );
/* Else, just grab the time based on the format. */
else
$time = get_the_time( $attr['format'] );
$published = '';
return $attr['before'] . $published . $attr['after'];
}
/**
* Displays a post's number of comments wrapped in a link to the comments area.
*
* @since 0.7.0
* @access public
* @param array $attr
* @return string
*/
function hybrid_entry_comments_link_shortcode( $attr ) {
$comments_link = '';
$number = doubleval( get_comments_number() );
$attr = shortcode_atts(
array(
'zero' => __( 'Leave a response', 'hybrid-core' ),
'one' => __( '%1$s Response', 'hybrid-core' ),
'more' => __( '%1$s Responses', 'hybrid-core' ),
'css_class' => 'comments-link',
'none' => '',
'before' => '',
'after' => ''
),
$attr,
'entry-comments-link'
);
if ( 0 == $number && !comments_open() && !pings_open() ) {
if ( $attr['none'] )
$comments_link = '' . sprintf( $attr['none'], number_format_i18n( $number ) ) . '';
}
elseif ( 0 == $number )
$comments_link = '' . sprintf( $attr['zero'], number_format_i18n( $number ) ) . '';
elseif ( 1 == $number )
$comments_link = '' . sprintf( $attr['one'], number_format_i18n( $number ) ) . '';
elseif ( 1 < $number )
$comments_link = '' . sprintf( $attr['more'], number_format_i18n( $number ) ) . '';
if ( $comments_link )
$comments_link = $attr['before'] . $comments_link . $attr['after'];
return $comments_link;
}
/**
* Displays an individual post's author with a link to his or her archive.
*
* @since 0.7.0
* @access public
* @param array $attr
* @return string
*/
function hybrid_entry_author_shortcode( $attr ) {
$post_type = get_post_type();
if ( post_type_supports( $post_type, 'author' ) ) {
$attr = shortcode_atts(
array(
'before' => '',
'after' => ''
),
$attr,
'entry-author'
);
$author = '' . get_the_author_meta( 'display_name' ) . '';
return $attr['before'] . $author . $attr['after'];
}
return '';
}
/**
* Displays a list of terms for a specific taxonomy.
*
* @since 0.7.0
* @access public
* @param array $attr
* @return string
*/
function hybrid_entry_terms_shortcode( $attr ) {
$attr = shortcode_atts(
array(
'id' => get_the_ID(),
'taxonomy' => 'post_tag',
'separator' => ', ',
'before' => '',
'after' => ''
),
$attr,
'entry-terms'
);
$attr['before'] = ( empty( $attr['before'] ) ? '' : '' . $attr['before'] . '' );
$attr['after'] = ( empty( $attr['after'] ) ? '' : '' . $attr['after'] . '' );
return get_the_term_list( $attr['id'], $attr['taxonomy'], $attr['before'], $attr['separator'], $attr['after'] );
}
/**
* Displays a post's title with a link to the post.
*
* @since 0.7.0
* @access public
* @return string
*/
function hybrid_entry_title_shortcode( $attr ) {
$attr = shortcode_atts(
array(
'permalink' => true,
'tag' => is_singular() ? 'h1' : 'h2'
),
$attr,
'entry-title'
);
$tag = tag_escape( $attr['tag'] );
$class = sanitize_html_class( get_post_type() ) . '-title entry-title';
if ( false == (bool)$attr['permalink'] )
$title = the_title( "<{$tag} class='{$class}'>", "{$tag}>", false );
else
$title = the_title( "<{$tag} class='{$class}'>", "{$tag}>", false );
return $title;
}
/**
* Displays the shortlink of an individual entry.
*
* @since 0.8.0
* @access public
* @return string
*/
function hybrid_entry_shortlink_shortcode( $attr ) {
$attr = shortcode_atts(
array(
'text' => __( 'Shortlink', 'hybrid-core' ),
'title' => the_title_attribute( array( 'echo' => false ) ),
'before' => '',
'after' => ''
),
$attr,
'entry-shortlink'
);
$shortlink = esc_url( wp_get_shortlink( get_the_ID() ) );
return "{$attr['before']}{$attr['text']}{$attr['after']}";
}
/**
* Returns the output of the [entry-permalink] shortcode, which is a link back to the post permalink page.
*
* @since 1.3.0.
* @param array $attr The shortcode arguments.
* @return string A permalink back to the post.
*/
function hybrid_entry_permalink_shortcode( $attr ) {
$attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr, 'entry-permalink' );
return $attr['before'] . '' . __( 'Permalink', 'hybrid-core' ) . '' . $attr['after'];
}
/**
* Returns the output of the [post-format-link] shortcode. This shortcode is for use when a theme uses the
* post formats feature.
*
* @since 1.3.0.
* @param array $attr The shortcode arguments.
* @return string A link to the post format archive.
*/
function hybrid_post_format_link_shortcode( $attr ) {
$attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr, 'post-format-link' );
$format = get_post_format();
$url = ( empty( $format ) ? get_permalink() : get_post_format_link( $format ) );
return $attr['before'] . '' . get_post_format_string( $format ) . '' . $attr['after'];
}
/**
* Displays the published date and time of an individual comment.
*
* @since 0.7.0
* @access public
* @return string
*/
function hybrid_comment_published_shortcode( $attr ) {
$attr = shortcode_atts(
array(
'human_time' => '',
'before' => '',
'after' => '',
),
$attr,
'comment-published'
);
/* If $human_time is passed in, allow for '%s ago' where '%s' is the return value of human_time_diff(). */
if ( !empty( $attr['human_time'] ) )
$published = '';
/* Else, just return the default. */
else
$published = '' . sprintf( __( '%1$s at %2$s', 'hybrid-core' ), '', '' ) . '';
return $attr['before'] . $published . $attr['after'];
}
/**
* Displays the comment author of an individual comment.
*
* @since 0.8.0
* @access public
* @global $comment The current comment's DB object.
* @return string
*/
function hybrid_comment_author_shortcode( $attr ) {
global $comment;
$attr = shortcode_atts(
array(
'before' => '',
'after' => '',
'tag' => 'span' // @deprecated 1.2.0 Back-compatibility. Please don't use this argument.
),
$attr,
'comment-author'
);
$author = esc_html( get_comment_author( $comment->comment_ID ) );
$url = esc_url( get_comment_author_url( $comment->comment_ID ) );
/* Display link and cite if URL is set. Also, properly cites trackbacks/pingbacks. */
if ( $url )
$output = '' . $author . '';
else
$output = '' . $author . '';
$output = '<' . tag_escape( $attr['tag'] ) . ' class="comment-author vcard">' . $attr['before'] . apply_filters( 'get_comment_author_link', $output ) . $attr['after'] . '' . tag_escape( $attr['tag'] ) . '>';
return $output;
}
/**
* Displays the permalink to an individual comment.
*
* @since 0.7.0
* @access public
* @return string
*/
function hybrid_comment_permalink_shortcode( $attr ) {
global $comment;
$attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr, 'comment-permalink' );
$link = '' . __( 'Permalink', 'hybrid-core' ) . '';
return $attr['before'] . $link . $attr['after'];
}
/**
* Displays a comment's edit link to users that have the capability to edit the comment.
*
* @since 0.7.0
* @access public
* @return string
*/
function hybrid_comment_edit_link_shortcode( $attr ) {
global $comment;
$edit_link = get_edit_comment_link( $comment->comment_ID );
if ( !$edit_link )
return '';
$attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr, 'comment-edit-link' );
$link = '';
$link = apply_filters( 'edit_comment_link', $link, $comment->comment_ID );
return $attr['before'] . $link . $attr['after'];
}
/**
* Displays a reply link for the 'comment' comment_type if threaded comments are enabled.
*
* @since 0.7.0
* @access public
* @return string
*/
function hybrid_comment_reply_link_shortcode( $attr ) {
if ( !get_option( 'thread_comments' ) || 'comment' !== get_comment_type() )
return '';
$defaults = array(
'reply_text' => __( 'Reply', 'hybrid-core' ),
'login_text' => __( 'Log in to reply.', 'hybrid-core' ),
'depth' => intval( $GLOBALS['comment_depth'] ),
'max_depth' => get_option( 'thread_comments_depth' ),
'before' => '',
'after' => ''
);
$attr = shortcode_atts( $defaults, $attr, 'comment-reply-link' );
return get_comment_reply_link( $attr );
}
?>