返回介绍

wp_parse_args()

发布于 2017-09-11 12:33:39 字数 17030 浏览 1027 评论 0 收藏 0

wp_parse_args( string|array|object $args,  array $defaults = '' )

Merge user defined arguments into defaults array.


description

This function is used throughout WordPress to allow for both string or array to be merged into another array.


参数

$args

(string|array|object) (Required) Value to merge with $defaults.

$defaults

(array) (Optional) Array that serves as the defaults.

Default value: ''


返回值

(array) Merged user defined values with defaults.


源代码

File: wp-includes/functions.php

function wp_parse_args( $args, $defaults = '' ) {
	if ( is_object( $args ) )
		$r = get_object_vars( $args );
	elseif ( is_array( $args ) )
		$r =& $args;
	else
		wp_parse_str( $args, $r );

	if ( is_array( $defaults ) )
		return array_merge( $defaults, $r );
	return $r;
}

更新日志

Versiondescription
2.3.0$args can now also be an object.
2.2.0Introduced.

相关函数

Uses

  • wp-includes/formatting.php: wp_parse_str()

Used By

  • wp-includes/category-template.php: get_term_parents_list()
  • wp-includes/class-oembed.php: WP_oEmbed::get_data()
  • wp-includes/widgets/class-wp-widget-media.php: WP_Widget_Media::form()
  • wp-includes/widgets/class-wp-widget-media.php: WP_Widget_Media::widget()
  • wp-includes/widgets/class-wp-widget-media-image.php: WP_Widget_Media_Image::render_media()
  • wp-includes/widgets/class-wp-widget-media.php: WP_Widget_Media::__construct()
  • wp-includes/rest-api.php: register_rest_field()
  • wp-includes/theme.php: wp_update_custom_css_post()
  • wp-includes/class-wp-taxonomy.php: WP_Taxonomy::set_props()
  • wp-includes/class-wp-term-query.php: WP_Term_Query::parse_query()
  • wp-includes/class-wp-term-query.php: WP_Term_Query::query()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::validate_setting_values()
  • wp-includes/class-wp-network-query.php: WP_Network_Query::query()
  • wp-includes/class-wp-network-query.php: WP_Network_Query::parse_query()
  • wp-includes/class-wp-post-type.php: WP_Post_Type::set_props()
  • wp-includes/class-wp-site-query.php: WP_Site_Query::query()
  • wp-includes/class-wp-site-query.php: WP_Site_Query::parse_query()
  • wp-admin/includes/ms.php: network_edit_site_nav()
  • wp-includes/theme.php: get_header_image_tag()
  • wp-includes/rest-api/class-wp-rest-server.php: WP_REST_Server::get_routes()
  • wp-includes/class-wp-comment.php: WP_Comment::get_children()
  • wp-includes/class-wp-user-query.php: WP_User_Query::fill_query_vars()
  • wp-includes/link-template.php: get_the_comments_navigation()
  • wp-includes/link-template.php: get_the_comments_pagination()
  • wp-admin/includes/class-wp-screen.php: WP_Screen::set_screen_reader_content()
  • wp-includes/customize/class-wp-customize-media-control.php: WP_Customize_Media_Control::__construct()
  • wp-includes/class-wp-comment-query.php: WP_Comment_Query::parse_query()
  • wp-includes/link-template.php: get_avatar_data()
  • wp-includes/post.php: get_children()
  • wp-includes/link-template.php: get_the_post_navigation()
  • wp-includes/link-template.php: get_the_posts_navigation()
  • wp-includes/link-template.php: get_the_posts_pagination()
  • wp-includes/class-oembed.php: WP_oEmbed::get_provider()
  • wp-includes/l10n.php: wp_dropdown_languages()
  • wp-admin/includes/class-language-pack-upgrader.php: Language_Pack_Upgrader::bulk_upgrade()
  • wp-admin/includes/class-core-upgrader.php: Core_Upgrader::upgrade()
  • wp-admin/includes/class-plugin-upgrader.php: Plugin_Upgrader::bulk_upgrade()
  • wp-admin/includes/class-theme-upgrader.php: Theme_Upgrader::install()
  • wp-admin/includes/class-theme-upgrader.php: Theme_Upgrader::upgrade()
  • wp-admin/includes/class-theme-upgrader.php: Theme_Upgrader::bulk_upgrade()
  • wp-admin/includes/class-wp-upgrader.php: WP_Upgrader::install_package()
  • wp-admin/includes/class-wp-upgrader.php: WP_Upgrader::run()
  • wp-admin/includes/class-plugin-upgrader.php: Plugin_Upgrader::install()
  • wp-admin/includes/class-plugin-upgrader.php: Plugin_Upgrader::upgrade()
  • wp-admin/includes/class-wp-screen.php: WP_Screen::render_screen_options()
  • wp-admin/includes/class-wp-screen.php: WP_Screen::add_help_tab()
  • wp-admin/includes/export.php: export_wp()
  • wp-admin/includes/class-theme-upgrader-skin.php: Theme_Upgrader_Skin::__construct()
  • wp-admin/includes/class-language-pack-upgrader-skin.php: Language_Pack_Upgrader_Skin::__construct()
  • wp-admin/includes/class-plugin-installer-skin.php: Plugin_Installer_Skin::__construct()
  • wp-admin/includes/class-theme-installer-skin.php: Theme_Installer_Skin::__construct()
  • wp-admin/includes/class-plugin-upgrader-skin.php: Plugin_Upgrader_Skin::__construct()
  • wp-admin/includes/class-bulk-upgrader-skin.php: Bulk_Upgrader_Skin::__construct()
  • wp-admin/includes/class-wp-upgrader-skin.php: WP_Upgrader_Skin::__construct()
  • wp-admin/includes/class-wp-list-table.php: WP_List_Table::__construct()
  • wp-admin/includes/class-wp-list-table.php: WP_List_Table::set_pagination_args()
  • wp-admin/includes/taxonomy.php: wp_insert_category()
  • wp-includes/option.php: register_setting()
  • wp-admin/includes/template.php: wp_star_rating()
  • wp-admin/includes/template.php: wp_terms_checklist()
  • wp-admin/includes/media.php: get_media_item()
  • wp-admin/includes/media.php: get_compat_media_markup()
  • wp-admin/includes/ajax-actions.php: wp_ajax_query_themes()
  • wp-admin/includes/bookmark.php: wp_insert_link()
  • wp-admin/includes/meta-boxes.php: post_tags_meta_box()
  • wp-admin/includes/meta-boxes.php: post_categories_meta_box()
  • wp-admin/includes/class-wp-terms-list-table.php: WP_Terms_List_Table::display_rows_or_placeholder()
  • wp-includes/category-template.php: wp_tag_cloud()
  • wp-includes/category-template.php: wp_generate_tag_cloud()
  • wp-includes/category-template.php: wp_dropdown_categories()
  • wp-includes/category-template.php: wp_list_categories()
  • wp-includes/theme.php: add_theme_support()
  • wp-includes/theme.php: get_custom_header()
  • wp-includes/theme.php: wp_get_themes()
  • wp-includes/pluggable.php: get_avatar()
  • wp-includes/pluggable.php: wp_text_diff()
  • wp-includes/general-template.php: paginate_links()
  • wp-includes/general-template.php: feed_links()
  • wp-includes/general-template.php: feed_links_extra()
  • wp-includes/general-template.php: wp_get_archives()
  • wp-includes/general-template.php: wp_login_form()
  • wp-includes/deprecated.php: wp_get_links()
  • wp-includes/deprecated.php: wp_get_linksbyname()
  • wp-includes/deprecated.php: wp_list_cats()
  • wp-includes/class-wp-query.php: WP_Query::query()
  • wp-includes/class-wp-query.php: WP_Query::parse_query()
  • wp-includes/category.php: get_categories()
  • wp-includes/class-wp-http-streams.php: WP_Http_Streams::request()
  • wp-includes/class-wp-http-curl.php: WP_Http_Curl::request()
  • wp-includes/class-http.php: WP_Http::post()
  • wp-includes/class-http.php: WP_Http::get()
  • wp-includes/class-http.php: WP_Http::head()
  • wp-includes/class-http.php: WP_Http::request()
  • wp-includes/functions.php: _default_wp_die_handler()
  • wp-includes/functions.php: _xmlrpc_wp_die_handler()
  • wp-includes/functions.php: _ajax_wp_die_handler()
  • wp-includes/widgets/class-wp-widget-categories.php: WP_Widget_Categories::form()
  • wp-includes/widgets/class-wp-widget-calendar.php: WP_Widget_Calendar::form()
  • wp-includes/widgets/class-wp-widget-text.php: WP_Widget_Text::form()
  • wp-includes/widgets/class-wp-widget-search.php: WP_Widget_Search::form()
  • wp-includes/widgets/class-wp-widget-search.php: WP_Widget_Search::update()
  • wp-includes/widgets/class-wp-widget-archives.php: WP_Widget_Archives::update()
  • wp-includes/widgets/class-wp-widget-archives.php: WP_Widget_Archives::form()
  • wp-includes/widgets/class-wp-widget-meta.php: WP_Widget_Meta::form()
  • wp-includes/widgets/class-wp-widget-pages.php: WP_Widget_Pages::form()
  • wp-includes/widgets/class-wp-widget-links.php: WP_Widget_Links::form()
  • wp-includes/widgets.php: wp_widget_rss_output()
  • wp-includes/widgets.php: wp_widget_rss_form()
  • wp-includes/class-wp-embed.php: WP_Embed::shortcode()
  • wp-includes/taxonomy.php: the_taxonomies()
  • wp-includes/taxonomy.php: get_the_taxonomies()
  • wp-includes/taxonomy.php: wp_update_term()
  • wp-includes/taxonomy.php: wp_get_object_terms()
  • wp-includes/taxonomy.php: wp_insert_term()
  • wp-includes/taxonomy.php: wp_count_terms()
  • wp-includes/taxonomy.php: wp_delete_term()
  • wp-includes/taxonomy.php: get_terms()
  • wp-includes/taxonomy.php: register_taxonomy()
  • wp-includes/taxonomy.php: get_objects_in_term()
  • wp-includes/link-template.php: paginate_comments_links()
  • wp-includes/link-template.php: get_posts_nav_link()
  • wp-includes/class-wp-ajax-response.php: WP_Ajax_Response::add()
  • wp-includes/class-wp-admin-bar.php: WP_Admin_Bar::add_node()
  • wp-includes/http.php: wp_http_supports()
  • wp-includes/class-oembed.php: WP_oEmbed::fetch()
  • wp-includes/user.php: get_users()
  • wp-includes/user.php: wp_dropdown_users()
  • wp-includes/bookmark-template.php: _walk_bookmarks()
  • wp-includes/bookmark-template.php: wp_list_bookmarks()
  • wp-includes/nav-menu-template.php: wp_nav_menu()
  • wp-includes/post-template.php: wp_link_pages()
  • wp-includes/post-template.php: wp_dropdown_pages()
  • wp-includes/post-template.php: wp_list_pages()
  • wp-includes/post-template.php: wp_page_menu()
  • wp-includes/post-template.php: the_title_attribute()
  • wp-includes/media.php: wp_enqueue_media()
  • wp-includes/media.php: wp_get_attachment_image()
  • wp-includes/post.php: wp_insert_attachment()
  • wp-includes/post.php: get_pages()
  • wp-includes/post.php: wp_get_post_categories()
  • wp-includes/post.php: wp_get_post_terms()
  • wp-includes/post.php: wp_get_recent_posts()
  • wp-includes/post.php: wp_insert_post()
  • wp-includes/post.php: get_posts()
  • wp-includes/post.php: register_post_status()
  • wp-includes/class-wp-rewrite.php: WP_Rewrite::add_permastruct()
  • wp-includes/revision.php: wp_get_post_revisions()
  • wp-includes/ms-deprecated.php: wp_get_sites()
  • wp-includes/ms-functions.php: wpmu_create_blog()
  • wp-includes/bookmark.php: get_bookmarks()
  • wp-includes/author-template.php: wp_list_authors()
  • wp-includes/nav-menu.php: wp_update_nav_menu_item()
  • wp-includes/nav-menu.php: wp_get_nav_menus()
  • wp-includes/nav-menu.php: wp_get_nav_menu_items()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_insert_post()
  • wp-includes/customize/class-wp-customize-image-control.php: WP_Customize_Image_Control::__construct()
  • wp-includes/class-wp-widget.php: WP_Widget::display_callback()
  • wp-includes/class-wp-widget.php: WP_Widget::form_callback()
  • wp-includes/class-wp-widget.php: WP_Widget::__construct()
  • wp-includes/widgets.php: the_widget()
  • wp-includes/widgets.php: register_sidebar()
  • wp-includes/widgets.php: wp_register_sidebar_widget()
  • wp-includes/widgets.php: wp_register_widget_control()
  • wp-includes/widgets.php: _register_widget_form_callback()
  • wp-includes/comment-template.php: get_post_reply_link()
  • wp-includes/comment-template.php: wp_list_comments()
  • wp-includes/comment-template.php: comment_form()
  • wp-includes/comment-template.php: get_comment_reply_link()
  • wp-includes/comment-template.php: get_comment_link()
  • wp-includes/class-wp-comment-query.php: WP_Comment_Query::query()
  • wp-includes/comment.php: get_page_of_comment()
  • wp-includes/meta.php: register_meta()
  • wp-includes/comment.php: get_approved_comments()
  • wp-includes/class-wp-editor.php: _WP_Editors::parse_settings()
  • Show 169 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 3You must log in to vote on the helpfulness of this note Contributed by Benjiwp

    Thank you bhlarsen for this function but there is a little mistake:

    if (!$echo) 

    must be replaced by

    if (!$args['echo'])

  2. Below is an example function using the wp_parse_args system to manage its single $args argument, which could be given whatever values you wanted.
    In this case $args stores detailed display overrides, a pattern found in many WordPress functions.

    
    /**
     * Define a new function that uses $args and wp_parse_args()
     */
    function wpdocs_explain_parse_args( $args ) {
    	$defaults = array (
    		'text' => 'wp_parse_args() merges $args into $defaults',
    		'before' => "<p>",
    		'after' => "</p> \n",
     		'echo' => TRUE
    	);
    	
    	// Parse incoming $args into an array and merge it with $defaults
    	$args = wp_parse_args( $args, $defaults );
    	
    	$output = $args['before'] . $args['text'] . $args['after'];
    	
    	if (!$echo) 
    		return $output;
    	
    	echo $output;
    }
    
    /**
     * Run our new function using the defaults (no $args)
     * This would print out: 
     * 	<p>wp_parse_args() merges $args into $defaults</p>
     */
    wpdocs_explain_parse_args();
    
    /**
     * Run the function with some options overridden with an array
     * This would echo the output with the modified text and before arguments:
     * 	<p class='specialclass'>A better explanation</p>
     */
    wpdocs_explain_parse_args( array (
    	'text' => "A better explanation",
    	'before' => "<p class='specialclass'>"
    ) );
    
    /**
     * We can also pass in URL-style string-query and it will be converted
     * This would set $args['echo'] to 1 and $args['text'] to 0	
     */
    wpdocs_explain_parse_args( 'echo=1&text=0' );
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文