Search code examples
wordpresswordpress-theminglinkify

add css class or id before a wordpress post/page


i'm building a wordpress plugin, but to my plugin work needed a specific class in to html code (javascript linkify), but all templates themes are using different classes to formating the posts, for example:

<div class="post_content">

or

    <div class="content">

How i can embebed through my plugin my own specific div class to identify a post or page?


Solution

  • Checkout the Codex for body_class() and post_class(). If the theme supports it (and every well written theme should support it) you can add your own classes to the body or post.

    function my_plugin_class($classes) {
        if ( my_plugin_is_loaded() ) {
            $classes[] = 'my-plugin-class';
        }
        return $classes;
    }
    add_filter('post_class', 'my_plugin_class');    // add the class to the post content
    add_filter('body_class', 'my_plugin_class');    // add the class to the body tag
    

    You can differ between pages and posts with is_page() and is_single()