Skip to content

Commit

Permalink
Merge pull request #13 from dartiss/develop
Browse files Browse the repository at this point in the history
Version 1.0
  • Loading branch information
dartiss authored Dec 2, 2020
2 parents f19954c + 4649e28 commit 4068fe9
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 29 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

<img src="https://ps.w.org/solo-search/assets/icon-128x128.png" align="left">You know when you search for something on a site and it finds just one result? You then have to click into it to display it. That's annoying. It's also adding an extra page load which is not necessary.

This plugin simply removes this middle step - if your search returns one result, it will be shown in all its post/page/whatever (delete as appropriate) glory. As well as a quicker answer for your visitor, removing this improves your site's sustainability (okay, just a little... but every little helps, right?).
And what about if you search for an exact match for a title and, well, it serves it up along with a number of other possible results. Hey, why didn't you just show me the one that I typed the name of in exactly? Well, you can do that too. Cool, eh?

The code passes WordPress and WordPress VIP coding standards. Because you're worth it.
This plugin simply removes this middle step - if your search returns one result or you type in the name of a title, it will be shown in all its post/page (delete as appropriate) glory. As well as a quicker answer for your visitor, removing this improves your site's sustainability (okay, just a little... but every little helps, right?).

The code also passes WordPress and WordPress VIP coding standards. Because you're worth it.

Thanks to my co-worker [Kailey](https://profiles.wordpress.org/trepmal/) for [the original code](https://trepmal.com/2011/04/22/redirect-when-search-query-only-returns-one-match/), which I've been happily using on my own site for many years. I thought it was time to share the ♥️.

Expand Down
Binary file added assets/screenshot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 27 additions & 11 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
=== Solo ===
Contributors: dartiss
Donate link: https://artiss.blog/donate
Tags: result, search, single, solo
Tags: result, search, single, solo, title
Requires at least: 4.6
Tested up to: 5.6
Requires PHP: 5.3
Stable tag: 0.2
Stable tag: 1.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand All @@ -15,15 +15,15 @@ License URI: http://www.gnu.org/licenses/gpl-2.0.html

You know when you search for something on a site and it finds just one result? You then have to click into it to display it. That's annoying. It's also adding an extra page load which is not necessary.

This plugin simply removes this middle step - if your search returns one result, it will be shown in all its post/page/whatever (delete as appropriate) glory. As well as a quicker answer for your visitor, removing this improves your site's sustainability (okay, just a little... but every little helps, right?).
And what about if you search for an exact match for a title and, well, it serves it up along with a number of other possible results. Hey, why didn't you just show me the one that I typed the name of in exactly? Well, you can do that too. Cool, eh?

Not only that, but with a flick of a switch, you can also get it to go straight to any results where the search text matches a post or page title. Cool, eh?
This plugin simply removes this middle step - if your search returns one result or you type in the name of a title, it will be shown in all its post/page (delete as appropriate) glory. As well as a quicker answer for your visitor, removing this improves your site's sustainability (okay, just a little... but every little helps, right?).

The code passes WordPress and WordPress VIP coding standards. Because you're worth it.
The code also passes WordPress and WordPress VIP coding standards. Because you're worth it.

Thanks to my co-worker [Kailey](https://profiles.wordpress.org/trepmal/) for [the original code](https://trepmal.com/2011/04/22/redirect-when-search-query-only-returns-one-match/), which I've been happily using on my own site for many years. I thought it was time to share the ♥️.

Iconography is courtesy of the very talented [Janki Rathod](https://www.fiverr.com/jankirathore) ♥️
Iconography is courtesy of the very talented [Janki Rathod](https://www.fiverr.com/jankirathore).

👉 Please visit the [Github page](https://github.com/dartiss/solo "Github") for the latest code development, planned enhancements and known issues 👈

Expand All @@ -34,22 +34,38 @@ Solo can be found and installed via the Plugin menu within WordPress administrat
1. Upload the entire `solo-search` folder to your `wp-content/plugins/` directory.
2. Activate the plugin through the 'Plugins' menu in WordPress administration.

It's now ready to go.
It's now ready to go. By default, not all features are active - head to Settings -> General -> Solo for all the options.

== Frequently Asked Questions ==

= Why do you have to switch on the "exact match" option? =

Because, if you're using pretty generic titles (e.g. "Twitter") then you may not want this behavior.
Because, if you're using pretty generic titles (e.g. "Twitter") then you may not want this behavior.

Let me explain.

On my own site (artiss.blog - always a good read. Never dull. Please subscribe), I often use short, single names for pages. "About", "Blog", that kind of thing. For my posts, however, I use something long and descriptive. For example, "See what the stars of The Banana Splits look like now. Number 3 will amaze you". For this reason, I have exact matching switched off for pages but on for posts - if someone types in that post title than they are very welcome to have it served straight up to them.

= For the "exact match" feature, it only works for posts and pages. Can I add additional taxonomies? =

Not at the moment, but if this interests you, please let me know!
What kind of monster are you?

But, seriously, not at the moment. But if this interests you, please let me know!

== Screenshots ==

1. The settings that are available

== Changelog ==

I use semantic versioning, with the first release being 0.1.

= 1.0 =
* Enhancement: You didn't ask for it, but you got it anyway - the search will now display content if you search for an exact match on the title
* Enhancement: Settings now allow you to turn each of the options on and off, to your likely. You can switch them all of, if you want, although that's a waste of an active plugin, if we're being honest
* Enhancement: Added a link in the plugin meta so you can go straight to the settings. Because I care
* Maintenance: Various tweaks and fiddles, some of which may even be legal

= 0.2 =
* Bug: Fixed text domain

Expand All @@ -58,5 +74,5 @@ I use semantic versioning, with the first release being 0.1.

== Upgrade Notice ==

= 0.2 =
* Fixed the incorrect text domain
= 1.0 =
* 100% more features. 100% more settings. 0% bugs (I can hope, right?)
172 changes: 156 additions & 16 deletions solo-search.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
<?php
/**
Plugin Name: Solo
Plugin URI: https://wordpress.org/plugins/solo-search/
Description: 🔍 Instantly display a single search result
Version: 0.2
Author: David Artiss
Author URI: https://artiss.blog
Text Domain: solo-search
@package solo
* Solo
*
* @package solo
* @author David Artiss
* @license GPL-2.0-or-later
*
* Plugin Name: Solo
* Plugin URI: https://wordpress.org/plugins/solo-search/
* Description: 🔍 Instantly display a single search result
* Version: 1.0
* Requires at least: 4.6
* Requires PHP: 5.3
* Author: David Artiss
* Author URI: https://artiss.blog
* Text Domain: solo-search
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License version 2, as published by the Free Software Foundation. You may NOT assume
* that you can use any other version of the GPL.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

/**
Expand All @@ -24,19 +39,43 @@ function solo_plugin_meta( $links, $file ) {

if ( false !== strpos( $file, 'solo-search.php' ) ) {

$links = array_merge( $links, array( '<a href="https://github.com/dartiss/solo">' . __( 'Github', 'solo-search' ) . '</a>' ) );
$links = array_merge(
$links,
array( '<a href="https://github.com/dartiss/solo">' . __( 'Github', 'solo-search' ) . '</a>' ),
array( '<a href="https://wordpress.org/support/plugin/solo-search">' . __( 'Support', 'solo-search' ) . '</a>' ),
array( '<a href="https://artiss.blog/donate">' . __( 'Donate', 'solo-search' ) . '</a>' ),
array( '<a href="https://wordpress.org/support/plugin/solo-search/reviews/#new-post">' . __( 'Write a Review', 'solo-search' ) . '&nbsp;⭐️⭐️⭐️⭐️⭐️</a>' )
);
}

$links = array_merge( $links, array( '<a href="https://wordpress.org/support/plugin/solo-search">' . __( 'Support', 'solo-search' ) . '</a>' ) );
return $links;
}

$links = array_merge( $links, array( '<a href="https://artiss.blog/donate">' . __( 'Donate', 'solo-search' ) . '</a>' ) );
add_filter( 'plugin_row_meta', 'solo_plugin_meta', 10, 2 );

/**
* Modify actions links.
*
* Add or remove links for the actions listed against this plugin
*
* @param string $actions Current actions.
* @param string $plugin_file The plugin.
* @return string Actions, now with deactivation removed!
*/
function solo_action_links( $actions, $plugin_file ) {

// Make sure we only perform actions for this specific plugin!
if ( strpos( $plugin_file, 'solo-search.php' ) !== false ) {

// Add link to the settings page.
array_unshift( $actions, '<a href="' . admin_url() . 'options-general.php">' . __( 'Settings', 'solo-search' ) . '</a>' );

$links = array_merge( $links, array( '<a href="https://wordpress.org/support/plugin/solo-search/reviews/#new-post">' . __( 'Write a Review', 'solo-search' ) . '&nbsp;⭐️⭐️⭐️⭐️⭐️</a>' ) );
}

return $links;
return $actions;
}

add_filter( 'plugin_row_meta', 'solo_plugin_meta', 10, 2 );
add_filter( 'plugin_action_links', 'solo_action_links', 10, 2 );

/**
* If just one post in result just show it
Expand All @@ -47,11 +86,112 @@ function solo_remove_single_results() {

global $wp_query;

if ( 1 == $wp_query->post_count && 1 == $wp_query->max_num_pages ) {
$posts = get_option( 'solo_option_exact_posts' );
$pages = get_option( 'solo_option_exact_pages' );
$single = get_option( 'solo_option_single' );
if ( false === $single ) {
$single = 1;
}

// If an exact match for a post or page title is found, go straight to that.
if ( function_exists( 'wpcom_vip_get_page_by_title' ) ) {

if ( wpcom_vip_get_page_by_title( get_search_query(), 'OBJECT', 'post' ) && 1 == $posts ) {
wp_safe_redirect( get_permalink( wpcom_vip_get_page_by_title( get_search_query() )->ID ) );
exit;
}

if ( wpcom_vip_get_page_by_title( get_search_query(), 'OBJECT', 'page' ) && 1 == $pages ) {
wp_safe_redirect( get_permalink( wpcom_vip_get_page_by_title( get_search_query() )->ID ) );
exit;
}
} else {

if ( get_page_by_title( get_search_query(), 'OBJECT', 'post' ) && 1 == $posts ) { // @codingStandardsIgnoreLine -- for non-VIP environments
wp_safe_redirect( get_permalink( get_page_by_title( get_search_query() )->ID ) ); // @codingStandardsIgnoreLine -- for non-VIP environments
exit;
}

if ( get_page_by_title( get_search_query(), 'OBJECT', 'page' ) && 1 == $pages ) { // @codingStandardsIgnoreLine -- for non-VIP environments
wp_safe_redirect( get_permalink( get_page_by_title( get_search_query() )->ID ) ); // @codingStandardsIgnoreLine -- for non-VIP environments
exit;
}
}

// If only one result is found, redirect straight to it!
if ( 1 == $single && 1 == $wp_query->post_count && 1 == $wp_query->max_num_pages ) {
wp_safe_redirect( get_permalink( $wp_query->posts[0]->ID ) );
exit;
}
}
}

add_action( 'template_redirect', 'solo_remove_single_results' );

/**
* Add to settings
*
* Add a field to the general settings screen for assorted options
*/
function solo_settings_init() {

add_settings_section( 'solo_section', __( 'Solo Search', 'solo-search' ), 'solo_section_callback', 'general' );

add_settings_field( 'solo_option_single', __( 'Single result redirect', 'solo-search' ), 'solo_setting_single_callback', 'general', 'solo_section', array( 'label_for' => 'solo_option_single' ) );

register_setting( 'general', 'solo_option_single' );

add_settings_field( 'solo_option_exact_posts', __( 'Exact title match', 'solo-search' ), 'solo_setting_exact_posts_callback', 'general', 'solo_section', array( 'label_for' => 'solo_option_exact_posts' ) );

register_setting( 'general', 'solo_option_exact_posts' );

add_settings_field( 'solo_option_exact_pages', '', 'solo_setting_exact_pages_callback', 'general', 'solo_section', array( 'label_for' => 'solo_option_exact_pages' ) );

register_setting( 'general', 'solo_option_exact_pages' );
}

add_action( 'admin_init', 'solo_settings_init' );

/**
* Section callback
*
* Create the new section that we've added to the general settings screen
*/
function solo_section_callback() {

echo esc_attr( __( 'Define how you want search results to be handled, when exact matches and single results are found.', 'solo-search' ) );

}

/**
* Single match setting callback
*
* Output the settings field for whether to allow single matches
*/
function solo_setting_single_callback() {

echo '<label><input name="solo_option_single" type="checkbox" value="1" ' . checked( 1, get_option( 'solo_option_single', 1 ), false ) . '/>&nbsp;&nbsp;If a single result is found, redirect to that result</label>';

}

/**
* Exact posts setting callback
*
* Output the settings field for whether to match exact named posts
*/
function solo_setting_exact_posts_callback() {

echo '<label><input name="solo_option_exact_posts" type="checkbox" value="1" ' . checked( 1, get_option( 'solo_option_exact_posts', '' ), false ) . '/>&nbsp;&nbsp;Posts</label>';

}

/**
* Exact pages setting callback
*
* Output the settings field for whether to match exact named pages
*/
function solo_setting_exact_pages_callback() {

echo '<label><input name="solo_option_exact_pages" type="checkbox" value="1" ' . checked( 1, get_option( 'solo_option_exact_pages', '' ), false ) . '/>&nbsp;&nbsp;Pages</label></br></br><p class="description">If an exact match to a title is found, redirect to it.</p>';

}

0 comments on commit 4068fe9

Please sign in to comment.