Code Snippets

Here are various Beans code snippets that I find useful. Place these into the child theme functions file.

 

// Remove site header - https://community.getbeans.io/discussion/remove-header-from-cpt/
beans_remove_action( 'beans_header_partial_template' ); 

//Remove site title
 beans_remove_action( 'beans_site_branding' );

//Remove title tagline 
beans_remove_action( 'beans_site_title_tag' );

 

// Remove beans editor styling
beans_remove_action( 'beans_add_editor_assets' );

 

// Remove breadcrumbs.
beans_remove_action( 'beans_breadcrumb' );

 

// Include when using style.css. Added filemtime to right away force CSS changes.
// Version CSS file in a theme
 wp_enqueue_style( 'theme-styles', get_stylesheet_directory_uri() . '/style.css', array(), filemtime( get_stylesheet_directory() . '/style.css' ) );

 

/* ----- Remove all page titles, but do not remove post titles.
 https://community.getbeans.io/discussion/remov-title-and-change-archive-h1/ ----*/
 add_action( 'wp', 'setup_document_remove_pagetitle' );
 function setup_document_remove_pagetitle() {
 if ( false === is_single() and !is_home() ) {
 beans_remove_action( 'beans_post_title' );
 }
 }


/* An alternative approach to removing page titles suggested by Tonya: https://community.getbeans.io/discussion/remove-page-titles-but-not-post-titles/ */
beans_add_smart_action('beans_before_posts_loop', 'remove_page_title_on_pages_only' );
function remove_page_title_on_pages_only() {
  if ( is_singular() && is_page() ) {
   remove_all_actions( 'beans_post_header' );
  }
}

 

// Removes featured image from all pages (posts and pages) except the blog page.
add_action( 'wp', 'beans_child_setup_document' );
function beans_child_setup_document() {
 if ( is_single() or is_page() ) { 
 beans_remove_action( 'beans_post_image' );
 }
}

 

/* Resize featured images seen on the blog page: 
https://community.getbeans.io/discussion/default-featured-image-size/ --- */
add_filter( 'beans_edit_post_image_args', 'example_post_image_edit_args' );
function example_post_image_edit_args( $args ) {
 return array_merge( $args, array(
 'resize' => array( 300, true ),
 ) );
}

 

// Blog page Excerpt - 
//THE FOLLOWING CODE needs to be added into an index.php file to work correctly.
//
// https://community.getbeans.io/discussion/how-to-show-post-excerpts/#post-4764
add_filter( 'the_content', 'beans_child_modify_post_content' );
function beans_child_modify_post_content( $content ) {
// Stop here if we are on a single view.
if ( is_singular() )
return $content;
}

// Return the excerpt() if it exists other truncate.
if ( has_excerpt() )
$content = '<p>' . get_the_excerpt() . '</p>';
else
$content = '<p>' . wp_trim_words( get_the_content(), 40, '...' ) . '</p>';

// Return content and readmore.
return $content . '<p>' . beans_post_more_link() . '</p>';
}

I commented out if ( is_singular() || is_admin() ) { as it gave me auto save 
problems in Gutenberg. 
if (!is_archive() ) { or if ( is_singular() ) will hopefully work instead.

 

/* Modify Continue reading text link to Read more.
https://community.getbeans.io/discussion/modify-wordpress-language/ */
add_filter( 'beans_post_more_link_text_output', 'example_modify_read_more' );
function example_modify_read_more() {
 return 'Read more..';
}

 

/* Modify >> to ... (dots).
https://community.getbeans.io/discussion/modify-wordpress-language/ */
beans_remove_markup('beans_next_icon[_more_link]');
beans_add_filter('beans_post_more_link_text_output', 'add_dots_after_continue_reading' );

function add_dots_after_continue_reading($continue_reading_text) {
   return $continue_reading_text . '...';
}

 

An alternative to the above code for adjusting the >> to …

// https://github.com/ThemeButler/tbr-totem/blob/master/functions.php and https://getuikit.com/docs/icon
beans_replace_attribute( 'beans_next_icon_more_link', 'class' , 'angle-double-right', 'long-arrow-right' );

 

 

// Select how many post previews are displayed per blog page.
function digwp_filter_pre_get_posts($query) {
if (is_home() && $query->is_main_query()) {
$query->set('posts_per_page', '6');
}
}
add_action('pre_get_posts', 'digwp_filter_pre_get_posts');

 

// Disable comments on posts.
// https://www.getbeans.io/code-snippets/disable-comments-for-specific-post-types/
// https://community.getbeans.io/discussion/3-ways-to-remove-page-titles-but-not-post-titles/
add_action( 'init', 'remove_post_comments' );
function remove_post_comments() {
 remove_post_type_support( 'post', 'comments' );
}

 

// Example code to remove or add fields. Number is for sorting/priority.
add_filter( 'beans_post_meta_items', 'beans_child_remove_post_meta_items' );
function beans_child_remove_post_meta_items( $items ) {

// Remove
//   unset( $items['author'] );
//   unset( $items['comments'] );
// Add 
     $items['date'] = 10;
     $items['author'] = 20;
     $items['categories'] = 30;
     $items['tags'] = 40;
     $items['comments'] = 50;  
 return $items;
}

// Remove the post meta categories below the content.
beans_remove_action( 'beans_post_meta_categories' );

// Remove the post meta tags below the content.
beans_remove_action( 'beans_post_meta_tags' );

// Removing prefixes for date, author, categories and tags.
beans_remove_output( 'beans_post_meta_date_prefix' );
beans_remove_output( 'beans_post_meta_author_prefix' );
beans_remove_output( 'beans_post_meta_categories_prefix' );
beans_remove_output( 'beans_post_meta_tags_prefix' );

 

// Adds the class post_category to style the meta area after the content. 
beans_add_attribute( 'beans_post_meta_categories', 'class', 'post_category' );

// Changes the default button color from .uk-button-primary to .uk.button-sucess: https://getuikit.com/v2/docs/button.html
beans_add_attribute( 'beans_comment_form_submit', 'class', 'uk-button-success' );

// Adds the class header-full to give a full width to the header
beans_add_attribute( 'beans_fixed_wrap[_header]', 'class', 'header-full' );

 

// Remove post archive title.
beans_remove_action( 'beans_post_archive_title' );

 

/* -------- Add icons --------*/

//Enqueue the Dashicons script
add_action( 'wp_enqueue_scripts', 'load_dashicons_front_end' );
function load_dashicons_front_end() {
wp_enqueue_style( 'dashicons' );
}
//Enqueue Font Awesome
add_action( 'wp_enqueue_scripts', 'enqueue_font_awesome' );
function enqueue_font_awesome() {
wp_enqueue_style( 'font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css' );
}

 

//Enable a sticky header
add_action( 'beans_before_load_document', 'add_class_sticky_beans_header' );
function add_class_sticky_beans_header() {
beans_add_attribute( 'beans_header', 'data-uk-sticky', 'top:-100' );
}
//Enqueue custom assets - One needs to get the UiKit sticky component.
add_action( 'beans_uikit_enqueue_scripts', 'introduce_uikit_library_sticky' );
function introduce_uikit_library_sticky() {
beans_uikit_enqueue_components( array( 'sticky' ), 'add-ons' );
}
/* Example CSS for the sticky header */
header.uk-active {
background-color: #fff;
padding-top: 20px;
padding-bottom: 20px;
}

 

/* This sticky footer does not fully work. */

html, body {
    /* Sticky Footer */
    margin:0;
    padding:0;
    height: 95%; /* Adjust the height to fit with the bottom. */
    line-height: 1.4em;
}

/* Full area similar to body */
.tm-site {
    font-size: 16px; 
    /* Sticky Footer */
    min-height: 100%; 
    position: relative; 
}

/* Behind content and sidebar */
.tm-main {
    background-color: #e7e4e4;
    /* Sticky footer */
    padding-top: 20px;
    padding-bottom: 80px; 
}

.tm-footer {
    /* Sticky Footer */
    position: absolute;
    bottom: 0;
    width: 100%; 
}

 

// Adding a site logo through code instead of the customizer:
// set logo programmatically
add_action( 'after_setup_theme', 'mytheme_set_logo' );
function mytheme_set_logo() {
set_theme_mod('beans_logo_image', get_stylesheet_directory_uri() . '/assets/images/logo.png');
}

 

Additional code snippets will be added along the way.

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *

Comment *
Name *
Email *
Website