• Resolved ruess

    (@ruess)


    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/

Viewing 15 replies - 1 through 15 (of 16 total)
  • Wow. I am looking for this too!

    Thread Starter ruess

    (@ruess)

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

    I need this too.

    Thread Starter ruess

    (@ruess)

    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

    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

    Thread Starter ruess

    (@ruess)

    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.

    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.

    Thread Starter ruess

    (@ruess)

    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

    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

    Sorry, let me try again.

    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
         *
    Thread Starter ruess

    (@ruess)

    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)

    Thread Starter ruess

    (@ruess)

    Also, switch to twenty-eleven or twenty-twelve theme temporarily to see if that affects your outcome πŸ™‚

    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.

    Thread Starter ruess

    (@ruess)

    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 πŸ˜‰

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Automatically Set Featured Image on Create New Page’ is closed to new replies.