@replace {{link}}
// Redirect a user after successful login
add_filter( 'login_redirect', function () {
return '{{link}}';
} );
@replace {{link}}
// Redirect a user after successful login
add_filter( 'login_redirect', function () {
return '{{link}}';
} );
@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;
} );
@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;
} );
/**
* 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 );
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>');
});
});
@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 the welcome panel for new WordPress installs
remove_action( 'welcome_panel', 'wp_welcome_panel' );
@replace @int {{seconds}}
add_filter( 'auth_cookie_expiration', function ( $expiration, $user_id, $remember ) {
if ( $remember )
return {{seconds}};
return $expiration;
}, 10, 3 );
@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 );
}
} );
// Remove the URL field from the WordPress comment form
add_filter( 'comment_form_default_fields', function ( $fields ) {
unset( $fields['url'] );
return $fields;
} );
/**
* 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
// 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;
}
}
} );
// Prevent auto installation of newly bundled WP themes & plugins
define('CORE_UPGRADE_SKIP_NEW_BUNDLED', true);
@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 } );