I have been working for the last couple of days on putting together code snippets that anyone in the Genesis community can add to and edit.
Pleased to announce the launch of collaborative code snippets where anyone in the #GenesisWP community can contribute.https://t.co/W7EdIm4cNh
Register an account, log in and start adding your code collection.
— Sridhar Katakam (@srikat) April 6, 2018
Click on the image below to visit.
Behind the Scenes
Plugins
Custom Post Type UI for creation and management of wiki
CPT and associated wiki_category
and wiki_tag
custom taxonomies.
Jetpack for Markdown module.
My WP-HighlightJS plugin for code syntax highlighting with copy to clipboard button.
ACF Pro for URL and Notes custom fields.
Advanced Access Manager to create a custom “Wiki Author” role, to remove access to dashboard menu items except for Media and Wikis and to grant permissions to Wiki Authors.
FacetWP for search as you type filter and filtering by tags.
SimpleModal Login for visitors to register and log in w/o having to go to a different page.
WP Featherlight for automatically showing the full image in a lightbox when a medium sized featured image thumbnail is clicked.
functions.php
// Registers `wiki-sidebar` widget area.
genesis_register_widget_area(
array(
'id' => 'wiki-sidebar',
'name' => __( 'Wiki Sidebar', 'genesis-sample' ),
'description' => __( 'This is the wiki sidebar section.', 'genesis-sample' ),
)
);
add_action( 'genesis_after_header', 'sk_change_genesis_primary_sidebar' );
/**
* Shows Wiki sidebar in Primary Sidebar location on Wiki archive but not on single wikis.
*/
function sk_change_genesis_primary_sidebar() {
if ( 'wiki' === get_post_type() && ! is_singular( 'wiki' ) ) {
// remove Primary Sidebar from the Primary Sidebar area.
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );
// show Wiki Sidebar in Primary Sidebar area.
add_action( 'genesis_sidebar', function() {
dynamic_sidebar( 'wiki-sidebar' );
} );
}
}
// Adds Archive Settings option to Wiki CPT.
// Commented out because I added this at https://genesis.community/wp-admin/admin.php?page=cptui_manage_post_types&action=edit.
// add_post_type_support( 'wiki', 'genesis-cpt-archives-settings' );
add_filter( 'wp_nav_menu_items', 'custom_menu_extras', 10, 2 );
/**
* Filter menu items to append Logout and Login links to Prmary Nav for logged in and non logged in users respectively.
* @link http://www.billerickson.net/customizing-wordpress-menus/
*
* @param string $menu HTML string of list items.
* @param object $args Menu arguments.
*
* @return string modified menu.
*/
function custom_menu_extras( $menu, $args ) {
if ( 'primary' !== $args->theme_location ) {
return $menu;
}
if ( is_user_logged_in() ) {
$menu .= '<li class="menu-item logout"><a href="' . wp_logout_url() . '">Logout</a></li>';
} else {
$menu .= '<li class="menu-item login"><a href="/wp-login.php" class="simplemodal-login">Login</a></li>';
}
return $menu;
}
add_action( 'pre_get_posts', 'post_types_author_archives' );
/**
* Adds `wiki` Custom Post Type to Author Archives.
*/
function post_types_author_archives( $query ) {
// Adds 'wiki' post type to author archives.
if ( $query->is_author ) {
$query->set( 'post_type', array( 'wiki' ) );
}
// Remove the action after it's run.
remove_action( 'pre_get_posts', 'post_types_author_archives' );
}
add_action( 'pre_get_posts', 'sk_show_all_entries' );
/**
* Shows all posts on archives.
*
* @author Bill Erickson
* @link http://www.billerickson.net/customize-the-wordpress-query/
* @param object $query data.
*
*/
function sk_show_all_entries( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_archive() ) {
$query->set( 'posts_per_page', '-1' );
}
}
// Removes Visual tab in the WordPress editor.
add_filter( 'user_can_richedit', '__return_false' );
add_action( 'admin_init', 'sk_remove_jetpack_menu_item' );
/**
* Removes Jetpack admin menu item for non-admin users.
*/
function sk_remove_jetpack_menu_item() {
if ( class_exists( 'Jetpack' ) && ! current_user_can( 'manage_options' ) ) {
remove_menu_page( 'jetpack' );
}
}
Wiki CPT Archive: archive-wiki.php
<?php
// Removes post info.
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
add_action( 'genesis_before_loop', 'sk_check_for_terms' );
/**
* Replace the Genesis loop with a custom one.
*/
function sk_check_for_terms() {
$taxonomy_name = 'wiki_category'; // specify your custom taxonomy here.
// Retrieve the terms in the above custom taxonomy.
$tax_terms = get_terms( $taxonomy_name );
// if there's at least 1 taxonomy term, then replace the default loop with a custom one.
if ( ! empty( $tax_terms ) && ! is_wp_error( $tax_terms ) ) {
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'sk_do_loop' );
}
}
/**
* Callback to output entries of a CPT grouped by the specified taxonomy.
*/
function sk_do_loop() {
$taxonomy_name = 'wiki_category'; // specify your custom taxonomy here.
// Retrieve the terms in the above custom taxonomy.
$tax_terms = get_terms( $taxonomy_name );
echo '<div class="wiki-list">';
foreach ( $tax_terms as $tax_term ) {
echo '<div class="row">';
echo '<h2>' . $tax_term->name . '</h2>';
// printf( '<h2><a href="%s">%s</a></h2>',
// get_term_link( $tax_term, array( $taxonomy_name ) ),
// $tax_term->name
// );
$args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy_name,
'field' => 'slug',
'terms' => array( $tax_term->slug )
)
),
'posts_per_page' => 5,
);
$query = new WP_Query( $args );
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li><h5><a href="' . get_permalink() . '">'. get_the_title() .'</a></h5></li>';
}
echo '</ul>';
// Uncomment the lines of code (not comments) below to display a link to custom taxonomy archive
/* Always check if it's an error before continuing. get_term_link() can be finicky sometimes */
$tax_term_link = get_term_link( $tax_term, $taxonomy_name );
if ( is_wp_error( $tax_term_link ) ) {
continue;
}
// We successfully got a link. Print it out.
echo '<p class="more-from-link"><a href="' . $tax_term_link . '">More from ' . $tax_term->name . ' category »</a></p>';
wp_reset_query();
echo '</div>';
} // end foreach
echo '</div>';
}
genesis();
Wiki single entry pages: single-wiki.php
<?php
add_filter( 'genesis_cpt_crumb', 'sk_add_taxonomy_crumb', 10, 2 );
/**
* Adds `wiki_category` taxonomy term link in the breadcrumb.
*
* @param string $crumb HTML markup for the CPT breadcrumb.
* @param array $args Arguments used to generate the breadcrumbs.
*
* @return string HTML markup for a single custom post type entry breadcrumb, including any parent (CPT name) breadcrumbs and taxonomy term link.
*/
function sk_add_taxonomy_crumb( $crumb, $args ) {
$terms = get_the_terms( get_the_ID(), 'wiki_category' );
if ( $terms && ! is_wp_error( $terms ) ) {
// store the first term in a variable.
$term = array_shift( $terms );
$termlink = sprintf( '<span %s><a href="%s" itemprop="item"><span itemprop="name">%s</span></a></span>',
genesis_attr( 'breadcrumb-link-wrap' ),
get_term_link( $term, array( 'wiki_category') ),
$term->name
);
}
$crumb = str_replace( $args['sep'], $args['sep'] . $termlink . $args['sep'], $crumb );
return $crumb;
}
// Forces full width content.
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
// Removes post info.
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
add_action( 'genesis_entry_content', 'sk_wiki_image' );
/**
* Outputs featured image, if present.
*/
function sk_wiki_image() {
if ( ! has_post_thumbnail() ) {
return;
}
$full_image_url = genesis_get_image( 'format=url&size=full' );
$image_url = genesis_get_image( 'format=url&size=medium' );
// get the alt text of featured image.
$thumb_id = get_post_thumbnail_id( get_the_ID() );
$alt = get_post_meta( $thumb_id, '_wp_attachment_image_alt', true );
// if no alt text is present for featured image, set it to entry title.
if ( '' === $alt ) {
$alt = the_title_attribute( 'echo=0' );
}
// display the image.
printf( '<div><a href="%s"><img src="%s" class="wiki-image" alt="%s" /></a></div>',
esc_url( $full_image_url ),
esc_url( $image_url ),
$alt
);
}
add_action( 'genesis_entry_content', 'sk_wiki_meta' );
/**
* Outputs source URL and Notes custom fields.
*/
function sk_wiki_meta() {
$url = get_post_meta( get_the_ID(), 'url', true );
$notes = get_post_meta( get_the_ID(), 'notes', true );
if ( $url || $notes ) {
echo '<div class="wiki-meta">';
}
if ( $url ) {
printf( '<div class="wiki-url">Source: <a href="%s">%s</a></div>',
esc_url( $url ),
esc_url( $url )
);
}
if ( $notes ) {
printf( '<div class="wiki-notes">Notes: %s</div>',
$notes
);
}
if ( $url || $notes ) {
echo '</div>';
}
}
add_action( 'genesis_after_loop', 'sk_prev_next_wiki_nav' );
/**
* Adds post navigation.
*/
function sk_prev_next_wiki_nav() {
genesis_markup( array(
'html5' => '<div %s>',
'xhtml' => '<div class="navigation">',
'context' => 'adjacent-entry-pagination',
) );
echo '<div class="pagination-previous alignleft">';
previous_post_link( '« %link', '%title', true, '', 'wiki_category' );
echo '</div>';
echo '<div class="pagination-next alignright">';
next_post_link( '%link »', '%title', true, '', 'wiki_category' );
echo '</div>';
echo '</div>';
}
add_filter( 'genesis_post_meta', 'sk_wiki_meta_filter' );
/**
* Shows `wiki_tag` taxonomy's hyperlinked terms.
*
* @param string $post_meta Default post meta text.
*
* @return string Modified post meta text.
*/
function sk_wiki_meta_filter( $post_meta ) {
$post_meta = 'Posted by: <a href="' . get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ) . '">' . get_the_author() . '</a> ';
return $post_meta;
}
genesis();
Wiki Category and Tag term pages, and Author archives: taxonomy-wiki_category.php, taxonomy-wiki_tag.php and author.php
<?php
// Remove all actions from entry hooks.
$hooks = array(
'genesis_entry_header',
'genesis_entry_content',
'genesis_entry_footer',
);
foreach ( $hooks as $hook ) {
remove_all_actions( $hook );
}
// Adds entry title.
add_action( 'genesis_entry_header', 'genesis_do_post_title' );
genesis();
Hello Sridhar, I am using yoast for my WordPress site. I used to remove my website name from heading, it doesn’t look good to me. But in search results it is showing my website name in heading. Please suggest how to remove website name from heading.
I am not sure why you want to remove the site title from search results. I would advise against doing so.
Anyway, if you insist, try running the Yoast SEO configuration wizard and in Step 9 (Title settings), empty the Website name field.
https://d.pr/i/YPnxmf