Let topics support gadgets
WordPress Gadget is a feature that allows users to freely drag the combination of content, and any plug-ins and themes can add an extra gadget to increase extensibility.
By default, a theme does not support gadgets, and requires the theme developer to enable gadget functionality and call the gadget at the appropriate foreground location so that the user can drag the side bar directly in the background.
This article will teach you how to activate the gadget feature and add a sidebar, which is displayed at the front end.
Register Side Bar
The default, background appearance is not a "gadget" menu button, if you want to let him appear, you need to register at least a sidebar, otherwise, even if the display, also no use.
Register a sidebar need to use the Register_sidebar () function, the usage is simpler, only one attribute, fill in the required information on the line.
Register_sidebar (Array (
' name ' => __ (' Default sidebar ', ' Bing '),//Side name
' id ' => ' widget_default ',//side sidebar ID, When registering multiple sidebar, do not repeat
' description ' => __ (' sidebar description ', ' Bing '),//Sidebar description, will be displayed in the background
' before_widget ' => ' <div class = "Widget%2$s" >,//Sidebar opening code, you can use%2$s in the inside to invoke the ID of the gadget, to add different styles to each gadget
' after_widget ' => ' </div> ', The end of the gadget in the sidebar code '
before_title ' => '
Using the above example code, you can create a sidebar, the appearance of the "Gadget" button is also displayed.
In this sidebar, users are free to add gadgets. Copy the code, and you can create more side bars.
Call Side bar
Light registration is meaningless, the user added, but also the gadget displayed in the foreground can be, this use of the Dynamic_sidebar () function.
In general, we need to first determine whether the gadget region has added gadgets, if added, display gadgets, or prompts the user to add gadgets. It is necessary to use the Is_active_sidebar () function to determine whether a gadget is added to a sidebar.
<?php
if (Is_active_sidebar (' Widget_default ')) {
echo ' <aside id= ' sidebar ' > ';
Dynamic_sidebar (' Widget_default ');
Echo ' </aside> ';
} else{
Echo ' <p> please set Gadget </p> ';
>
Very simple, after the call is good, the foreground of the gadget will be registered in accordance with the format of a single output.
Add plug-in Enable function
Register_activation_hook () allows you to add a function that executes when the specified plug-in is enabled and is typically used for plug-in development.
Usage
Register_activation_hook ($file, $function);
Parameters
$file
(string) (must) the main file path of the plug-in that needs to execute the function when enabled, and if it is the current plug-in, write __file__ directly.
$function
(The callback function) (must) execute the function.
Example
function Bing_myplugin_activate () {
global $wpdb;
$wpdb->query (");//create some data tables
}
register_activation_hook (__file__, ' bing_myplugin_activate ');
Other
This function is located at: wp-includes/plugin.php