WordPress插件製做筆記(二)---Second Plugins Demo

1->插件演示代碼:下載地址:http://pan.baidu.com/s/1gd1lFlLphp

在 wordpress/wp-content/plugins/ 目錄下 新建一個文件夾取名爲second_plugins_demo(或本身定義),在新建的second_plugins_demo目錄下新建second_plugins_demo.php(自定義.php但要求與父級目錄名一直)文件,將如下代碼拷貝到second_plugins_demo.php文件中保存,刷新wordpress後臺-插件菜單-已安裝的插件,啓用second_plugins_demo 插件便可css

目錄示例:*/wordpress/wp-content/plugins/second_plugins_demo/second_plugins_demo.phphtml

<?php 
/**
*Plugin Name: Second Plugins Demo
*Plugin URI: http://www.cnblogs.com/fxmbz/p/4030286.html
*Description: Second Plugins Demo, This plugin shows a line end of welcome information on the content, add the Settings menu button In the Settings men
*Version: 1.0
*Author: Zhangxl
*Author URI: http://www.cnblogs.com/fxmbz
*License: GPL
*Text Domain: 29583035
*/

/**
 * add_action funtions not exists exit and prompt
 */
if ( !function_exists( 'add_action' ) ) {
    echo 'Hi there!  I\'m just a plugin, not much I can do when cwholeed directly.';
    exit;
}
/**
 * [add_action($hook,$function)]
 * [add_css_link add css link]
 */
add_action( 'wp_head','add_css_link' );
function add_css_link() {
    $styleUrl = plugin_dir_url( __FILE__ ) . 'css/style.css';
    echo "<link rel='stylesheet' href='$styleUrl'>";
}

/**
 * [register_activation_hook run set_text_options]
 */
register_activation_hook( __FILE__, 'set_text_options' );

/**
 * [register_deactivation_hook run del_text_options]
 */
register_deactivation_hook( __FILE__, 'del_text_options' );

/**
 * [set_text_options insert data in the options table]
 */
function set_text_options() {
    add_option( 'display_text_thearticle', '<p id="display_text_thearticle">Welcome to second plugin testing! This plugin is only added word at the end of the article. author: Zhangxl, Email: 29583035@qq.com</p>' );
}

/**
 * [del_text_options delete data in the options table]
 */
function del_text_options() {
    delete_option( 'display_text_thearticle' );
}

/**
 * [add_action add text to the content end]
 * [display_text is single show content and new add infomation]
 * @param  [type] $content [the source article content]
 * @return [type]          [string]
 */
add_action('the_content','display_text');
function display_text($content) {
    if ( is_single() ) {
        $content = $content . get_option( 'display_text_thearticle' );
        return $content;
    } else {
        return $content;
    }
}

/**
 * is admin page run [add_action($hook,$function) ]
 */
if ( is_admin() ) {
    add_action('admin_menu','display_text_menu');
}

/**
 * [display_text_menu add admin menu settings page]
 * @return [type] [description]
 */
function display_text_menu() {
     // add_options_page( $page_title, $menu_title, $capability(權限), $menu_slug(URL-friendly name), $function );
    add_options_page('Text Settings', 'Text Settings', 'manage_options', 'display_text_thearticle','display_text_html_page');
}

/**
 * [display_text_html_page settings page form]
 * @return [type] [description]
 */
function display_text_html_page() {
?>
<div>   
    <h2>Settings Version information</h2>   
    <form method="post" action="options.php">   
        <!-- 下面這行代碼用來保存表單中內容到數據庫(官網的方法) -->
        <?php wp_nonce_field( 'update-options' ); ?>   
        <p>   
            <textarea cols="80" rows="6" name="display_text_thearticle" id="display_text_thearticle" ><?php echo get_option('display_text_thearticle'); ?></textarea>
        </p>
        
        <!-- 下面這兩個隱藏字段爲必須,其中第二個字段的值爲要修改的字段名(官網的方法) -->
        <p>
            <input type="hidden" name="action" value="update" />   
            <input type="hidden" name="page_options" value="display_text_thearticle" />
            <input type="submit" value="Save Changes" class="button-primary" />
        </p>
    </form>
</div>
<?php }

?>
相關文章
相關標籤/搜索