函数文档

register_importer()

💡 云策文档标注

概述

register_importer() 函数用于在 WordPress 中注册一个导入器,允许开发者添加自定义导入工具到管理界面。它通过指定唯一标识符、名称、描述和回调函数来定义导入器。

关键要点

  • 函数用于注册导入器,参数包括 $id(唯一标识符)、$name(名称和标题)、$description(描述)和 $callback(回调函数)。
  • 成功时返回 void,如果 $callback 是 WP_Error 则返回 WP_Error。
  • 必须在 admin_init 或 init 动作钩子中调用,否则可能导致致命错误。

代码示例

add_action( 'admin_init', function () {
    register_importer( 
        'wpdocs-import-the-things',
        'WPDocs Import the Things',
        'Lets you import those things',
        'wpdocs_the_things_importer'
    );
} );

function wpdocs_the_things_importer() {
    // echo the content of the page for when this importer is called. 
    // Just like any other admin page
}

注意事项

确保在 admin_init 或 init 动作钩子中调用 register_importer(),以避免潜在错误。


📄 原文内容

Registers importer for WordPress.

Parameters

$idstringrequired
Importer tag. Used to uniquely identify importer.
$namestringrequired
Importer name and title.
$descriptionstringrequired
Importer description.
$callbackcallablerequired
Callback to run.

Return

void|WP_Error Void on success. WP_Error when $callback is WP_Error.

Source

function register_importer( $id, $name, $description, $callback ) {
	global $wp_importers;
	if ( is_wp_error( $callback ) ) {
		return $callback;
	}
	$wp_importers[ $id ] = array( $name, $description, $callback );
}

Changelog

Version Description
2.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    This function needs to be called inside the admin_init (or init) action, otherwise it will sometimes cause a Fatal Error.

    add_action( 'admin_init', function () {
    	register_importer( 
    		'wpdocs-import-the-things',
    		'WPDocs Import the Things',
    		'Lets you import those things',
    		'wpdocs_the_things_importer'
    	);
    } );
    
    function wpdocs_the_things_importer() {
    	// echo the content of the page for when this importer is called. 
    	// Just like any other admin page
    }