Sort by: Recently Added Likes

Redirect After Login

@replace {{link}}

// Redirect a user after successful login
add_filter( 'login_redirect', function () {
  return '{{link}}';
} );

#

Customize Recovery Mode Email Recipient

@replace {{email}}

// Customize the email address to which recovery mode email is sent when site encounters a fatal error
add_filter( 'recovery_mode_email', function( $email ) {

    $email['to'] = '{{email}}';

    return $email;

} );

#

Add CPT(s) to Main Blog Feed

@replace {{cpt-slug}}

add_action( 'pre_get_posts', function ( $query ) {

   if ( is_home() && $query->is_main_query() )
      $query->set( 'post_type', array( 'post', '{{cpt-slug}}' ) );

   return $query;

} );

Source

Sync Roles Across Multisite Network

@requires @plugin Members

// Sync roles across multisite network by hooking to Members plugin (https://wordpress.org/plugins/members/)
add_action( 'members_role_updated', function() {

   if ( function_exists( 'get_sites' ) && class_exists( 'WP_Site_Query' ) ) {

      $roles = get_option( 'wp_user_roles' );
      $sites = get_sites( array( 'fields' => 'ids' ) );

      foreach ( $sites as $site_id ) {
         update_blog_option( $site_id, 'wp_' . $site_id . '_user_roles', $roles );
      }

   }

} );

#

Remove Welcome Panel

// Remove the welcome panel for new WordPress installs
remove_action( 'welcome_panel', 'wp_welcome_panel' );

#

Disallow Specific Terms in WordPress Search

@replace @arr $restricted

// Disallow users from searching specific terms on your site
add_filter( 'pre_get_posts', function( $query ) {

   $restricted = array(
      'term-1',
      'term-2'
   );

   if ( $query->is_search && $query->is_main_query() ) {

      $stripped = trim( str_replace( $restricted, '', $query->get('s') ) );

      if ( $stripped == '' )
         $query->set( 'p', -1 );
      else
         $query->set( 's', $stripped );

   }

} );

Source

Advanced Forms for Your WordPress-Powered Website.

Remove Comment Form URL Field

// Remove the URL field from the WordPress comment form
add_filter( 'comment_form_default_fields', function ( $fields ) {

   unset( $fields['url'] );

   return $fields;

} );

Source

Perform Function On Any Site of Multisite Network

/**
 *  Perform any function on any site on the network
 *
 *  @param    integer         $site_id   site on which to perform the function
 *  @param    string|array    $action    function name to perform
 *  @param    array           $args      parameters passed as an indexed array
 *  @return
 */
function do_network_request( $site_id, $action, $args = array() ) {

   switch_to_blog( $site_id );

      if ( ( is_string( $action ) && function_exists( $action ) ) || ( is_array( $action ) && method_exists( ...$action ) ) )
         $result = call_user_func( $action, ...$args );
      else
         $result = NULL;

   restore_current_blog();

   return $result;

} // end do_network_request

#

Redirect Solitary Search Results to the Post

// If a search results returns only a single post, then redirect the user to that post
add_action( 'template_redirect', function () {

   if ( is_search() ) {

      global $wp_query;

      if ( $wp_query->post_count == 1 && $wp_query->max_num_pages == 1 ) {

         wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
         exit;

      }

   }

} );

Source

Add CSS Class to Filled Fields

@replace {{class}}

<?php
// Checks whether form inputs are filled and adds a targetable class if they have value
add_action( 'wp_footer', function () { ?>

   <script type="text/javascript">
      jQuery(document).ready( function() {
         function checkFilledInputs() {
            jQuery('textarea,input').each( function() { toggleFilledInputs(this); });
            jQuery('textarea,input').change( function() { toggleFilledInputs(this); });
         }

         function toggleFilledInputs(e) {
            if ( jQuery(e).val().trim().length > 0 ) {
               jQuery(e).addClass('{{class}}');
            } else {
               jQuery(e).removeClass('{{class}}');
            }
         }

         jQuery(document).bind('gform_post_render', function() { checkFilledInputs(); });
         
         checkFilledInputs();
      });
   </script>

<?php } );

#

Auto-Link Twitter Usernames

// Automatically link Twitter usernames (i.e. any text in the form of @username)
add_filter( 'the_content', 'typewheel_link_twitter_username' );   
add_filter( 'the_excerpt', 'typewheel_link_twitter_username' );
add_filter( 'comment_text', 'typewheel_link_twitter_username' );

function typewheel_link_twitter_username( $content ) {
   return preg_replace( '/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/', '$1<a href="https://twitter.com/$2" target="_blank" rel="nofollow">@$2</a>', $content );
}
 

Source