website design gold coast
website design gold coast

Code Snippets

Some handy code snippets for your functions.php

functions.php code snippets

 

Fix HTTPS links generated from wp_get_attachment_url – Source


function fix_ssl_attachment_url( $url ) {

  if ( is_ssl() )
    $url = str_replace( 'http://', 'https://', $url );
  return $url;
}
add_filter( 'wp_get_attachment_url', 'fix_ssl_attachment_url' );

Disable Pingbacks – Source


add_filter( ‘xmlrpc_methods’, function( $methods ) {
   unset( $methods['pingback.ping'] );
   return $methods;
} );

Maintenance mode without plug-in


function maintenace_mode() {
    if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {wp_die('Maintenance.');}
}
add_action('get_header', 'maintenace_mode');

Using shortcodes to show members-only content – source


add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
  if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
  return $content;
  return '';
}


add_shortcode( 'member', 'member_check_shortcode' );
function member_check_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
  return $content;
  return '';
}

Search only posts


// Search only posts
function SearchFilter($query) {

  if ($query->is_search) {
      $query->set('post_type', 'post');
  }
  
  return $query;

}
add_filter('pre_get_posts','SearchFilter');

Adjust jpeg quality – source


function gpp_jpeg_quality_callback($arg) {

    return (int)100; // change 100 to whatever you prefer, but don't go below 60

}
add_filter('jpeg_quality', 'gpp_jpeg_quality_callback');

Add an excerpt box for pages


//Add an excerpt box for pages
if ( function_exists('add_post_type_support') ){

  add_action('init', 'add_page_excerpts');
  
  function add_page_excerpts(){
  
    add_post_type_support( 'page', 'excerpt' );

  }
}

Customise your admin footer


//change admin footer
function remove_footer_admin () {

  echo 'Fueled by <a href="http://www.wordpress.org" target="_blank">WordPress</a> | Designed by <a href="https://thriveweb.com.au/" target="_blank">thrive</a>';

}
add_filter('admin_footer_text', 'remove_footer_admin');

Define your own login logo – source



//Define your own login logo
function my_custom_login_logo() {

  echo 'h1 a { background-image:url('.get_bloginfo('template_directory').'/images/logo.png) !important;}';

}

add_action('login_head', 'my_custom_login_logo');
add_filter( 'login_headerurl', 'w4_login_headerurl');

function w4_login_headerurl(){

  return home_url('/');

}

Hide the Themes menu item.



//Remove theme menus
add_action('admin_init', 'remove_theme_menus');

function remove_theme_menus() {

  global $submenu;
  
  unset($submenu['themes.php'][5]);
  
  unset($submenu['themes.php'][15]);

}

Add a favicon to your admin area


// add a favicon for your admin
function admin_favicon() {

  echo '<link href="'.get_bloginfo('stylesheet_directory').'/favicon.png" rel="Shortcut" type="image/x-icon" />';

}

add_action('admin_head', 'admin_favicon');

Remove unused menu items – source


function remove_menus () {

  global $menu;
  
  $restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));
  
  end ($menu);
  
  while (prev($menu)){
  
    $value = explode(' ',$menu[key($menu)][0]);
    
    if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
    
  }

}

dd_action('admin_menu', 'remove_menus');

Remove admin tool bar


function my_function_admin_bar(){
  return 0;
}
add_filter( 'show_admin_bar' , 'my_function_admin_bar');

Limit the max upload size – source


add_filter('wp_handle_upload_prefilter', function($file) {
  
  $size = $file['size'];
  
  if ($size > 500 * 1024) {
  
  $file['error'] = '"Files larger than X bytes are prevented from uploads.';
  
  }
  
  return $file;

});

Let's work together!

Get in touch Request a quote

Sharing is caring:

Created Nov 9, 2010 - Last modified on August 5th, 2020 - Web Design Gold Coast 2 Responses

2 Comments

  1. Warford Designs

    Thanks for the great read, Glad to see all of these culminated on one post. great reference, Will definitely utilize on my client sites.

  2. cccam fiables y seguras

    Thanks mate, even when that’s not difficult I really forget how to put the codes very often and I make silly mistakes many times so thank god you helped me with this article!

Leave a Reply