Lamp development essentials (4): WordPress plug-in example 2-reconstruction of Example 1

Source: Internet
Author: User

Example 1 is ready to work, which has been confirmed in WP 2.3.3 and 2.9.1.

However, Example 1 has several defects.

Due to the large number of WP plug-ins, many programmers in the world, including our programmers in China, are developing plug-ins for them. These plug-ins are often installed together in a set of WP systems for coordination, this will inevitably lead to conflicts.

To isolate the code of each plug-in, we need to find some ways. Encapsulation of plug-in code in a class structure is one of the effective methods. Correct Application of Some detection functions ensures that as long as the plug-in class name does not conflict, the member functions contained in the class will not conflict with other plug-ins. This greatly enhances the robustness of the plug-in code. Whether the plug-in class names conflict with each other is also part of the code Fault Tolerance detection, so that the system will not crash seriously.

Please refer to the following complete WP plug-in code. Refer to the above considerations to completely refactor the code in Example 1.

<? PHP
/*
Plugin name: mycopyright
Plugin URI: http://www.why100000.com
Version: 0.1
Author: mesh (Zhang Qing-Xi'an, Shaanxi-kaikai studio-http://www.myopenit.cn)
Author URI: http://blog.why100000.com
Description: Set the string "& lt ;! -- Mycopyrigth -- & gt; "is replaced with the copyright information: Show copyright once there are letters match" & lt ;! -- Mycopyri.pdf -- & gt ;".
You shoshould config your copyright information in this file, with this verison.
*/

If (! Class_exists ("why100000_plugin "))
{
Class why100000_plugin
{
// Constructor
Function why100000_plugin ()
{
}

// Add it to the "Settings" Main Menu
Function why100000_add_options_page ()
{
Add_options_page ('mycopyright', 'mycopyright-config', 8, 'mycopyright. php', array (& $ this, 'why100000 _ mycopyright_mainpage '));
}

Function why100000_mycopyright_mainpage ()
{
?>
<Form name = "formamt" method = "Post" Action = "options. php">
<? PHP wp_nonce_field ('Update-options')?>
<Div class = "Wrap">
Mycopyright is active. <br>
<H2> mycopyright config (Copyright plug-in configuration) </H2>

<Div id = "msgall">
<Fieldset class = "options">
<Legend> keyword (original string): </legend>
<P>
<Textarea style = "width: 80%;" rows = "5" Cols = "40" name = "why100000_keyword"> <? PHP echo get_option ("why100000_keyword");?> </Textarea>
</P>
</Fieldset>

<Fieldset class = "options">
<Legend> replacement (replacement string): </legend>
<P>
<Textarea style = "width: 80%;" rows = "5" Cols = "40" name = "why100000_replace"> <? PHP echo get_option ("why100000_replace");?> </Textarea>
</P>
</Fieldset>
</Div>

<P class = "Submit">
<Input type = "Submit" value = "<? PHP _ E ('Update options & raquo; ')?> "Name =" update_message "/>
</P>
<Input type = "hidden" name = "action" value = "Update">
<Input type = "hidden" name = "page_options" value = "why100000_keyword, why100000_replace">
</Div>
</Form>
<? PHP
}

// Execute when the plug-in is "enabled" for the first time and save it to the table wp_options as the initial value
Function why100000_copyright_install ()
{
Add_option ("why100000_keyword", "<! -- Mycopyright --> ");
Add_option ("why100000_replace", "My version ");
}

// WP calls the the_content function to filter the content of the article.
Function why100000_showcopyright ($ content)
{
$ Search = get_option ("why100000_keyword"); // "<! -- Mycopyright --> ";
$ Replace = get_option ("why100000_replace"); // Replace the string;
$ Content = str_replace ($ search, $ replace, $ content );
Return $ content;
}

}
}

If (class_exists ("why100000_plugin "))
{
$ Myplugin = new why100000_plugin ();
}

If (isset ($ myplugin ))
{
Add_action ('activate _ mycopyright/mycopyright. php', array (& $ myplugin, 'why100000 _ copyright_install '));
Add_action ('admin _ menu ', array (& $ myplugin, 'why100000 _ add_options_page '));
Add_filter ('the _ content', array (& $ myplugin, 'why100000 _ showcopyright '));
}
?>

Therefore, we have summarized a relatively complete WP plug-in code writing architecture:

<? PHP
/* Plug-in comments ......*/

/* Plug-in code :*/

/* Plug-in class definition :*/
If (! Class_exists ("plug-in class name "))
{
Class plug-in Class Name
{
/* All plug-in functions ......*/
}
}

/* Instantiate the plug-in class :*/
If (class_exists ("plug-in class name "))
{
$ Myplugin = new plug-in Class Name ();
}

/* Define plug-in event functions :*/
If (isset ($ myplugin ))
{
Add_action ('activate _... php', array (& $ myplugin ,...));
Add_action ('admin _ menu ', array (& $ myplugin ,...));
Add_filter ('the _ content', array (& $ myplugin ,...));
}
?>

Author: Zhang Qing (mesh) 2010-4-5
From mesh horizon: http://blog.why100000.com
Why 100,000 computer learning networks: http://www.why100000.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.