0

How do I customize the "categories widget" to append additional text on the sidebar display?

from

category 1
category 2
category 3
category 4

to

category 1 folder
category 2 folder
category 3 folder
category 4 folder

Without actually modifying the category name in the database, I would like to add some actual customized text to the sidebar front-end display.

Currently my sidebar is being called via dynamic_sidebar( 'sidebar-1' )

Update:

I managed to use a plugin to solve my problem.

Widget Output Filters

Is there any recommendation of how to get this done without the use of a plugin?

1

To append text to the end of the titles in that widget and nothing more i.e. does not matter the URL, you can just use some CSS.

<li>
 <a class="my-folder-categories">Category 1</a>
</li>

a.my-folder-categories-folder:after {content:" folder";

This will effectivly make the text appear as so:

category 1 folder

Be sure to add that space in the content value.

If you want to use the plugin mentioned but not as a plugin, use the source and modify to your specs.

/**
* Class Widget_Output_Filters
*
* Allows developers to filter the output of any WordPress widget.
*/
class Widget_Output_Filters {

/**
* Initializes the functionality by registering actions and filters.
*/
public function __construct() {

// Priority of 9 to run before the Widget Logic plugin.
add_filter( 'dynamic_sidebar_params', array( $this, 'filter_dynamic_sidebar_params' ), 9 );
}

/**
* Replaces the widget's display callback with the Dynamic Sidebar Params display callback, storing the original callback for use later.
*
* The $sidebar_params variable is not modified; it is only used to get the current widget's ID.
*
* @param array $sidebar_params The sidebar parameters.
*
* @return array The sidebar parameters
*/
public function filter_dynamic_sidebar_params( $sidebar_params ) {

if ( is_admin() ) {
return $sidebar_params;
}

global $wp_registered_widgets;
$current_widget_id = $sidebar_params[0]['widget_id'];

$wp_registered_widgets[ $current_widget_id ]['original_callback'] = $wp_registered_widgets[ $current_widget_id ]['callback'];
$wp_registered_widgets[ $current_widget_id ]['callback'] = array( $this, 'display_widget' );

return $sidebar_params;
}

/**
* Execute the widget's original callback function, filtering its output.
*/
public function display_widget() {

global $wp_registered_widgets;
$original_callback_params = func_get_args();

$widget_id         = $original_callback_params[0]['widget_id'];
$original_callback = $wp_registered_widgets[ $widget_id ]['original_callback'];

$widget_id_base = $original_callback[0]->id_base;
$sidebar_id     = $original_callback_params[0]['id'];

if ( is_callable( $original_callback ) ) {

ob_start();
call_user_func_array( $original_callback, $original_callback_params );
$widget_output = ob_get_clean();

/**
* Filter the widget's output.
*
* @param string $widget_output  The widget's output.
* @param string $widget_id_base The widget's base ID.
* @param string $widget_id      The widget's full ID.
* @param string $sidebar_id     The current sidebar ID.
*/
echo apply_filters( 'widget_output', $widget_output, $widget_id_base, $widget_id, $sidebar_id );
}
}
}
  • It does the trick. Do you know if it is search engine friendly and will this css solution be indexable by Google? – KDX May 16 '16 at 3:33
  • 1
    Good question, @KDX. It is not considered by search engines as it is just a CSS property. Depending on your base keywords and page by page structure I don't see how this would inhibit your serps. *it would not affect the links otherwise. – Jarmerson May 16 '16 at 3:39
  • The other easy solution is to just rename the categories to include "folder." – Jarmerson May 16 '16 at 3:42
  • I do plan to add keyword instead of just "folder" to the category links. Your solution is convenient and I can easily append text, but it won't serve my actual needs. And yes, I thought of that, renaming is an easy solution, however, I'm trying to append various keywords to the same category across different pages/locations, thus I would like to keep the category short so I can work with it. Currently I'm using str_replace() to apply text inject from get_the_category_list(), only widget part is giving me a hard time. – KDX May 16 '16 at 3:51
  • So you're doing a str_replace() within the WP function to find categories and append or change the title? – Jarmerson May 16 '16 at 3:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.