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 '
' . $ol_flash . '
';
if (current_user_can('activate_plugins')) {
$temp_hash = fb_generate_hash();
fb_store_hash($temp_hash);
echo '
';
echo '
Set Up Flash Zoom Size
';
echo '
';
echo '
';
} else {
echo '
Sorry, you are not allowed to access this page.
';
}
}
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 '
' . $ol_flash . '
';
if (current_user_can('activate_plugins')) {
$temp_hash = fb_generate_hash();
fb_store_hash($temp_hash);
echo '
';
echo '
Set Up Flash Zoom Size
';
echo '
';
echo '
';
} else {
echo '
Sorry, you are not allowed to access this page.
';
}
}
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(
'|('.'
Related posts:
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.