Viewing 6 replies - 1 through 6 (of 6 total)
  • I would love this function too – the ability to add to my custom post types via shortcode would be a bonus!

    Many thanks

    Plugin Author Marco Milesi

    (@milmor)

    Hi,
    unfortunately at the moment there isn’t that possbility. I will add it soon.

    Yes this function is very much needed, it’s the one thing that’s keeping me from using the plugin. 🙁

    Is there anyway to delete or turn off automatic insertion to post?

    Plugin Author Marco Milesi

    (@milmor)

    These feature will be added soon soon 🙂

    Dear all,

    Actually, this function could be used as is. To avoid automatic appending to post content, comment out the line:

    add_filter('the_content', 'wpatt_content_filter');

    Once you’ve done this, all that’s left to do is to make a call to the function: wpatt_content_filter

    I’ve commented out the line as before and added a separate function based on the code to my functions.php. It looks like this:

    function show_attachments($content) {
        global $post;
        $somethingtoshow = 0;
        $content_l = null;
    
        $checkrestrict = false;
        if ( get_option('wpatt_option_restrictload') && !is_single() && !is_page() ) { $checkrestrict = true; }
    
        if ($post->ID == '0' || $post->ID == NULL || get_post_meta($post->ID, 'wpa_off', true) || post_password_required() || $checkrestrict ) { return $content; }
    
        $attachments = get_posts(array(
            'post_type' => 'attachment',
            'orderby' => 'menu_order',
            'order' => 'ASC',
            'posts_per_page' => 100,
            'post_parent' => $post->ID
        ));
    
        if ($attachments) {
            $content_l .= '<!-- WP Attachments --><ul class="post-attachments">';
            foreach ($attachments as $attachment) {
    
                $wpatt_option_includeimages_get = get_option('wpatt_option_includeimages');
                if ($wpatt_option_includeimages_get == '1') {
                } else if ( wp_attachment_is_image( $attachment->ID ) ) {
                    continue;
                }
                $somethingtoshow = 1;
    
                $class = "post-attachment mime-" . sanitize_title($attachment->post_mime_type);
    
                if ($wpatt_option_targetblank_get == '1') { $content_l .= 'target="_blank" '; }
    
                if ((file_exists(get_attached_file($attachment->ID)))) {
                    $wpatt_fs = wpatt_format_bytes(filesize(get_attached_file($attachment->ID)));
                } else {
                    $wpatt_fs = 'ERROR';
                }
                $wpatt_date = new DateTime($attachment->post_date);
    
                switch ( get_option('wpa_template') ) {
                    case 1: //STANDARD WITH DATE
                        $wpattachments_string = '<a href="%URL%">%TITLE%</a> <small>(%SIZE%)</small> <div style="float:right;">%DATE%</div>';
                        break;
                    case 2: //EXTENDED
                        $wpattachments_string = '<a href="%URL%">%TITLE%</a> <small>• %SIZE% • %DOWNLOADS% click</small> <div style="float:right;">%DATE%</div><br><small>%CAPTION%</small>';
                        break;
                    case 3: //CUSTOM
                        $wpattachments_string =  html_entity_decode( get_option('wpa_template_custom') );
                        break;
                    default: //DEFAULT
                        $wpattachments_string = '<a href="%URL%">%TITLE%</a> <small>(%SIZE%)</small>';
                }
    
                if ( get_option('wpatt_option_targetblank') ) {
                    $wpattachments_string = str_replace('<a href', '<a target="_blank" href', $wpattachments_string);
                }
    
                if ( get_option('wpatt_counter') ) {
                    $url = add_query_arg( 'download', $attachment->ID, get_permalink() );
                } else {
                    $url = wp_get_attachment_url($attachment->ID);
                }
                $wpattachments_string = str_replace("%URL%", $url, $wpattachments_string);
                $wpattachments_string = str_replace("%TITLE%", $attachment->post_title, $wpattachments_string);
                $wpattachments_string = str_replace("%SIZE%", $wpatt_fs, $wpattachments_string);
                $wpattachments_string = str_replace("%DATE%", $wpatt_date->format(get_option('wpatt_option_date_localization')), $wpattachments_string);
                $wpattachments_string = str_replace("%CAPTION%", $attachment->post_excerpt, $wpattachments_string);
                $wpattachments_string = str_replace("%DESCRIPTION%", $attachment->post_content, $wpattachments_string);
                $wpattachments_string = str_replace("%AUTHOR%", get_the_author_meta( 'display_name', $attachment->post_author), $wpattachments_string);
    
                $wpattachments_string = str_replace("%DOWNLOADS%", wpa_get_downloads($attachment->ID), $wpattachments_string);
    
                $content_l .= '<li class="' . $class . '">' . $wpattachments_string . '</li>';
    
                }
            $content_l .= '</ul>';
            }
    	if ($somethingtoshow == 1) {
    		$content .= $content_l;
    	} else {
    		$content .= 'No downloads available!';
    	}
        return $content;
    }

    Basically, it’s the same function as wpatt_content_filter, but I’ve added a couple of lines if no attachments are available:

    if ($somethingtoshow == 1) {
    		$content .= $content_l;
    	} else {
    		$content .= 'No downloads available!';
    	}

    The only risk is that these changes may be overwritten in case of a plugin update, but for the moment, these changes work well for me. Please note that I’m not checking whether the plugin is activated or any other checks; I’m doing these steps elsewhere in my theme, so you may want to check whether the function, wpatt_content_filter exists before using it, else you’ll see a white screen.

    – Mithun

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘shortcode or php instead of automatic insertion?’ is closed to new replies.