Conditionally disable Jetpack’s Infinite Scroll

So you implemented Jetpack’s Infinite Scroll feature in your theme, but you don’t want it in a particular page, say a category archive page. Here’s the code you need to add to your theme’s functions.php file:

/**
 * Disable Jetpack's Infinite Scroll Conditionally
 *
 */
function _kucrut_disable_jetpack_infinite_scroll_conditionally() {
	if ( true === my_conditionals() ) {
		remove_theme_support( 'infinite-scroll' );
	}
}
add_action( 'template_redirect', '_kucrut_disable_jetpack_infinite_scroll_conditionally', 9 );

Just remember to change my_conditionals() with your actual conditionals and to never add the action callback after priority 9, otherwise it will be too late ūüėČ

Forward requests to another host with iptables

Like the cool kids in the block, I too am using Vagrant in my web development workflow. Everything is fine until I need to test the sites with real mobile browsers. Since the sites I’m working on are hosted inside a virtual machine, it’s not accessible from outside of the host machine.

Here’s how we can solve this using iptables. Let’s assume that the host machine’s IP is 192.168.1.2 and the guest/virtual machine’s IP is 10.86.73.80. These commands are to be run in the host machine as root:

iptables -F
iptables -t nat -F
iptables -X
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 10.86.73.80:80
iptables -A FORWARD -d 10.86.73.80 -p tcp --dport 80 -j ACCEPT
iptables -t nat -A POSTROUTING -j MASQUERADE

Now, add the site’s domain to your rooted phone’s /etc/hosts file or your router’s configs:

192.168.1.2  mylocalsite.dev

You can save the rules above permanently with:

iptables-save > /etc/iptables/iptables.rules

Set attachment’s default “Link To” value

attachment-display-settings When inserting an attachment into a WordPress post, we can customize how it should be displayed. There are three settings; Alignment, Link To and Size. Here’s how we can tell WordPress to select None as the default value of Link To:

 * Set default media link to 'none'
 *
 * @param   string $value Option value
 * @wp_hook filter pre_option_image_default_link_type
 * @return  string
 */
function _kucrut_image_default_link_type( $value ) {
    return 'none';
}
add_filter( 'pre_option_image_default_link_type', '_kucrut_image_default_link_type' );

As of WordPress 4.0, there are four values available: file (default), post, custom and none.

Get ACF Repeater field value

So you’re using ACF‘s repeater field to store your data and are wondering if there’s an easier way to get its value? Here’s how:

/**
 * Get ACF repeater field value
 *
 * @param int|object $post          Post ID/object
 * @param string     $meta_key      Meta key
 * @param array      $subfield_keys Subfield keys
 *
 * return mixed
 */
function kucrut_get_acf_repeater_field_value( $post, $meta_key, Array $subfield_keys ) {
    $post = get_post( $post );
    if ( ! ( $post instanceof WP_Post ) ) {
        return false;
    }

    $count = absint( get_post_meta( $post->ID, $meta_key, true ) );
    if ( empty( $count ) ) {
        return false;
    }

    $values = array();
    for ( $i = 0; $i < $count; $i++ ) {
        $item = array();
        foreach ( $subfield_keys as $sub_key ) {
            $item[ $sub_key ] = get_post_meta(
                $post->ID,
                sprintf( '%s_%d_%s', $meta_key, $i, $sub_key ),
                true
            );
        }
        $values[] = $item;
    }

    return $values;
}