Skip to content

Instantly share code, notes, and snippets.

@gatespace
Last active January 14, 2022 08:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gatespace/15479631f6b1051c2637d1f6522b3373 to your computer and use it in GitHub Desktop.
Save gatespace/15479631f6b1051c2637d1f6522b3373 to your computer and use it in GitHub Desktop.
WordPress + Algoliaで、Algoliaの検索対象から特定の投稿(固定ページ)を除外するプラグイン
<?php
/*
Plugin Name: Exclude Algolia search
Plugin URI:
Description: Algolia の検索結果から除外するフラグを追加。WP Search with Algolia プラグインが必要。
Version: 0.1
Author: gatespace
Author URI: https://gatespace.jp
License: GPLv2 or later
*/
//Register Meta box
function exclude_algolia_search_meta_boxes() {
$args = array(
'public' => true,
);
$my_screens = get_post_types( $args );
foreach ( $my_screens as $screen ) {
add_meta_box( 'exclude_algolia_search', 'Exclude Algolia search', 'exclude_algolia_search_cb', $screen, 'side' );
}
}
add_action( 'add_meta_boxes', 'exclude_algolia_search_meta_boxes', 10, 1 );
//Meta callback function
function exclude_algolia_search_cb( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'exclude_algolia_search_box', 'exclude_algolia_search_box_nonce' );
// Use get_post_meta to retrieve an existing value from the database.
$value = get_post_meta( $post->ID, 'exclude_algolia_search_flag', true );
// Display the form, using the current value.
?>
<p><label><input type="checkbox" name="exclude_algolia_search_flag" value="1" <?php checked( $value, 1 ); ?> >
Exclude Algolia search</label></p>
<?php
}
//save meta value with save post hook
function exclude_algolia_save_post( $post_id ) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['exclude_algolia_search_box_nonce'] ) ) {
return $post_id;
}
// Verify that the nonce is valid.
$nonce = $_POST['exclude_algolia_search_box_nonce'];
if ( ! wp_verify_nonce( $nonce, 'exclude_algolia_search_box' ) ) {
return $post_id;
}
/*
* If this is an autosave, our form has not been submitted,
* so we don't want to do anything.
*/
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
/* OK, it's safe for us to save the data now. */
$value = isset($_POST['exclude_algolia_search_flag']) ? $_POST['exclude_algolia_search_flag'] : '';
if ( ! empty( $value ) ) {
update_post_meta( $post_id, 'exclude_algolia_search_flag', $value );
} else {
delete_post_meta( $post_id, 'exclude_algolia_search_flag' );
}
}
add_action( 'save_post', 'exclude_algolia_save_post', 10, 1 );
// Hook into Algolia to manipulate the post that should be indexed.
function exclude_algolia_filter_post( $should_index, WP_Post $post ) {
if ( false === $should_index ) {
return false;
}
$exclude_algolia_search_flag = get_post_meta( $post->ID, 'exclude_algolia_search_flag', true );
if ( 1 == $exclude_algolia_search_flag ) {
return false;
}
return $should_index;
}
// Hook into Algolia to manipulate the post that should be indexed.
add_filter( 'algolia_should_index_searchable_post', 'exclude_algolia_filter_post', 10, 2 );
add_filter( 'algolia_should_index_post', 'exclude_algolia_filter_post', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment