WordPress.org

Ready to get started?Download WordPress

Forums

NextGEN Gallery
[resolved] Automatically Set Featured Image on Create New Page (17 posts)

  1. ruess
    Member
    Posted 1 year ago #

    Hi there,

    I'm working on a client site and would like for some things to be a bit more automated for them if possible. Right now, if you've just created a new gallery, you have the option of Create New Page in the gallery settings where pressing the Add Page button will create a new page with a shortcode to that gallery. Then it's up to the user to go to that new page and select an image to be the featured image.

    The code that makes this happen in Nextgen appears to be in \admin\manage.php:

    // Add a new page
    
    check_admin_referer('ngg_updategallery');
    
    $parent_id      = esc_attr($_POST['parent_id']);
    $gallery_title  = esc_attr($_POST['title']);
    $gallery_name   = $wpdb->get_var("SELECT name FROM $wpdb->nggallery WHERE gid = '$this->gid' ");
    
    // Create a WP page
    global $user_ID;
    
    $page['post_type']    = 'page';
    $page['post_content'] = '[nggallery id=' . $this->gid . ']';
    $page['post_parent']  = $parent_id;
    $page['post_author']  = $user_ID;
    $page['post_status']  = 'publish';
    $page['post_title']   = $gallery_title == '' ? $gallery_name : $gallery_title;
    $page = apply_filters('ngg_add_new_page', $page, $this->gid);
    
    $gallery_pageid = wp_insert_post ($page);
    if ($gallery_pageid != 0) {
    $result = $wpdb->query("UPDATE $wpdb->nggallery SET title= '$gallery_title', pageid = '$gallery_pageid' WHERE gid = '$this->gid'");
    p_cache_delete($this->gid, 'ngg_gallery');
    nggGallery::show_message( __('New gallery page ID','nggallery'). ' ' . $gallery_pageid . ' -> <strong>' . $gallery_title . '</strong> ' .__('created','nggallery') );
    			}
    
     do_action('ngg_gallery_addnewpage', $this->gid);
    }

    Could anyone give me some insights on how to have this function automatically use the selected "Preview Image" from the particular gallery settings page become the default featured image of the newly created page?

    Thanks much!

    http://wordpress.org/extend/plugins/nextgen-gallery/

  2. anicode
    Member
    Posted 1 year ago #

    Wow. I am looking for this too!

  3. ruess
    Member
    Posted 1 year ago #

    Cool anicode - let me know if you discover anything - and will as well.

  4. quirkylissy
    Member
    Posted 1 year ago #

    I need this too.

  5. ruess
    Member
    Posted 1 year ago #

    OK, so I have found a solution for this after much toil ;)

    Add the following to \nextgen-gallery\admin\manage.php right after the following code:

    do_action('ngg_gallery_addnewpage', $this->gid);
    }

    and before the "}" immediately following:

    //Start Set Featured Image
    
    //Determine filepath of selected NextGEN preview image
    $gallery_id = $this->gid; // Use the PODS column, custom post field or however you would get this ID
    $results = $wpdb->get_results("SELECT ng.path, np.filename FROM wp_ngg_pictures np, wp_ngg_gallery ng WHERE np.galleryid=ng.gid AND np.galleryid=".$gallery_id." AND np.pid=ng.previewpic",ARRAY_A);
    if(!empty($results[0]['path']) && !empty($results[0]['filename'])) :
         $imgpath = ABSPATH.$results[0]['path'].'/'.$results[0]['filename'];
    endif;
    //Set filepath and filename variables
    $image_url = $imgpath;
    
    //Copy the file from the NextGen Gallery to the Media Library (uploads folder)
    $upload_dir = wp_upload_dir();
    $filename = basename($image_url);
    if(wp_mkdir_p($upload_dir['path']))
    	$file = $upload_dir['path'] . '/' . $filename;
    else
    	$file = $upload_dir['basedir'] . '/' . $filename;
    
    //Perform copy function
    if (!copy($imgpath, $file)) {
    	echo "failed to copy $file...\n";
    }				
    
    //Create entry in media library
    $wp_filetype = wp_check_filetype($filename, null );
    $attachment = array(
    	'post_mime_type' => $wp_filetype['type'],
    	'post_title' => sanitize_file_name($filename),
    	'post_content' => '',
    	'post_status' => 'inherit'
    );
    		$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
    
    //Set featured image for newly created page
    set_post_thumbnail($gallery_pageid, $attach_id);
    //End Set Featured Image

    What this code does is to grab the file location of the selected preview image from the particular NextGen gallery you're on, copies that image into the media library, creates a media library database entry for the file, and finally updates the featured image on the new page with the ID of that image.

    Hope this helps!

    kevin

  6. quirkylissy
    Member
    Posted 1 year ago #

    Thanks Kevin,

    I tried to insert the codes but it didn't work.

    To clarify, where exactly do we add the first strip of code? And exactly which "}" that we add the 2nd blocks of code?

    It would be so helpful if you provide an example of the 'modified' file of manage.php

  7. ruess
    Member
    Posted 1 year ago #

    Hey Quirklissy,

    The original file has the following lines:

    $gallery_pageid = wp_insert_post ($page);
    			if ($gallery_pageid != 0) {
    				$result = $wpdb->query("UPDATE $wpdb->nggallery SET title= '$gallery_title', pageid = '$gallery_pageid' WHERE gid = '$this->gid'");
    				wp_cache_delete($this->gid, 'ngg_gallery');
                    nggGallery::show_message( __('New gallery page ID','nggallery'). ' ' . $gallery_pageid . ' -> <strong>' . $gallery_title . '</strong> ' .__('created','nggallery') );
    			}
    
                do_action('ngg_gallery_addnewpage', $this->gid);
    		}
    --> enter my code above right here
    }

    Let me know if that works for you or if you're still having trouble. Also, just to be save, please include the version of NextGen you're using.

  8. quirkylissy
    Member
    Posted 1 year ago #

    Thanks for the quick response.

    Unfortunately, it still doesn't work for me. I am using WP3.5 and nextgen V1.9.10; but there is something I should mention. My theme didn't support Feature Image before. I added the code to the function.php, index.php and single.php to support featured image.

  9. ruess
    Member
    Posted 1 year ago #

    Hmm. . so one question to see whether the code is functioning at all would be to see if the preview image is appearing in the media gallery after you click the Add Page from the NextGen gallery you're in. Also, see if you can check the uploads folder to see if any new images have been copied there. If nothing is going on, then there's something wrong with the code itself or the placement of the code.

    Thanks,

    kevion

  10. quirkylissy
    Member
    Posted 1 year ago #

    I confirm that nothing is going on. My gallery has a different gallery directory, does that affect the outcome?

    I created a test post using an existing gallery, no featured image was grabbed, no new image was copied to media library. I placed the code right after `do_action('ngg_gallery_addnewpage', $this->gid);
    }`

    do_action('ngg_gallery_addnewpage', $this->gid);
    		}
    $results = $wpdb->get_results("SELECT ng.path, np.filename FROM wp_ngg_pictures np, wp_ngg_gallery ng WHERE np.galleryid=ng.gid AND np.galleryid=".$gallery_id." AND np.pid=ng.previewpic",ARRAY_A);
    if(!empty($results[0]['path']) && !empty($results[0]['filename'])) :
         $imgpath = ABSPATH.$results[0]['path'].'/'.$results[0]['filename'];
    endif;
    //Set filepath and filename variables
    $image_url = $imgpath;
    
    //Copy the file from the NextGen Gallery to the Media Library (uploads folder)
    $upload_dir = wp_upload_dir();
    $filename = basename($image_url);
    if(wp_mkdir_p($upload_dir['path']))
    	$file = $upload_dir['path'] . '/' . $filename;
    else
    	$file = $upload_dir['basedir'] . '/' . $filename;
    
    //Perform copy function
    if (!copy($imgpath, $file)) {
    	echo "failed to copy $file...\n";
    }				
    
    //Create entry in media library
    $wp_filetype = wp_check_filetype($filename, null );
    $attachment = array(
    	'post_mime_type' => $wp_filetype['type'],
    	'post_title' => sanitize_file_name($filename),
    	'post_content' => '',
    	'post_status' => 'inherit'
    );
    		$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
    
    //Set featured image for newly created page
    set_post_thumbnail($gallery_pageid, $attach_id);
    //End Set Featured Image
    	}
    
       	/**
       	 * Publish a new post with the shortcode from the selected image
  11. quirkylissy
    Member
    Posted 1 year ago #

    Sorry, let me try again.

  12. quirkylissy
    Member
    Posted 1 year ago #

    I think I placed the code wrongly earlier. But it still doesn't work.

    do_action('ngg_gallery_addnewpage', $this->gid);
    		}
    //Start Set Featured Image
    
    //Determine filepath of selected NextGEN preview image
    $gallery_id = $this->gid; // Use the PODS column, custom post field or however you would get this ID
    $results = $wpdb->get_results("SELECT ng.path, np.filename FROM wp_ngg_pictures np, wp_ngg_gallery ng WHERE np.galleryid=ng.gid AND np.galleryid=".$gallery_id." AND np.pid=ng.previewpic",ARRAY_A);
    if(!empty($results[0]['path']) && !empty($results[0]['filename'])) :
         $imgpath = ABSPATH.$results[0]['path'].'/'.$results[0]['filename'];
    endif;
    //Set filepath and filename variables
    $image_url = $imgpath;
    
    //Copy the file from the NextGen Gallery to the Media Library (uploads folder)
    $upload_dir = wp_upload_dir();
    $filename = basename($image_url);
    if(wp_mkdir_p($upload_dir['path']))
    	$file = $upload_dir['path'] . '/' . $filename;
    else
    	$file = $upload_dir['basedir'] . '/' . $filename;
    
    //Perform copy function
    if (!copy($imgpath, $file)) {
    	echo "failed to copy $file...\n";
    }				
    
    //Create entry in media library
    $wp_filetype = wp_check_filetype($filename, null );
    $attachment = array(
    	'post_mime_type' => $wp_filetype['type'],
    	'post_title' => sanitize_file_name($filename),
    	'post_content' => '',
    	'post_status' => 'inherit'
    );
    		$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
    
    //Set featured image for newly created page
    set_post_thumbnail($gallery_pageid, $attach_id);
    //End Set Featured Image
    	}
    
       	/**
       	 * Publish a new post with the shortcode from the selected image
         *
  13. ruess
    Member
    Posted 1 year ago #

    Hmm. not sure if it helps, but please note that this code will only work when adding a new page FROM a NextGEN gallery - it will not work when inserting a NextGEN gallery into a page.

    Thus, make sure you're going from the Dashboard to:
    Gallery->Manage Gallery->[Choose a gallery]->Create new page (button located at upper right)

  14. ruess
    Member
    Posted 1 year ago #

    Also, switch to twenty-eleven or twenty-twelve theme temporarily to see if that affects your outcome :)

  15. quirkylissy
    Member
    Posted 1 year ago #

    Ok, my bad. I completely missed the 'page' in this thread. I was looking for automatically using the first image of nextgen gallery as featured image of a 'post', and I actually want it to work retrospectively (because many old posts do not have featured image now).

    I am so sorry to have wasted your time, and thank you so much for taking the time.

  16. ruess
    Member
    Posted 1 year ago #

    No worries quirkylissy.

    I can't help you with the "retrospectively" part, but if you want this to work for creating new posts instead of pages from inside a NextGEN gallery, find the code (also in manage.php):

    // Create a WP page
    global $user_ID;
    
    $page['post_type']    = 'page';

    and change the post type from "page to "post":
    $page['post_type'] = 'post';

    Maybe that will help a bit ;)

  17. Arturo emilio
    Member
    Posted 10 months ago #

    Hi! Is this working for the latest version?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic