WordPress enables themes to support gadgets as well as add plug-in enablement functions, _php Tutorial

Source: Internet
Author: User

WordPress enables the theme to support gadgets and add plug-in enable functions,


Let the theme support gadgets
WordPress Gadget (widget) is a feature that allows users to freely drag the composition of content, and any plug-ins and themes can add an extra gadget, increase extensibility.

By default, a theme does not support gadgets and requires the theme developer to enable the gadget feature and call the gadget in the appropriate foreground location so that users can drag the build sidebar directly in the background.

This article will teach you how to activate the gadget feature, add a sidebar, and finally show it in the foreground.

Register Sidebar

The default, background appearance is no "gadget" this menu button, if you want to let him appear, at least need to register a sidebar, or even if displayed, it is not used.

Registering a sidebar requires the use of the Register_sidebar () function, which is simple, with only one attribute, and the required information is filled in.

Register_sidebar (' name ' = ' + ' (' Default sidebar ', ' Bing '),///side name ' id ' = ' widget_default ',//' side bar ' ID, register multiple sidebar without repeating ' Description ' + __ (' sidebar description ', ' Bing '),///Sidebar description, will be displayed in the background ' before_widget ' = ', '//' the start code of gadgets in the sidebar, can use%2$s in the inside to invoke the gadget I D, implement to add different styles to each gadget ' after_widget ' = ', '//The end code of widgets in sidebar ' before_title ' = '

',///The sidebar in the beginning of the title of the gadget's code ' after_title ' = '

'///side bar with the end code of the gadget's title);

Using the example code above, you can create a sidebar that also displays the gadget button under the skin.

In this sidebar, users are free to add gadgets. You can also create more sidebar by copying the code.

Call the side bar

Light registration is meaningless, after the user added, but also to display the gadget in the foreground, which is used in the Dynamic_sidebar () function.

In general, we need to determine whether a gadget is added to the gadget area, or if it is added, the gadget is displayed, or the user is prompted to add a gadget. The Is_active_sidebar () function is used to determine if a widget is added to the sidebar.

<?phpif (Is_active_sidebar (' Widget_default ')) {echo ';  Dynamic_sidebar (' Widget_default '); Echo ';} else{Echo '

Please set the gadget

';}? >

Very simple, after the call, the foreground gadget will follow the registration given the format of one 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 it is enabled, and if it is the current plug-in, write __file__ directly.

$function

(callback function) (the function that must be executed).

Example

function Bing_myplugin_activate () {global $wpdb; $wpdb->query (");//create some data table}register_activation_hook (__file__, ' Bing_myplugin_activate ');

Other

This function is located at: wp-includes/plugin.php

Articles you may be interested in:

    • Fix wordpress feed display error with fix RSS feeds plugin
    • Several plugins to optimize the JavaScript loading experience in WordPress
    • Use the Wp-cron plugin to set up timed tasks in WordPress
    • 8 excellent wordpress SEO Plugin Collection
    • WordPress plugin--coolcode How to use and download
    • Detailed description of WordPress in the reminder to install plug-ins and hidden plug-in function implementation

http://www.bkjia.com/PHPjc/1084564.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084564.html techarticle WordPress makes the theme support widgets and add plug-in enable function, let the theme support Gadget WordPress Gadget (widget) is a big feature, it lets the user freely drag the composition content ...

  • 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.