如何在WordPress中实现自定义媒体管理功能?

玖比肆 WordPress 30

详细介绍如何使用WordPress代码实现自定义媒体管理功能的方法。

回复

共1条回复 我来回复
  • 玖比肆的头像
    玖比肆 评论

    在WordPress中,自定义媒体管理功能可以通过使用插件或通过编写自己的代码来实现。下面是一个使用自定义代码实现的步骤:

    1. 创建自定义媒体分类和标签

    WordPress的媒体库可以通过分类和标签来组织。你可以通过添加自定义分类和标签来拓展它的功能。为了创建自定义分类和标签,你需要在functions.php文件或者一个独立的插件文件中添加以下代码:

    ```php
    // 创建自定义标签
    function custom_media_categories( $args ) {
    $args['label'] = '自定义标签';
    return $args;
    }
    add_filter( 'taxonomy_labels_media_category', 'custom_media_categories' );

    // 创建自定义分类
    function custom_media_tags( $args ) {
    $args['label'] = '自定义分类';
    return $args;
    }
    add_filter( 'taxonomy_labels_media_tag', 'custom_media_tags' );
    ```

    2. 添加自定义过滤器

    接下来,我们需要添加一个自定义过滤器来将媒体库中的分类和标签筛选器连接到自定义分类和标签。你需要在functions.php或插件文件中添加以下代码:

    ```php
    // 添加自定义过滤器
    function custom_media_filter( $query ) {
    global $pagenow, $current_screen;
    if ( $pagenow == 'upload.php' && $current_screen->post_type == 'attachment' ) {
    $terms = get_terms( array(
    'taxonomy' => array( 'media_category', 'media_tag' ),
    'hide_empty' => false,
    ) );
    $post_vars = array( 'attachment-filter' => 1 );
    foreach ( $terms as $term ) {
    $post_vars[ 'tax_input[' . $term->taxonomy . '][]' ] = $term->slug;
    }
    wp_redirect( add_query_arg( $post_vars, admin_url( $pagenow ) ) );
    exit();
    }
    }
    add_action( 'admin_init', 'custom_media_filter' );
    ```

    3. 自定义列

    创建自定义列我们可以给媒体库添加自定义列来更好地管理媒体文件。你可以在functions.php或插件文件中添加以下代码:

    ```php
    // 添加自定义列
    function custom_media_columns( $posts_columns )
    {
    // 添加自定义分类列
    $posts_columns['media_category'] = __( '自定义分类' );

    // 添加自定义标签列
    $posts_columns['media_tag'] = __( '自定义标签' );

    return $posts_columns;
    }
    add_filter( 'manage_media_columns', 'custom_media_columns' );

    function custom_media_column( $column_name, $id )
    {
    if ( $column_name == 'media_category' ) {
    $terms = get_the_terms( $id, 'media_category' );
    if ( !empty( $terms ) ) {
    foreach( $terms as $term ) {
    $out .= esc_html( $term->name ) . '
    ';
    }
    }
    }
    elseif ( $column_name == 'media_tag' ) {
    $terms = get_the_terms( $id, 'media_tag' );
    if ( !empty( $terms ) ) {
    foreach( $terms as $term ) {
    $out .= esc_html( $term->name ) . '
    ';
    }
    }
    }
    echo wp_kses_post( $out );
    }
    add_action( 'manage_media_custom_column', 'custom_media_column', 10, 2 );
    ```

    4. 添加媒体过滤器

    接下来,我们需要添加一个自定义过滤器,以允许在媒体库中筛选指定分类和标签的媒体文件。你可以在functions.php或插件文件中添加以下代码:

    ```php
    // 添加媒体过滤器
    function custom_media_filtering() {
    $links = array();
    $terms = get_terms( array(
    'taxonomy' => array( 'media_category', 'media_tag' ),
    'order' => 'DESC',
    'hide_empty' => false,
    ) );
    foreach ( $terms as $term ) {
    reset( $posts ); //reset the query so to speak
    $query_args = array(
    'post_type' => 'attachment',
    'post_status' => 'inherit',
    'posts_per_page' => -1, // unlimited
    'tax_query' => array(
    array(
    'taxonomy' => $term->taxonomy,
    'field' => 'slug',
    'terms' => $term->slug,
    ),
    ),
    );
    $posts = get_posts( $query_args ); // get the posts
    if ( !empty( $posts ) ) {
    $links[] = sprintf( '%2$s (%3$d)', esc_attr( add_query_arg( array( 'media_category' => $term->slug ), admin_url( 'upload.php' ) ) ), esc_html( $term->name ), count( $posts ) );
    }
    }
    echo '

    ' . implode( ' | ', $links ) . '

    ';
    }
    add_action( 'restrict_manage_posts', 'custom_media_filtering' );
    ```

    5. 创建自定义表单

    最后,我们要创建一个自定义表单来允许用户在上传媒体文件时选择自定义分类和标签。在你的插件或functions.php文件中添加以下代码:

    ```php
    // 创建自定义表单
    function custom_media_attachment_fields( $form_fields, $post ) {
    $form_fields['media_category'] = array(
    'label' => __( '自定义分类' ),
    'input' => 'text',
    'value' => ''
    );

    $form_fields['media_tag'] = array(
    'label' => __( '自定义标签' ),
    'input' => 'text',
    'value' => ''
    );

    return $form_fields;
    }
    add_filter( 'attachment_fields_to_edit', 'custom_media_attachment_fields', 10, 2 );

    function custom_media_attachment_fields_save( $attachment_id ) {
    if ( isset( $_REQUEST['attachments'][ $attachment_id ]['media_category'] ) ) {
    $term = sanitize_text_field( $_REQUEST['attachments'][ $attachment_id ]['media_category'] );
    wp_set_post_terms( $attachment_id, $term, 'media_category', false );
    }

    if ( isset( $_REQUEST['attachments'][ $attachment_id ]['media_tag'] ) ) {
    $term = sanitize_text_field( $_REQUEST['attachments'][ $attachment_id ]['media_tag'] );
    wp_set_post_terms( $attachment_id, $term, 'media_tag', false );
    }
    }
    add_action( 'edit_attachment', 'custom_media_attachment_fields_save' );
    ```

    通过以上步骤,我们就可以成功地自定义媒体库中的分类和标签,添加自定义列和过滤器。同时,我们还可以向上传媒体文件时添加自定义表单,以允许用户选择自定义分类和标签。

    2023年 5月 9日 下午6:09 0条评论
微信客服
分享本页
返回顶部
玖比肆(9Bi4)专注于帮助企业实现数字化转型,提供全方位的网站建设、网络运营、网络推广、网络营销和SEO优化等一系列数字化营销服务。