Your code will run when the plugin is first initiated, which will be before the post data has been set up. Move all of your code from $cat = get_the_category($id);
up to (but not including) add_filter...
inside the function.
Your code will run when the plugin is first initiated, which will be before the post data has been set up. Move all of your code from $cat = get_the_category($id);
up to (but not including) add_filter...
inside the function.