Skip to content

Instantly share code, notes, and snippets.

@eruizdechavez
Last active February 22, 2021 04:49
Show Gist options
  • Save eruizdechavez/9a09542799923c21b1104b8e89ae36c0 to your computer and use it in GitHub Desktop.
Save eruizdechavez/9a09542799923c21b1104b8e89ae36c0 to your computer and use it in GitHub Desktop.
Search for Connections WordPress Plugin
<?php
add_filter(
'cn_entry_output_category_item',
function( $content, $category, $count, $i, $properties, $instance ) {
global $wp_rewrite;
$text = '';
$rel = is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ? 'rel="category tag"' : 'rel="category"';
$entry = $instance->getObject();
$url = cnTerm::permalink(
$category,
'category',
array(
'force_home' => $entry->directoryHome['force_home'],
'home_id' => $entry->directoryHome['page_id'],
)
);
$text .= '<a href="' . $url . '" ' . $rel . '>' . esc_html( $category->name ) . '</a>';
return sprintf(
'<%1$s class="cn-category-name cn_category cn-category-%2$d">%3$s%4$s</%1$s>',
$instance->get( 'item_tag' ),
$category->term_id,
$text,
$count > $i && 'list' !== $instance->get( 'type' ) ? esc_html( $instance->get( 'separator' ) ) : ''
);
},
10,
6
);
<?php
get_header();
global $wpdb;
//phpcs:ignore WordPress.Security.NonceVerification.Recommended
$search_param = isset( $_GET['search'] ) ? sanitize_text_field( $_GET['search'] ) : '';
//phpcs:ignore WordPress.Security.NonceVerification.Recommended
$category_param = isset( $_GET['category'] ) ? sanitize_text_field( $_GET['category'] ) : '';
$shortcode = '[connections %%CATEGORY%% %%IDS%%]';
$id_attr = '';
if ( ! empty( $search_param ) ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
$search_results = $wpdb->get_col(
$wpdb->prepare(
"SELECT id
FROM `{$wpdb->prefix}connections`
WHERE MATCH(`family_name`,`first_name`,`middle_name`,`last_name`,`title`,`organization`,`department`,`contact_first_name`,`contact_last_name`,`bio`,`notes`)
AGAINST (%s IN BOOLEAN MODE)",
$search_param
)
);
if ( ! empty( $search_results ) ) {
$id_attr = 'id="' . implode( ',', $search_results ) . '"';
}
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
$categories = $wpdb->get_results(
"SELECT `term_id`, `name`
FROM `{$wpdb->prefix}connections_terms`
WHERE `term_id` > 1",
ARRAY_A
);
if ( ! empty( $category_param ) ) {
$category_attr = 'category_in="' . $category_param . '"';
}
$shortcode = str_replace( '%%CATEGORY%%', $category_attr, $shortcode );
$shortcode = str_replace( '%%IDS%%', $id_attr, $shortcode );
$sidebar_layout = apply_filters( 'hestia_sidebar_layout', get_theme_mod( 'hestia_page_sidebar_layout', 'full-width' ) );
$wrap_class = apply_filters( 'hestia_filter_page_content_classes', 'col-md-8 page-content-wrap ' );
do_action( 'hestia_before_single_page_wrapper' );
?>
<div class="<?php echo hestia_layout(); ?>">
<div class="blog-post">
<div class="container">
<?php the_post(); ?>
<article id="post-<?php the_ID(); ?>" class="section section-text">
<div class="row">
<?php
if ( 'sidebar-left' === $sidebar_layout ) {
do_action( 'hestia_page_sidebar' );
}
?>
<div class="<?php echo esc_attr( $wrap_class ); ?>">
<?php
do_action( 'hestia_before_page_content' );
the_content();
?>
<form action="<?php the_permalink(); ?>" method="GET">
<table>
<tr>
<td width="45%">
<label for="search"><?php esc_html_e( 'Buscar:', 'tiempodemujeres' ); ?></label>
<input style="display: inline-block; width: auto; vertical-align: baseline" type="text" name="search" id="search" value="<?php echo esc_attr( $search_param ); ?>">
</td>
<td width="45%">
<label for="category"><?php esc_html_e( 'en:', 'tiempodemujeres' ); ?></label>
<select name="category" id="category">
<option value="" <?php echo '' === $category_param ? 'selected' : ''; ?>>
<?php esc_html_e( 'Todas las categorías', 'tiempodemujeres' ); ?>
</option>
<?php foreach ( $categories as $category ) : ?>
<option value="<?php echo esc_attr( $category['term_id'] ); ?>" <?php echo $category['term_id'] === $category_param ? 'selected' : ''; ?>>
<?php echo esc_html( $category['name'] ); ?>
</option>
<?php endforeach; ?>
</select>
</td>
<td width="10%">
<input type="submit" value="<?php esc_html_e( 'Buscar', 'tiempodemujeres' ); ?>">
</td>
</tr>
</table>
</form>
<?php echo do_shortcode( $shortcode ); ?>
<?php
echo apply_filters( 'hestia_filter_blog_social_icons', '' );
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
?>
</div>
<?php
if ( 'sidebar-right' === $sidebar_layout ) {
do_action( 'hestia_page_sidebar' );
}
?>
</div>
</article>
<?php
if ( is_paged() ) {
hestia_single_pagination();
}
?>
</div>
</div>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment