How do wordpress plugins add content?

前端 未结 1 965
甜味超标
甜味超标 2021-01-23 04:24

This may be a weird question. When I add plugins like the Facebook Like Button and Gigpress, they offer options to insert content before or after each single-page blog post. For

相关标签:
1条回答
  • 2021-01-23 05:01

    They are achieving it with Filters, Actions and Hooking into them.

    In your case - with the_content filter ..

    Example ( from codex ) :

    add_filter( 'the_content', 'my_the_content_filter', 20 );
    /**
     * Add a icon to the beginning of every post page.
     *
     * @uses is_single()
     */
    function my_the_content_filter( $content ) {
    
        if ( is_single() )
            // Add image to the beginning of each page
            $content = sprintf(
                '<img class="post-icon" src="%s/images/post_icon.png" alt="Post icon" title=""/>%s',
                get_bloginfo( 'stylesheet_directory' ),
                $content
            );
    
        // Returns the content.
        return $content;
    }
    

    A simpler to understand example :

     add_filter( 'the_content', 'add_something_to_content_filter', 20 );
    
    
     function add_something_to_content_filter( $content ) {
    
                $original_content = $content ; // preserve the original ...
                $add_before_content =  ' This will be added before the content.. ' ;
                $add_after_content =  ' This will be added after the content.. ' ;
                $content = $add_before_content . $original_content  . $add_after_content ;
    
            // Returns the content.
            return $content;
        }
    

    to see this example in action , put it in your functions.php

    This is actually the single most important step to understanding wordpress, and starting to write plugins. If you really are interested , read the links above.

    Also , Open the plugin files you have just mentioned and look for the Filters and Actions...

    0 讨论(0)
提交回复
热议问题