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

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

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

Create fields once. Use everywhere.

Create Globally Accessible Counter in Queries

/**
 * Create a globally accessible counter for all queries
 * Even custom new WP_Query!
 */

// Initialize your variables
add_action( 'init', function () {
   global $cqc;
   $cqc = -1;
} );

// At loop start, always make sure the counter is -1
// This is because WP_Query calls "next_post" for each post,
// even for the first one, which increments by 1
// (meaning the first post is going to be 0 as expected)
add_action( 'loop_start', function ( $q ) {
   global $cqc;
   $cqc = -1;
}, 100, 1 );

// At each iteration of a loop, this hook is called
// We store the current instance's counter in our global variable
add_action( 'the_post', function ( $p, $q ) {
   global $cqc;
   $cqc = $q->current_post;
}, 100, 2 );

// At each end of the query, we clean up by setting the counter to
// the global query's counter. This allows the custom $cqc variable
// to be set correctly in the main page, post or query, even after
// having executed a custom WP_Query.
add_action( 'loop_end', function ( $q ) {
   global $wp_query, $cqc;
   $cqc = $wp_query->current_post;
}, 100, 1 );

Source