0

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?

Richzendy
  • 31
  • 8

2 Answers2

1

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()

brasofilo
  • 25,496
  • 15
  • 91
  • 179
wedi
  • 1,332
  • 1
  • 13
  • 28
  • +1 Nice catch with `post_class()`, unfortunately we can never be sure if the theme author added it... Nonetheless, there's nothing much that can be done. – brasofilo Apr 13 '14 at 23:47
  • This work, but post_class add a name class to
    – Richzendy Apr 14 '14 at 02:07
  • You can use too the_content filter: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content This filter permit do format to post and add anything, the_content make my day, thanks a lot to all for your Answers – Richzendy Apr 14 '14 at 02:47
  • That is my code right now using the_content filter: function roll_over_link_class($content){ $str = '"; return $newcontent; } add_filter( 'the_content', 'roll_over_link_class' ); – Richzendy Apr 14 '14 at 03:20
0

Use the body class filter. This will add a class to the body element.

E.g.

function wpse_plugin_add_body_class( $classes ) {
    if ( my_plugin_conditional() ) {
        $classes[] = 'my-plugin-class';
    }

    return $classes; 
}
add_filter( 'body_class', 'wpse_plugin_add_body_class' );

Be sure to change the conditional function to whatever you need. Then add your CSS like so:

.my-plugin-class #content {
    color: #ff0000;
}
Nathan Dawson
  • 18,138
  • 3
  • 52
  • 58