Remove Extra Classes from Post Title

I want to remove tag, category classes from Post Titles of my WordPress blog. On the frontend, Wordpress generates extra classes for every single article title.

For Example, this article, has these article title classes "category-tips-guides" "tag-indian-army" as displayed in the following image:

I have tried the following snippet to remove some extra classes (shared on this page) and it works:

function lsmwp_remove_postclasses($classes, $class, $post_id) {
$classes = array_diff( $classes, array(
    'hentry',
    'type-' . get_post_type($post_id),
    'status-' . get_post_status($post_id),
) );
return $classes;
add_filter('post_class', 'lsmwp_remove_postclasses', 10, 3);

But the issue with this code is that it doesn't remove any Tag, Category classes as I want.

Some References to post_class:

  1. https://codex.wordpress.org/Function_Reference/post_class
  2. https://core.trac.wordpress.org/browser/tags/4.9.7/src/wp-includes/post-template.php#L0

Topic filters post-class Wordpress

Category Web


Try this.

// **** Remove unwanted classes
function remove_classes($classes, $class, $post_id)
{
    // Array that holds the undesired classes
    $removeClasses = array(
        'category-',
        'tag-'
    );

    // Array to store the new class names
    $newClasses = array();
    foreach ($classes as $_class)
    {
        // Iterate through the array of undesired classes and
        // check if the current $_class name starts with the
        // undesired class name
        $hasClass = FALSE;
        foreach ($removeClasses as $_removeClass)
        {
            if (strpos($_class, $_removeClass) === 0)
            {
                $hasClass = TRUE;
                break;
            }
        }

        // If $_class does not contain an undesired class name,
        // add it to the array of new class names.
        if (!$hasClass)
        {
            $newClasses[] = $_class;
        }
    }

    // Return the array of new class names
    return ($newClasses);
}
add_filter('post_class', 'remove_classes', 10, 3);

This filter declares an array of undesired class names ($removeClasses). Expand it with the class names you don't want to have.

Then, the function iterates through the passed array of classes ($classes) and checks if it contains classes you have defined in the $removeClasses array. If not, it will add it to a new array ($newClasses). If yes, it will skip it.

Finally it returns the new array $newClasses.

Basically it sorts out the classes you don't want. And instead of manipulating the passed $classes array, it creates a new one with only the good classes and returns that instead.

I haven't tested it though. I don't have a WordPress installation available here right now to fiddle around with. It could be that it doesn't work, because post_class might not be the right filter.


Obviously you will also have to remove all elements from $classes that begin with tag- and category-. The difficult is that other than with type- and status- there may be multiple instance of them.

So, you will have to loop through the array and remove any instances that start with these strings:

foreach ($classes as $class) {
  if (substr ($class, 0, 4) === "tag-") $class = '';
  if (substr ($class, 0, 8) === "category-") $class = '';
  }

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.