In WordPress, the theme supports gadgets and adding plug-ins to enable functions. _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags wordpress widgets
In WordPress, the theme supports gadgets and adding plug-ins to enable functions ,. In WordPress, themes support gadgets and add plug-ins to enable functions, making theme support gadgets WordPress widgets a major feature, it allows users to freely drag the combined content WordPress to make theme support gadgets and add plug-ins to enable functions,

Make theme support gadgets
WordPress Widgets are a special feature that allows users to freely drag and combine content, and any plug-in and theme can add an additional tool to increase scalability.

By default, a theme does not support gadgets. the theme developer needs to enable the gadgets function and call the gadgets at the corresponding front-end location so that users can drag the widgets in the background to generate a sidebar.

This article will show you how to activate the gadgets function, add a sidebar, and display it at the front end.

Registration sidebar

By default, there is no "gadgets" menu button in the background. to make it appear, you must register at least one Sidebar. Otherwise, it will be useless even if it is displayed.

To register a sidebar, you must use the register_sidebar () function. it is easy to use and has only one attribute. just fill in the required information.

Register_sidebar (array ('name' => _ ('default sidebar ', 'Bing'), // The side name 'id' => 'widget _ default ', // the ID of the sidebar. when registering multiple sidebar, do not repeat 'description' = >_( 'sidebar description', 'Bing '), // The description of the sidebar, the 'Before _ widget '=>' is displayed in the background'

', // The code at the beginning of the tool in the sidebar. you can use % 2 $ s to call the ID of the tool, add different styles for each widget 'after _ components' =>'

', // The end code of the tool in the sidebar 'Before _ title' => '', // code starting with the title of the widget in the sidebar 'after _ title' => ''// code ending with the title of the gadget in the sidebar ));

Using the above example code, you can create a sidebar with the "gadgets" button displayed.

In this sidebar, you can add gadgets as needed. , You can also create more sidebar.

Call sidebar

Registration is meaningless. after you add a widget, you must display it in the foreground. This uses the dynamic_sidebar () function.

In general, we need to first determine whether the gadgets are added in the gadgets area. if the gadgets are added, the gadgets are displayed; otherwise, the user is prompted to add the gadgets. Use the is_active_sidebar () function to determine whether a widget is added in the sidebar.

<? Phpif (is_active_sidebar ('widget _ default') {echo ''; dynamic_sidebar ('widget _ default'); echo'';} Else {echo'

Set gadgets

';}?>

It's very easy. after the call, the front-end gadgets will be output one by one according to the format given during registration.

Add plug-in to enable function
Register_activation_hook () allows you to add a function that is executed when the specified plug-in is enabled and is generally used for plug-in development.

Usage

register_activation_hook( $file, $function );

Parameters

$ File

(String) (required) the main FILE path of the plug-in that executes the function when it is enabled. if it is the current plug-in, write _ FILE _ directly.

$ Function

(Callback function) (required) executed function.

Example

Function Bing_myplugin_activate () {global $ wpdb; $ wpdb-> query (''); // create some data tables} register_activation_hook (_ FILE __, 'Bing _ myplugin_activate ');

Others

This function is located in: wp-uplodes/plugin. php

Articles you may be interested in:
  • Use the Fix Rss Feeds plug-in to Fix WordPress Feed display errors
  • Introduction to several plug-ins that optimize the JavaScript loading experience in WordPress
  • Use the wp-cron plug-in WordPress to set scheduled tasks
  • 8 excellent WordPress SEO plug-ins
  • WordPress Plugin-how to use and download CoolCode
  • Install the plug-in and hide the functions of the plug-in WordPress.

WordPress Widgets are a special feature that allows users to freely drag and combine content...

Related Article

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.