1

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:

enter image description here

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
6
  • Is jQuery an option? Commented Jul 7, 2018 at 12:29
  • No sir, No jQuery, please. Currently, my blog doesn't make use of jQuery. So, I don't want to use jQuery just for this single purpose. Hope you understand :) Commented Jul 7, 2018 at 12:46
  • jQuery is a part of WordPress core. So even if you don't use it, WordPress already uses it anyway. You should be more specific in your question. It would help if you edit your question and add some HTML examples where these classes appear and what exactly you want to remove. Commented Jul 7, 2018 at 13:59
  • Actually, I want to remove these classes from the Post Title, generated at the front end where I don't have any jQuery. Question Updated with more specific details. Thanks :) Commented Jul 7, 2018 at 14:01
  • Just for info: WordPress also utilizes jQuery in the frontend. Commented Jul 7, 2018 at 14:38

2 Answers 2

0

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.

4
  • Thanks, Mate but this snippet is causing fatal error resulting in the whole site down. Commented Jul 7, 2018 at 15:05
  • @NarenderChopra I just tested the code and it works fine. Please see phpfiddle.org/lite/code/2qrp-ane1 - Please enable error reporting in your code: error_reporting(E_ALL); ini_set('display_errors', 1); Commented Jul 7, 2018 at 15:27
  • @NarenderChopra I updated the code. I think it might be caused because I forgot the add the $priority and $accepted_args parameters to the add_filter function. Try add_filter('post_class', 'remove_classes', 10, 3); instead. Commented Jul 7, 2018 at 15:31
  • Thanks a ton, Mate. It finally worked. The perfect solution for my need :) Commented Jul 8, 2018 at 4:03
0

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 = '';
  }
6
  • Thanks for the quick answer. Actually, I am looking for a code that I can add to my theme's functions.php to remove these extra classes. I don't want to edit core Wordpress files :) Commented Jul 7, 2018 at 12:28
  • This is not jquery but php. You simply add it to the filter you already have.
    – cjbj
    Commented Jul 7, 2018 at 12:37
  • I am currently using the following function. May you please let me know where to add the strings provided by you: ` 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); ` Commented Jul 7, 2018 at 12:47
  • Insert it before return $classes;
    – cjbj
    Commented Jul 7, 2018 at 12:50
  • But if you have no idea what your filter actually does it might help to learn a little bit more about php.
    – cjbj
    Commented Jul 7, 2018 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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