//Create a function called "wporg_init" if it doesn't already exist
if ( !function_exists( 'wporg_init' ) ) {
function wporg_init() {
register_setting( 'wporg_settings', 'wporg_option_foo' );
}
}
//Create a function called "wporg_get_foo" if it doesn't already exist
if ( !function_exists( 'wporg_get_foo' ) ) {
function wporg_get_foo() {
return get_option( 'wporg_option_foo' );
}
}
面向对象的方法
解决命名冲突问题的一种更简单的方法是为插件的代码使用一个类。
但仍然需要检查的类的名称是否已经被占用,其余的将由 PHP 处理。
示例:
if ( !class_exists( 'WPOrg_Plugin' ) ) {
class WPOrg_Plugin
{
public static function init() {
register_setting( 'wporg_settings', 'wporg_option_foo' );
}
public static function get_foo() {
return get_option( 'wporg_option_foo' );
}
}
WPOrg_Plugin::init();
WPOrg_Plugin::get_foo();
}
/**
* Plugin Name: My Basics Plugin
* Plugin URI: https://example.com/plugins/the-basics/
* Description: Handle the basics with this plugin.
* Version: 1.10.3
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: John Smith
* Author URI: https://author.example.com/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Update URI: https://example.com/my-plugin/
* Text Domain: my-basics-plugin
* Domain Path: /languages
*/
/**
* Plugin Name
*
* @package PluginPackage
* @author Your Name
* @copyright 2019 Your Name or Company Name
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: Plugin Name
* Plugin URI: https://example.com/plugin-name
* Description: Description of the plugin.
* Version: 1.0.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Your Name
* Author URI: https://example.com
* Text Domain: plugin-slug
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Update URI: https://example.com/my-plugin/
*/
授权示例:
/*
{Plugin Name} is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
{Plugin Name} is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with {Plugin Name}. If not, see {URI to Plugin License}.
*/
/**
* 注册一个“book”的自定义类型
*/
function pluginprefix_setup_post_type() {
register_post_type( 'book', ['public' => true ] );
}
add_action( 'init', 'pluginprefix_setup_post_type' );
/**
* 激活插件.
*/
function pluginprefix_activate() {
// Trigger our function that registers the custom post type plugin.
pluginprefix_setup_post_type();
// Clear the permalinks after the post type has been registered.
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'pluginprefix_activate' );
禁用示例:
/**
* Deactivation hook.
*/
function pluginprefix_deactivate() {
// Unregister the post type, so the rules are no longer in memory.
unregister_post_type( 'book' );
// Clear the permalinks to remove our post type's rules from the database.
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'pluginprefix_deactivate' );
// if uninstall.php is not called by WordPress, die
if (!defined('WP_UNINSTALL_PLUGIN')) {
die;
}
$option_name = 'wporg_option';
delete_option($option_name);
// for site options in Multisite
delete_site_option($option_name);
// drop a custom database table
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}mytable");
$args An array that describes the activity item that you’re creating. Possible values:
'id' (optional) Pass a numerical id to update an existing activity item
'action' An HTML string summarizing the activity item, which is used by the template when displaying the activity item. Usually contains links to related users, groups, etc. E.g., '<a href="http://example.com/members/bill">Bill</a> created the group <a href="http://example.com/groups/tmnt">TMNT</a>'
'content' (optional) The string content of the activity item. In the case of a blog post, for example, you might use an excerpt from the post content. In some cases, it’s appropriate for activity items not to have any content for this field – e.g., when a user creates a new group.
'component' A string denoting the unique “component” that this activity item is associated with. BuddyPress components are ‘groups’, ‘members’, etc. If you are writing a BuddyPress plugin, you might consider having a single component name for all activity in your plugin.
'type' A string denoting the specific kind of activity being created. This should be more specific than 'component'. For example, when creating a group, BuddyPress posts an activity item with the component ‘groups’ and the type ‘created_group’. 'type' is used for filtering, as in the “New Groups”, “New Blog Posts”, etc dropdown on Activity directories.
'primary_link' (optional) The primary URL associated with the activity item. BuddyPress uses this value when creating activity RSS feeds, to tell the feed reader where to find the original item. If you omit this value, the fallback value is the permalink page for the single activity item.
'user_id' (optional) The unique numeric id of the user associated with the activity item. In BuddyPress, the primary use of this value is to filter which items appear on the Activity tab of an individual member’s profile. If the value is omitted, it will default to the id of the currently logged-in user. Note that you can pass 0 for items that may be associated with no user at all.
'item_id' (optional) The numeric ID of the primary item associated with this activity item. For example, when recording the creation of a group, BuddyPress sets the 'item_id' to the numeric id of the newly created group. This value can be used for filtering.
'secondary_item_id' (optional) A second numeric ID for further filtering. For example, when recording a new blog comment, BuddyPress sets the 'item_id' to the ID of the blog, and the 'secondary_item_id' to the ID of the comment.
'recorded_time' (optional) The time the activity is recorded. Should be in GMT, MySQL format. (In PHP, date( 'Y-m-d H:i:s', $time ).) Defaults to the current time.
'hide_sitewide' (optional)'hide_sitewide' is used in a few different ways:
To prevent duplicate entries when viewing the sitewide activity stream. For instance, when a new friendship is established, two activity items are created: one with 'user_id' set to the user who sent the request and 'item_id' set to recipient, and the other one vice versa. We need both items so that both users see the item on their own profiles, but it’s not desirable to see both of them on the sitewide stream, so the second one is marked 'hide_sitewide' => true.
To prevent activity items from hidden or private groups from appearing in sitewide activity streams. That is, when an activity item is posted to a non-public group, 'hide_sitewide' is set to false. Then, when viewing the group’s activity stream (which is accessible only to members), BP includes 'hide_sitewide' items; in contrast, the sitewide stream excludes these items.
Thus, 'hide_sitewide' is used simultaneously for preventing duplicates and for privacy. Please be sure you understand the way that 'hide_sitewide' works before attempting to use it for the purposes of privacy in your own plugins.
'is_spam' (optional) Set to true to mark an item as spam.