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

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

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

#

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 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

Auto-Link Caption to Same URL As the Image’s Link

Add link-the-caption as Additional CSS Class on image block. Caption should contain no other links.

// Auto-link image caption to the same URL as that to which the image has been linked
jQuery(document).ready(function( $ ) {

   $('.link-the-caption').each( function() {

      let image_link = $(this).attr('href');
      let image_caption = $(this).next().html();

      $(this).next().html('<a href="'+image_link+'">'+image_caption+'</a>');

   });

});

#

The smarter way to manage your WordPress sites.

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 );
      }

   }

} );

#