Skip to content

Instantly share code, notes, and snippets.

@imath
Created February 2, 2017 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imath/bb4d0af0fd8111b8adf118d05cc8858d to your computer and use it in GitHub Desktop.
Save imath/bb4d0af0fd8111b8adf118d05cc8858d to your computer and use it in GitHub Desktop.
Add an extra filter nav to filter ideas by category or only get unrated ideas (Needs the WP Idea Stream WordPress plugin)
<?php
/**
* For more infos about how to use the wp-idea-stream-custom.php file
* @see https://github.com/imath/wp-idea-stream/wiki/wp-idea-stream-custom.php
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/**
* Add some inline style and script.
*
* In a regular plugin or theme, using wp_enque_style() & wp_enqueue_script()
* would be a lot better!
*/
function xwara_extra_enqueue_scripts() {
// Only enqueue inline script and inline style id viewing an Idea loop
if ( ! wp_idea_stream_is_idea_archive() && ! wp_idea_stream_is_category() && ! wp_idea_stream_is_tag() ) {
return;
}
// Inline style
wp_add_inline_style( 'wp-idea-stream-style', '
#xwara-extra-filters {
margin: 1.5em 0;
background-color: #f1f1f1;
padding: 0.5em;
overflow: hidden;
display: table;
width: 100%;
border: solid 1px #ccc;
}
#xwara-extra-filters label,
#xwara-extra-filters .submit {
width: auto;
display: table-cell;
}
' );
// Get term links.
$term_links = array();
if ( wp_idea_stream_ideas_has_terms() ) {
$idea_terms = wp_idea_stream_get_idea_var( 'edit_form_terms' );
foreach ( $idea_terms as $idea_term ) {
$term_links[ $idea_term->term_id ] = esc_url_raw( wp_idea_stream_get_category_url( $idea_term ) );
}
}
if ( ! empty( $term_links ) ) {
// As we will use jQuery, make sure it is enqueue
wp_enqueue_script( 'jquery' );
wp_add_inline_script( 'jquery-migrate', sprintf( '
var WPideaTerms = %1$s;
var WPformAction = \'%2$s\';
jQuery( document ).ready( function( $ ) {
// Replace term IDs by term links.
$.each( $( \'#select_idea_category option\' ), function( i, o ) {
if ( WPideaTerms[ $( o ).val() ] ) {
$( o ).prop( \'value\', WPideaTerms[ $( o ).val() ] );
}
} );
$( \'#xwara-extra-filters\' ).on( \'submit\', function( f ) {
if ( \'-1\' !== $( \'#select_idea_category\' ).val() ) {
$( f.currentTarget ).prop( \'action\', $( \'#select_idea_category\' ).val() );
} else {
$( f.currentTarget ).prop( \'action\', WPformAction );
}
$( \'#select_idea_category\' ).prop( \'disabled\', true );
return f;
} );
} );
', wp_json_encode( $term_links ), esc_url_raw( wp_idea_stream_get_root_url() ) ) );
}
}
add_action( 'wp_idea_stream_enqueue_scripts', 'xwara_extra_enqueue_scripts', 20 );
/**
* Add an extra filter nav after the main one.
*
* Another option for a theme could be to override the
* /templates/archive.php within its folder
* eg: nameoftheme/wp-idea-stream/archive.php
*
* @return string HTML Output.
*/
function xwara_extra_filtering_bar() {
$dropdown_args = array(
'show_option_none' => __( 'Select Category' ),
'taxonomy' => wp_idea_stream_get_category(),
'id' => 'select_idea_category',
'name' => 'select_idea_category',
);
// If viewing a category archive, set the selected dropdown value.
if ( wp_idea_stream_is_category() ) {
$dropdown_args['selected'] = wp_idea_stream_get_current_term()->term_id;
}
// Output the extra filter nav.
?>
<form id="xwara-extra-filters" class="nav-form" action="" method="get">
<label for="select_idea_category">
<?php esc_html_e( 'Filter by:', 'custom-text-domain' ); ?>
<?php wp_dropdown_categories( $dropdown_args ); ?>
</label>
<label for="unrated_ideas">
<input type="checkbox" name="unrated_ideas" id="unrated_ideas" value="1" <?php checked( ! empty( $_GET['unrated_ideas'] ) ); ?>>
<?php esc_html_e( 'Only show unrated ideas', 'custom-text-domain' ); ?>
</label>
<div class="submit">
<input type="submit" value="<?php esc_attr_e( 'Filter', 'custom-text-domain' ); ?>">
</div>
</form>
<?php
}
add_action( 'wp_idea_stream_after_archive_main_nav', 'xwara_extra_filtering_bar' );
/**
* Edit the main query arguments to only fetch
* unrated ideas if needed.
*
* @param WP_Query $q The WordPress main Query object.
*/
function xwara_parse_query( WP_Query $q ) {
if ( ! wp_idea_stream_is_idea_archive() && ! wp_idea_stream_is_category() && ! wp_idea_stream_is_tag() ) {
return;
}
if ( empty( $_GET['unrated_ideas'] ) ) {
return;
}
$orderby = $q->get( 'orderby' );
if ( 'rates_count' === $orderby ) {
$q->set( 'orderby', 'date' );
}
$q->set( 'meta_query', array(
array(
'key' => '_ideastream_average_rate',
'compare' => 'NOT EXISTS'
)
) );
}
add_action( 'parse_query', 'xwara_parse_query', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment