Adding custom settings screens to SearchWP

Last updated December 18, 2018 « Knowledge Base

Note: This functionality was introduced in SearchWP 2.6

SearchWP’s settings screen can be extended in a number of ways. The default settings screens are implemented using this functionality, so they can all be customized by modifying the hooks as outlined below. To add your own tab to the SearchWP settings screen, begin with the following:

<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Implement the shell of a SearchWP settings screen tab
*/
class SearchWP_Settings_Implementation_My_Settings_Screen {
function init() {
// render the tab on the settings screen
add_action( 'searchwp_settings_nav_tab', array( $this, 'render_tab_my_settings' ) );
// render the My Settings view when the my_settings tab is viewed
add_action( 'searchwp_settings_view\my_settings', array( $this, 'render_view_my_settings' ) );
}
/**
* Callback to render the settings nav for the My Settings screen
*/
function render_tab_my_settings() {
if ( current_user_can( apply_filters( 'searchwp_settings_cap', 'manage_options' ) ) ) {
searchwp_get_nav_tab( array(
'tab' => 'my_settings',
'label' => __( 'My Settings', 'searchwp' ),
) );
}
}
/**
* Outputs the HTML for the My Settings tab
*/
function render_view_my_settings() {
?>
<div class="my-settings">
<h3><?php echo __( 'My Settings', 'searchwp' ); ?></h3>
</div>
<?php
}
}
$my_searchwp_settings = new SearchWP_Settings_Implementation_My_Settings_Screen();
$my_searchwp_settings->init();
view raw gistfile1.php hosted with ❤ by GitHub

That snippet will add a “My Settings” tab to the navigation bar and when that tab is viewed will output a heading that reads “My Settings”. It’s just a shell of what a settings screen can do. There are many other hooks available within the settings area of SearchWP. Using the following hooks you can implement your own fully custom SearchWP settings screens.

searchwp_settings_before_header
Output markup before the navigation bar

<?php
function my_searchwp_settings_before_header() {
echo 'Before settings header';
}
add_action( 'searchwp_settings_before_header', 'my_searchwp_settings_before_header' );
view raw gistfile1.php hosted with ❤ by GitHub

searchwp_settings_nav_tab
Output a tab in the settings navigation

<?php
function my_searchwp_settings_nav_tab() {
searchwp_get_nav_tab( array(
'tab' => 'my_settings',
'label' => __( 'My Settings', 'searchwp' ),
) );
}
add_action( 'searchwp_settings_nav_tab', 'my_searchwp_settings_nav_tab', 200 );
view raw gistfile1.php hosted with ❤ by GitHub

searchwp_settings_after_header
Output markup after the navigation bar

<?php
function my_searchwp_settings_after_header() {
echo 'After settings header';
}
add_action( 'searchwp_settings_after_header', 'my_searchwp_settings_after_header' );
view raw gistfile1.php hosted with ❤ by GitHub

searchwp_settings_before\{my_settings}
Output markup before the settings screen view where {my_settings} is the name of your settings tab

<?php
function my_searchwp_settings_before_my_settings() {
echo 'Before the My Settings view output';
}
add_action( 'searchwp_settings_before\my_settings', 'my_searchwp_settings_before_my_settings' );
view raw gistfile1.php hosted with ❤ by GitHub

searchwp_settings_view\{my_settings}
Output markup for the settings screen view where {my_settings} is the name of your settings tab

<?php
function my_searchwp_settings_view_my_settings() {
echo 'The settings view itself';
}
add_action( 'searchwp_settings_view\my_settings', 'my_searchwp_settings_view_my_settings' );
view raw gistfile1.php hosted with ❤ by GitHub

searchwp_settings_after\{my_settings}
Output markup after the settings screen view where {my_settings} is the name of your settings tab

<?php
function my_searchwp_settings_after_my_settings() {
echo 'After the My Settings view output';
}
add_action( 'searchwp_settings_after\my_settings', 'my_searchwp_settings_after_my_settings' );
view raw gistfile1.php hosted with ❤ by GitHub
Fix Search on Your Site. No Coding Required!

Now you can utilize all of the content that's gone unrecognized by native WordPress search instantly with SearchWP

Get SearchWP