简体   繁体   中英

Wordpress: Display media files on a page based on logged in user

I have a code that displays entire media library files on a page. I want to achieve functionality to display the media files only to users who are currently logged in and the files they have uploaded. If the user did not upload the file it should simply say no file uploaded.

Current PHP snippet:

<?php 
class list_media {
    public function list_media_controller( $atts, $content = "" ) {
        //Get the Shortcode attributes
        extract( 
            shortcode_atts( 
                array( 
                'order' => 'ASC',
                'posts_per_page' => -1,
                'post_status' => null,
                'post_parent' => null,
                'default_styles' => true,
                'date_format' => "Y/m/d"
                ), $atts 
            )
        );

        //Query ARGS
        $args = array(
         'post_type' => 'attachment',
        'author' => $user_ID,
            'posts_per_page' => $posts_per_page,
            'numberposts' => $numberposts,
            'post_type' => 'attachment',
            'post_status' => $post_status,
            'post_parent' => $post_parent, 
            'orderby' => $order_by,
            'order' => $order   
        );

        //Print the first part of the Table
        echo "
        <table class='list_media'>
            <thead>
                <tr>
                    <th class='header'>File</th>
                    <th class='header'>Author</th>
                    <th class='header'>Date</th>
                </tr>
            </thead>
            <tbody>
        ";
    if( is_user_logged_in() ) {

    //get the currenly logged in user
    $user_ID = get_current_user_id();
    }
        //Get the Media files
       //$attachments = get_posts( $args );
           $attachments = new WP_Query( $args );
        if ( !empty( $attachments ) ) {
            foreach ( $attachments as $attachment ) {
                $attachment_id = $attachment->ID;
                $attachment_title = get_the_title( $attachment_id );
                $attachment_url = wp_get_attachment_url( $attachment_id );
                $attachment_author_id = $attachment->post_author;
                $attachment_author_url = get_author_posts_url( $attachment_id );
                $attachment_author_name = get_the_author_meta( "user_nicename", $attachment_author_id ); 
                $attachment_publish_date = get_the_date( $date_format, $attachment_id );

                $attachment_post_parent_id = $attachment->post_parent;

                if ( !empty( $attachment_post_parent_id ) ) {
                    $attachment_post_parent_title = get_the_title( $attachment_post_parent_id );
                    $attachment_post_parent_url = get_permalink( $attachment_post_parent_id );
                } else {
                    $attachment_post_parent_url = "#!";
                    $attachment_post_parent_title = "Unattached";
                }

                echo "
                <tr>
                    <td class='title'>
                        <a href='$attachment_url' target='_blank'>
                            $attachment_title
                        </a>
                    </td>
                    <td class='author'>
                        <a href='$attachment_author_url' target='_blank'>
                            $attachment_author_name
                        </a>
                    </td>
                    <td class='date'>
                        $attachment_publish_date
                    </td>
                </tr>
                ";
            }
        }

        //Print end of the Table
        echo "
            </tbody>
        </table>
        ";
    }
}
add_shortcode( 'list_media', array( 'list_media', 'list_media_controller') );

?>

This is an existing plugin -> https://wordpress.org/plugins/list-media/

After playing with the code i was able to get it right finally. Adding the answer here. I am sure it will help others.

class list_media {
    public function list_media_controller( $atts, $content = "" ) {
        //Get the Shortcode attributes
        extract( 
            shortcode_atts( 
                array( 
                'order' => 'ASC',
                'posts_per_page' => -1,
                'post_status' => null,
                'post_parent' => null,
                'default_styles' => true,
                'date_format' => "Y/m/d"
                ), $atts 
            )
        );

        //Default table styles
        if ( $default_styles == true ) {
            $default_styles = "
                <style>
                    .list-media-table {
                        display: block;
                        width: 100%;
                        height: auto;
                        border-collapse: collapse;
                    }

                    .list-media-table th,
                    .list-media-table td {
                        border: 1px solid black;
                        vertical-align: top;
                    }

                    .list-media-table .header {
                        font-size: 16px;
                        font-weight: bold;
                        color: #000;
                        padding: 5px 5px;
                    }

                    .list-media-table td {
                        font-size: 14px;
                        padding: 5px 5px;
                        border: 1px solid black;
                    }

                    .list-media-table .attachment img {
                        width: 72px;
                        height: 72px;
                    }
                </style>
            ";
        }

        //Print the first part of the Table
        echo "
        <table class='list_media'>
            <thead>
                <tr>
                    <th class='header'>File</th>
                    <th class='header'>Author</th>
                    <th class='header'>Date</th>
                </tr>
            </thead>
            <tbody>
        ";
    if( is_user_logged_in() ) {

    //get the currenly logged in user
    $user_ID = get_current_user_id();
        //Query ARGS
        $args = array(
         'post_type' => 'attachment',
        'author' => $user_ID,
            'posts_per_page' => $posts_per_page,
            'numberposts' => $numberposts,
            'post_type' => 'attachment',
            'post_status' => $post_status,
            'post_parent' => $post_parent, 
            'orderby' => $order_by,
            'order' => $order   
        );
        $attachments = get_posts( $args );

    }


        //Get the Media files
       //     $attachments = new WP_Query( $args );


        if ( !empty( $attachments ) ) {
            foreach ( $attachments as $attachment ) {
                $attachment_id = $attachment->ID;
                $attachment_title = get_the_title( $attachment_id );
                $attachment_url = wp_get_attachment_url( $attachment_id );
                $attachment_author_id = $attachment->post_author;
                $attachment_author_url = get_author_posts_url( $attachment_id );
                $attachment_author_name = get_the_author_meta( "user_nicename", $attachment_author_id ); 
                $attachment_publish_date = get_the_date( $date_format, $attachment_id );

                $attachment_post_parent_id = $attachment->post_parent;

                if ( !empty( $attachment_post_parent_id ) ) {
                    $attachment_post_parent_title = get_the_title( $attachment_post_parent_id );
                    $attachment_post_parent_url = get_permalink( $attachment_post_parent_id );
                } else {
                    $attachment_post_parent_url = "#!";
                    $attachment_post_parent_title = "Unattached";
                }

                echo "
                <tr>
                    <td class='title'>
                        <a href='$attachment_url' target='_blank'>
                            $attachment_title
                        </a>
                    </td>
                    <td class='author'>
                        <a href='$attachment_author_url' target='_blank'>
                            $attachment_author_name
                        </a>
                    </td>
                    <td class='date'>
                        $attachment_publish_date
                    </td>
                </tr>
                ";
            }
        }

        //Print end of the Table
        echo "
            </tbody>
        </table>
        ";
    }
}
add_shortcode( 'list_media', array( 'list_media', 'list_media_controller') );

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM