WordPress to add categories and tags to media files php function to achieve _php skills

Source: Internet
Author: User

From the WordPress background Media Library upload media files, not like the article can give it to specify categories and tags, but many times we need such a function, such as some download stations, picture stations and so on.

The original state of the media edit page

Obviously, in the WordPress background of the media editing page, by default there is no classification and label for you to choose.

Adding categories to media files

Add the following PHP code to the functions.php of the current topic:

function ludou_add_categories_to_attachments () {
  register_taxonomy_for_object_type (' category ', ' attachment ');
}
add_action (' init ', ' ludou_add_categories_to_attachments ');

Save and upload functions.php, refresh the media edit page, you will see the right column more than a category directory:

Tagging media files

Add the following code to the functions.php of the current topic:

function ludou_add_tags_to_attachments () {
  register_taxonomy_for_object_type (' Post_tag ', ' attachment ');
}
Add_action (' init ', ' ludou_add_tags_to_attachments ');

Save and upload the functions.php, refresh the media editing page, you will see the right side of the bar more than a tab bar:

At the same time, in the Multimedia list page, also add the Display media category and label, also the left column of the multimedia menu under the Category directory and label two submenu:

Well, adding categories and labels to media files is simple, and we can synthesize the code added above to make it simpler and more efficient:

function ludou_add_categories_tags_to_attachments () {
  register_taxonomy_for_object_type (' Category ', ' Attachment ');
  Register_taxonomy_for_object_type (' Post_tag ', ' attachment ');
}
Add_action (' init ', ' ludou_add_categories_tags_to_attachments ');

Another way

In fact, there is a way to achieve, and can be implemented to add independent classification of multimedia, rather than mixing the classification of articles, and in the WordPress background of the Multimedia list page can visually display the classification of the media.

Using the method, you also add PHP code to the functions.php of the current topic:

function Ludou_create_media_category () {
 $args = array (
  ' label ' => ' Media classification ', '
  hierarchical ' => true,
  ' Show_admin_column ' => true,
  ' show_ui '   => true,
  ' Query_var '  => true,
  ' rewrite '   => True,
 );

 Register_taxonomy (' attachment_category ', ' attachment ', $args);
}

Add_action (' init ', ' ludou_create_media_category ');

If you do not reject the use of plug-ins, recommend this plugin: media Library Categories, you can achieve the bulk of the classification of the file changes.

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.