WordPress.org

Ready to get started?Download WordPress

Forums

Adding a featured image to an automated feed post? (2 posts)

  1. rjt1985
    Member
    Posted 1 year ago #

    Hi there,

    I'm using a plugin called Keyring Social Importer, and it imports my Tweets from Twitter and posts them as individual blog posts on my WordPress.org website.

    I was wondering, if any of you know php and WordPress plugins, how I could make it set the featured image of all of the blog posts it creates to something. I've put all of the code over here for you to look at: http://www.daniweb.com/web-development/php/threads/452013/adding-a-default-featured-image-to-wordpress-plugin-code

    Currently it just imports my Tweets as publishes them as blog posts... no images whatsoever, but my theme relies heavily on featured images and thumbnails so if I could set one in this code it would be really handy.

    Let me know your thoughts.

    Many thanks,

    R

  2. rjt1985
    Member
    Posted 1 year ago #

    Can anyone help with this? I'm thinking it'll be something like telling the below code that for these posts the_post_thumbnail(); = imgID, but I'm not sure how.

    // Apply selected category
                $post_category = array( $this->get_option( 'category' ) );
    
                // Clean up content a bit
                $post_content = $post->text;
                $post_content = $wpdb->escape( html_entity_decode( trim( $post_content ) ) );
    
                // Handle entities supplied by Twitter
                if ( count( $post->entities->urls ) ) {
                    foreach ( $post->entities->urls as $url ) {
                        $post_content = str_replace( $url->url, $url->expanded_url, $post_content );
                    }
                }
    
                // Any hashtags used in a tweet will be applied to the Post as tags in WP
                $tags = $this->get_option( 'tags' );
                if ( preg_match_all( '/(^|[(\[\s])#(\w+)/', $post_content, $tag ) )
                    $tags = array_merge( $tags, $tag[2] );
    
                // Add HTML links to URLs, usernames and hashtags
                $post_content = make_clickable( esc_html( $post_content ) );
    
                // Include geo Data (if provided by Twitter)
                if ( !empty( $post->geo ) && 'point' == strtolower( $post->geo->type ) )
                    $geo = array(
                        'lat' => $post->geo->coordinates[0],
                        'long' => $post->geo->coordinates[1]
                    );
                else
                    $geo = array();
    
                // Get a GUID from Twitter, plus other important IDs to store in postmeta later
                $user = $this->service->get_token()->get_meta( 'username' );
                $twitter_id              = $post->id_str;
                $twitter_permalink       = "https://twitter.com/{$user}/status/{$twitter_id}";
                $in_reply_to_user_id     = $post->in_reply_to_user_id;
                $in_reply_to_screen_name = $post->in_reply_to_screen_name;
                $in_reply_to_status_id   = $post->in_reply_to_status_id;
                $post_author             = $this->get_option( 'author' );
                $post_status             = 'publish';
                $twitter_raw             = $post;
    
                // Build the post array, and hang onto it along with the others
                $this->posts[] = compact(
                    'post_author',
                    'post_date',
                    'post_date_gmt',
                    'post_content',
                    'post_title',
                    'post_status',
                    'post_category',
                    'tags',
                    'twitter_id',
                    'twitter_permalink',
                    'geo',
                    'in_reply_to_user_id',
                    'in_reply_to_screen_name',
                    'in_reply_to_status_id',
                    'twitter_raw'
                );
            }
        }
    
        function insert_posts() {
            global $wpdb;
            $imported = 0;
            $skipped  = 0;
            foreach ( $this->posts as $post ) {
                extract( $post );
                if (
                    $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->postmeta} WHERE meta_key = 'twitter_id' AND meta_value = %s", $twitter_id ) )
                ||
                    $post_id = post_exists( $post_title, $post_content, $post_date )
                ) {
                    // Looks like a duplicate
                    $skipped++;
                } else {
                    $post_id = wp_insert_post( $post );
    
                    if ( is_wp_error( $post_id ) )
                        return $post_id;
    
                    if ( !$post_id )
                        continue;

Topic Closed

This topic has been closed to new replies.

About this Topic