Skip to content
Kurtis Shaner edited this page Nov 1, 2018 · 2 revisions

Table of Contents

Filters

jarvis/themes

Filter the list of themes to add custom themes or remove themes

add_filter( 'jarvis/themes', function( $themes ) {
    $themes['my-brand'] = 'My Brand'; // add a custom theme
    unset( $themes['solarized-light'] ); // remove a theme
    return $themes;
} );

jarvis/theme/{{theme-name}}

Provide a css uri for a theme

add_filter( 'jarvis/theme/my-brand', function( $uri ) {
    return 'https://example.com/jarvis-theme-my-brand.css';
} );

jarvis/theme_default

Set the default theme for a user when they haven't made their own selection in the user profile page

add_filter( 'jarvis/theme_default', function( $default ) {
    return 'my-brand';
} );

jarvis/instants

filter the list of instant classes that provide instant suggestions through their get method

@see https://github.com/WDGDC/Jarvis/wiki/Extending-Jarvis

add_filter( 'jarvis/instants', function( $instants ) {
    array_push( $instants, 'MyInstantsClass' );
    return $instants;
} );

jarvis/suggestions

modify any of the prepopulated suggestions from instants

add_filter( 'jarvis/suggestions, function( $suggestions ) {

    // this contrived example doesn't want any posts returned
    $suggestions = array_filter( $suggestions, function( $suggestion ) {
        return $suggestion->kind !== 'post';
   } );

    return $suggestions;

} );

jarvis/suggestions/recent/query

Modify the WP_Query arguments for the recent posts instant

add_filter( 'jarvis/suggestions/recent/query', function( $args ) {
    // this example makes it the 10 most recent edited by anyone, and not the current author
    unset( $args['author'] );
    return $args;
} );

jarvis/recent/posts

modify the models of the recent posts after they have been queried and modeled

add_filter( 'jarvis/recent/posts', function( $models ) {
    // this example changes the icons for recent posts to the clock dashicon
    foreach( $models as &$model ) {
        $model->iconType = 'dashicon';
        $model->icon = 'dashicons-clock';
    }
    return $models;
} );

Modify the post types that are searched as part of the search query, by default this includes post_types that are registered with 'show_ui' => true.

jarvis/include_post_types

add_filter( 'jarvis/include_post_types', function( $post_types ) {
    // this example excludes pages from being searched
    $post_types = array_diff( $post_types, [ 'pages' ] );
    return $post_types;
} );

jarvis/exclude_post_stati

Exclude any custom post_status that you do not want jarvis to search. Default is to exclude revision, auto-draft, and trash

add_filter( 'jarvis/exclude_post_stati', function( $stati ) {
    array_push( $stati, 'my-custom-post-status' );
    return $stati;
} );

jarvis/exclude_post_ids

Exclude any post ids you want excluded from searching

add_filter( 'jarvis/exclude_post_ids', function( $ids ) {
	// this example excludes any posts by an author id
	$exclude = get_posts( [
		'author' => 1,
		'fields' => $ids
	] );

	return array_merge( $ids, $exclude );
} )

jarvis/results

This filter allows modification of search results

add_filter( 'jarvis/results', function( $results, $query ) {
	// this contrived example removes posts from the results
	if ( 'do_not_search_posts' === $query ) {
		$results = array_filter( $results, function( $result ) {
			return $result->kind !== 'post';
		} );
	}

	return $results;
}, 10, 2 );

Actions

jarvis/model

This action allows you to hook into any model that extends the base \Jarvis\Models\Model. Model is passed by reference.

add_action( 'jarvis/model', function( $model ) {
	if ( preg_match( '/Top Secret/i', $model->title ) ) {
		$model->title = '*********';
	}
} );

jarvis/model/{{model-name}}

This action allows you to hook into a specific model type.

add_action( 'jarvis/model/user', function( $model ) {
	array_push( $model->attributes, get_user_meta( $model->id, 'my_custom_attribute', true ) );
} );