Skip to content

Instantly share code, notes, and snippets.

@pararang
Forked from bueltge/set-featured-image.php
Last active August 29, 2015 14:22
Show Gist options
  • Save pararang/415bd98a44696b4ccbb5 to your computer and use it in GitHub Desktop.
Save pararang/415bd98a44696b4ccbb5 to your computer and use it in GitHub Desktop.

Revisions

  1. @bueltge bueltge revised this gist Jul 9, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion set-featured-image.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php
    /**
    * Plugin Name: Set featured image
    * Plugin URI: http://bueltge.de
    * Plugin URI: http://wpengineer.com/2460/set-wordpress-featured-image-automatically/
    * Description: Set featureed image automaticly on save post/page
    * Version: 1.0.1
    * Author: Frank Bültge
  2. @bueltge bueltge revised this gist Jul 9, 2012. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions set-featured-image.php
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    * Plugin Name: Set featured image
    * Plugin URI: http://bueltge.de
    * Description: Set featureed image automaticly on save post/page
    * Version: 1.0.0
    * Version: 1.0.1
    * Author: Frank Bültge
    * Author URI: http://bueltge.de
    * License: GPLv3
    @@ -14,17 +14,18 @@

    if ( ! function_exists( 'fb_set_featured_image' ) ) {

    // Use this hook, comment the follow line and the image was only set on publish a post
    // add_action( 'publish_post', 'fb_set_featured_image' );
    add_action( 'save_post', 'fb_set_featured_image' );
    /**
    * Set featured image on posts
    *
    */
    function fb_set_featured_image() {

    if ( ! isset( $GLOBALS['post']->ID ) )
    return NULL;

    if ( has_post_thumbnail( get_the_ID() ) )
    return;
    return NULL;

    $args = array(
    'numberposts' => 1,
  3. @bueltge bueltge revised this gist Jul 7, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions set-featured-image.php
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,8 @@

    if ( ! function_exists( 'fb_set_featured_image' ) ) {

    // Use this hook, comment the follow line and the image was only set on publish a post
    // add_action( 'publish_post', 'fb_set_featured_image' );
    add_action( 'save_post', 'fb_set_featured_image' );
    /**
    * Set featured image on posts
  4. @bueltge bueltge revised this gist Jun 21, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion set-featured-image.php
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    * License: GPLv3
    */

    // This file is not called from WordPress. We don't like that.
    // This file is not called by WordPress. We don't like that.
    ! defined( 'ABSPATH' ) and exit;

    if ( ! function_exists( 'fb_set_featured_image' ) ) {
  5. @bueltge bueltge created this gist Jun 14, 2012.
    100 changes: 100 additions & 0 deletions set-featured-image.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    <?php
    /**
    * Plugin Name: Set featured image
    * Plugin URI: http://bueltge.de
    * Description: Set featureed image automaticly on save post/page
    * Version: 1.0.0
    * Author: Frank Bültge
    * Author URI: http://bueltge.de
    * License: GPLv3
    */

    // This file is not called from WordPress. We don't like that.
    ! defined( 'ABSPATH' ) and exit;

    if ( ! function_exists( 'fb_set_featured_image' ) ) {

    add_action( 'save_post', 'fb_set_featured_image' );
    /**
    * Set featured image on posts
    *
    */
    function fb_set_featured_image() {

    if ( has_post_thumbnail( get_the_ID() ) )
    return;

    $args = array(
    'numberposts' => 1,
    'order' => 'ASC', // DESC for the last image
    'post_mime_type' => 'image',
    'post_parent' => get_the_ID(),
    'post_status' => NULL,
    'post_type' => 'attachment'
    );

    $attached_image = get_children( $args );
    if ( $attached_image ) {
    foreach ( $attached_image as $attachment_id => $attachment )
    set_post_thumbnail( get_the_ID(), $attachment_id );
    }

    }

    }

    if ( ! function_exists( 'fb_add_thumb_column' ) ) {

    // posts columns
    add_filter( 'manage_posts_columns', 'fb_add_thumb_column' );
    add_action( 'manage_posts_custom_column', 'fb_add_thumb_value', 10, 2 );
    // pages columns
    add_filter( 'manage_pages_columns', 'fb_add_thumb_column' );
    add_action( 'manage_pages_custom_column', 'fb_add_thumb_value', 10, 2 );
    /**
    * Add description for table head
    *
    * @param $cols Array
    * @return $cols Array
    */
    function fb_add_thumb_column( $cols ) {

    $cols['thumbnail'] = __('Thumbnail');
    return $cols;
    }

    /**
    * Add thumbnail, if exist
    *
    * @param $column_name String
    * @param $post_id Integer
    */
    function fb_add_thumb_value( $column_name, $post_id ) {

    if ( 'thumbnail' !== $column_name )
    return;

    $width = (int) 35;
    $height = (int) 35;

    $args = array(
    'numberposts' => 1,
    'order' => 'ASC', // DESC for the last image
    'post_mime_type' => 'image',
    'post_parent' => get_the_ID(),
    'post_status' => NULL,
    'post_type' => 'attachment'
    );

    $attached_image = get_children( $args );

    if ( $attached_image ) {
    foreach ( $attached_image as $attachment_id => $attachment )
    echo wp_get_attachment_image( $attachment_id, array( $width, $height ), TRUE );
    } else {
    echo __( 'None' );
    }

    }

    }