I choose a new wordpress template some days ago, it looks good. But have some problems like this. I spent about four hours to do this, because I know little about php and had never written wordpress plugins. Now I think it is very interesting to write the plugin. So I want to introduce it.
Where to get start?
First, I know I will filter the source, so I regist the filter with this function:
add_filter('the_content','filter_shrink'); |
The “the_content” is the filter’s name, and filter_shrink is my filter function, I will write it later. Then I should could change the width and height from option page, so I write:
add_action('admin_menu', 'flash_zoom_options_page'); |
This function told wordpress I will regist a menu in “admin_menu” with the flash_zoom_options_page function.
implements the functions
Now let’s see the filter_shrink:
function replaceSize($matches) { global $flashzoom_settings; $replaceStr = ' width="'.$flashzoom_settings['target_width'].'" height="'.$flashzoom_settings['target_height'].'" '; return $matches[1].$replaceStr.$matches[3].$replaceStr.$matches[5]; } function filter_shrink($content) { global $flashzoom_settings; if(is_home()){ $content = preg_replace_callback( '|(<'.'object.*)(width="d*"s*height="d*")(.*)(width="d*"s*height="d*")(.*</object>)|', "replaceSize", $content); } return $content; } |
Look at the “$flashzoom_settings”, it is the length and height that stored in wordpress. I just use preg_replace_callback function to replace the origin width and height. The php regexp is strange, especially the usage of “|”.
Then we can see the flash_zoom_options_page function:
function isInteger($input){ return preg_match('@^[-]?[0-9]+$@',trim($input)) === 1; } function flash_zoom_options_page() { add_options_page('FlashZoom', 'FlashZoom', 8, basename(__FILE__), 'flash_zoom_options_subpanel'); } function flash_zoom_options_subpanel() { global $ol_flash, $flashzoom_settings, $_POST, $wp_rewrite; if (current_user_can('activate_plugins')) { // Easiest test to see if we have been submitted to if(isset($_POST['target_width']) || isset($_POST['target_height'])) { if(!isInteger($_POST['target_width'])||!isInteger($_POST['target_height'])){ $ol_flash = "Please input the number!"; }else{ // Now we check the hash, to make sure we are not getting CSRF if(fb_is_hash_valid($_POST['token'])) { if (isset($_POST['target_width'])) { $flashzoom_settings['target_width'] = $_POST['target_width']; update_option('flashzoom_settings',$flashzoom_settings); $ol_flash = "Your settings have been saved."; } if (isset($_POST['target_height'])) { $flashzoom_settings['target_height'] = $_POST['target_height']; update_option('flashzoom_settings',$flashzoom_settings); $ol_flash = "Your settings have been saved."; } } else { // Invalid form hash, possible CSRF attempt $ol_flash = "Security hash missing."; } // endif fb_is_hash_valid } //endif isInteger } // endif isset(feedburner_url) } else { $ol_flash = "You don't have enough access rights."; } if ($ol_flash != '') echo ' <div id="message" class="updated fade"> ' . $ol_flash . '</div> '; if (current_user_can('activate_plugins')) { $temp_hash = fb_generate_hash(); fb_store_hash($temp_hash); echo ' <div class="wrap">'; echo ' <h2>Set Up Flash Zoom Size</h2> '; echo ' <form method="post"> <input name="redirect" type="hidden" value="true" /> <input name="token" type="hidden" value="' . fb_retrieve_hash() . '" /> <table class="form-table" border="0"> <tbody> <tr valign="top"> <th scope="row"><label for="target_width">Target Width</label></th> <td> <input id="target_width" maxlength="4" name="target_width" size="4" type="text" value="' . htmlentities($flashzoom_settings['target_width']) . '" /></td> </tr> <tr valign="top"> <th scope="row"><label for="target_height">Target Height</label></th> <td> <input id="target_height" maxlength="4" name="target_height" size="4" type="text" value="' . htmlentities($flashzoom_settings['target_height']) . '" /></td> </tr> </tbody></table> <input type="submit" value="Save" /> </form> '; echo '</div> '; } else { echo ' <div class="wrap"> Sorry, you are not allowed to access this page.</div> '; } } |
The flash_zoom_options_page function create an panel flash_zoom_options_subpanel, and the first part of flash_zoom_options_subpanel will check the parameters and store it into database, the other part will display the form.
We have not use the install step, so we add these two lines .
add_option('flashzoom_settings',$data,'FlashZoom Options'); $flashzoom_settings = get_option('flashzoom_settings'); |
Last, don’t forget the plugin header, so the wordpress can recognize the plugin. The full plugin is:
/* Plugin Name: Flash Zoom Plugin URI: http://rocksun.cn/flash-zoom/ Description: zoom the flash in homepage. Version: 0.8 Author: Rock Sun Author URI: http://rocksun.cn/ */ add_option('flashzoom_settings',$data,'FlashZoom Options'); $flashzoom_settings = get_option('flashzoom_settings'); function isInteger($input){ return preg_match('@^[-]?[0-9]+$@',trim($input)) === 1; } function flash_zoom_options_page() { add_options_page('FlashZoom', 'FlashZoom', 8, basename(__FILE__), 'flash_zoom_options_subpanel'); } function flash_zoom_options_subpanel() { global $ol_flash, $flashzoom_settings, $_POST, $wp_rewrite; if (current_user_can('activate_plugins')) { // Easiest test to see if we have been submitted to if(isset($_POST['target_width']) || isset($_POST['target_height'])) { if(!isInteger($_POST['target_width'])||!isInteger($_POST['target_height'])){ $ol_flash = "Please input the number!"; }else{ // Now we check the hash, to make sure we are not getting CSRF if(fb_is_hash_valid($_POST['token'])) { if (isset($_POST['target_width'])) { $flashzoom_settings['target_width'] = $_POST['target_width']; update_option('flashzoom_settings',$flashzoom_settings); $ol_flash = "Your settings have been saved."; } if (isset($_POST['target_height'])) { $flashzoom_settings['target_height'] = $_POST['target_height']; update_option('flashzoom_settings',$flashzoom_settings); $ol_flash = "Your settings have been saved."; } } else { // Invalid form hash, possible CSRF attempt $ol_flash = "Security hash missing."; } // endif fb_is_hash_valid } //endif isInteger } // endif isset(feedburner_url) } else { $ol_flash = "You don't have enough access rights."; } if ($ol_flash != '') echo ' <div id="message" class="updated fade"> ' . $ol_flash . '</div> '; if (current_user_can('activate_plugins')) { $temp_hash = fb_generate_hash(); fb_store_hash($temp_hash); echo ' <div class="wrap">'; echo ' <h2>Set Up Flash Zoom Size</h2> '; echo ' <form method="post"> <input name="redirect" type="hidden" value="true" /> <input name="token" type="hidden" value="' . fb_retrieve_hash() . '" /> <table class="form-table" border="0"> <tbody> <tr valign="top"> <th scope="row"><label for="target_width">Target Width</label></th> <td> <input id="target_width" maxlength="4" name="target_width" size="4" type="text" value="' . htmlentities($flashzoom_settings['target_width']) . '" /></td> </tr> <tr valign="top"> <th scope="row"><label for="target_height">Target Height</label></th> <td> <input id="target_height" maxlength="4" name="target_height" size="4" type="text" value="' . htmlentities($flashzoom_settings['target_height']) . '" /></td> </tr> </tbody></table> <input type="submit" value="Save" /> </form> '; echo '</div> '; } else { echo ' <div class="wrap"> Sorry, you are not allowed to access this page.</div> '; } } function replaceSize($matches) { global $flashzoom_settings; $replaceStr = ' width="'.$flashzoom_settings['target_width'].'" height="'.$flashzoom_settings['target_height'].'" '; return $matches[1].$replaceStr.$matches[3].$replaceStr.$matches[5]; } function filter_shrink($content) { global $flashzoom_settings; if(is_home()){ $content = preg_replace_callback( '|('.'<object width="100" height="100" type="application/x-shockwave-flash"></object> |
Related posts:
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.