Skip to content

Instantly share code, notes, and snippets.

@rahilwazir
Last active July 27, 2016 09:34
Show Gist options
  • Save rahilwazir/60dc4cc40bb50b98fb02eeb600e3db09 to your computer and use it in GitHub Desktop.
Save rahilwazir/60dc4cc40bb50b98fb02eeb600e3db09 to your computer and use it in GitHub Desktop.
Code that breaks Algolia WP Tasks functionality

I have ACF fields on my post/page screens which add advance content fields, so it updates the post_content field with correct html formatting.

If you remove the first $post_type === ... conditional check, it would cause Algolia to not update it's task content which is obvious that this hook will run for every post type

functions.php

<?php
function my_update_body($post_id) {
    global $post_type;
    // remove this check to make Algolia sad :(
    if ($post_type !== 'post' || $post_type !== 'page') {
        return;
    }

    $_post_id = is_object($post_id) ? $post_id->ID : $post_id;

    // disable to avoid infinite loops
    remove_action('save_post', 'my_update_body');
    remove_action('pre_post_update', 'my_update_body');
    remove_action('edit_page_form', 'my_update_body');

    $new_post = array();
    $new_post['ID'] = $_post_id;
    // get_flexible_content here wraps the formatted html of each acf field content
    $new_post['post_content'] = get_flexible_content($_post_id);

    wp_update_post($new_post);

    // re-enable
    add_action('save_post', 'my_update_body');
    add_action('pre_post_update', 'my_update_body');
    add_action('edit_page_form', 'my_update_body');
}

add_action('save_post', 'my_update_body');
add_action('pre_post_update', 'my_update_body');
add_action('edit_page_form', 'my_update_body');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment