How to handle/manage custom images? - wordpress

I am working on a special plugin for a customer.
The situation in short:
The plugin contains a automatic import for a .zip file. Inside this files are one .xml file and images.
The plugin reads the .xml file and insert the information to the database.
My question:
How I can handle the images the best way. Should I import them into the wordpress gallery or should I manage them on my own.
Is there a way to use the wordpress gallery, because it will automatic generate thumbnails, or is it not a good idea?
I need some suggestions. Thanks!

You should add images in wordpress gallery. Then you have to get these uploaded images from the wordpress gallery:
Step 1: Prepare the query
global $post;
$args = array(
'post_parent' => $post->ID, // For the current post
'post_type' => 'attachment', // Get all post attachments
'post_mime_type' => 'image', // Only grab images
'order' => 'ASC', // List in ascending order
'orderby' => 'menu_order', // List them in their menu order
'numberposts' => -1, // Show all attachments
'post_status' => null, // For any post status
);
First, we set up the global Post variable ($post) so we can have access to the relevant data about our post.
Second, we set up an array of arguments ($args) that define the kind of information we want to retrieve. Specifically, we need to get images that are attached to the current post. We're also going to get all of them, and return them in the same order they appear in the WordPress gallery.
Step 2: Retrieve the Images from Wordpress gallery
// Retrieve the items that match our query; in this case, images attached to the current post.
$attachments = get_posts($args);
// If any images are attached to the current post, do the following:
if ($attachments) {
// Initialize a counter so we can keep track of which image we are on.
$count = 0;
// Now we loop through all of the images that we found
foreach ($attachments as $attachment) {
Here we are using the WordPress get_posts function to retrieve the images that match our criteria as defined in $args. We are then storing the results in a variable called $attachments.
Next, we check to see if $attachments exists. If this variable is empty (as will be the case when your post or page has no images attached to it), then no further code will execute. If $attachments does have content, then we move on to the next step.
Set parameters for a WordPress function called wp_get_attachment_image for the images information.
Source: Read the link for complete tutorial or other steps > https://code.tutsplus.com/tutorials/how-to-create-an-instant-image-gallery-plugin-for-wordpress--wp-25321

Related

use same image in wordpress gallery without upload it many times

I know this sounds strange but I need to make a gallery with repeated images.
I need to know if is possible to use the same image in the gallery more than one time without having to add it again and again. It's very annoying to have the same image repeated many times among the uploaded images.
Thanks in advance.
No, not with with orginal wordpress code. Even manually putting several times the same media-id will result in only one image being rendered. you can, however, create your own gallery shortcode: code from here
//Init hook
add_action('init', 'override_gallery');
//Override function
function override_gallery()
{
remove_shortcode('gallery');
add_shortcode('gallery', 'my_gallery_shortcode');
}
//Custom gallery shortcode
function my_gallery_shortcode($atts, $content) {
//Default parameters to
extract( shortcode_atts( array(
'ids' => '',
'orderby' => 'post__in',
'columns' => '3',
'link' => 'file' //file | link | <empty string> (for linking to attachment page)
), $atts ));
//Your own presentational code here...
}
But you still can't choose the same image several times in the image selector. But you could multiply it in your shortcode. Probably it's better to create your own shortcode using another name though.

How do I embed an attached file into the post in the single-template, after retrieving attachment from the $Post,

I have a custom content type, called "sheets" with an XLS spreadsheet attached.
I'm trying to now display the spreadsheet embedded into the post inside the single-sheets.php template.
I'm using Google Embedded Document plugin, which allows Excel spreadsheets to be embedded and displayed in a post through a shortcode inside the editor of a post, so I hope it's still possible to embed the document when the spreadsheet is added to the post manually in a custom field.
I have managed to retrieve the attachments and display just the link to download the spreadsheet, but I want the actual attachment to appear embedded into the post, Just like it would if I were to.
Here is a piece of the single-sheets.php template, what I have so far.
<?php
// place inside loop
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'application/zip, application/msword, application/vnd.ms-excel, application/pdf',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
echo '<h3>Attached files</h3>';
if ($attachments) {
foreach ($attachments as $attachment) {
the_attachment_link($attachment->ID, false);
}
}
?>
This returns a link to the spreadsheet to download the attachment, but it's not embedded in the post:
Attached files
Test Spreadsheet 2 - Link to spreadsheet download
Ok I figured it out haha, I had basically answered my own question here.
I just had to use wordpress function: do_shortcode()
and include the shortcode I needed which was [gview file="http://linkhere"];
This embedded the attachment, although the attachment_link() didnt work, I needed to get the GUID link instead.

wordpress count image attachments

I'm looking for some help, for some reason this code isn't working when trying to display total image attachments on the main index.php page.
// Get all the attachments
$attachments = get_posts ( array(
'numberposts' => -1,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => get_the_ID(),
'post_status' => 'inherit',
) );
// Count all the attachments
$total = count( $attachments );
The problem is, the loop is in the main index.php page, but it's calling a post.php template so I have placed this code there instead (I'm assuming that's still in the loop?) I am then calling $total just below it where I want to show "View all # images".
Any ideas why this is just displaying the number as 0 even though I have added an image gallery to the post using the basic Wordpress gallery media library?
Thanks
The attachment count only increases if it's attached to a post. You can force the count by adding this:
$wp_taxonomies['category']->update_count_callback = '_update_generic_term_count';
in a function in your functions.php something like this:
function change_category_arg() {
global $wp_taxonomies;
if ( ! taxonomy_exists('category') )
return false;
$wp_taxonomies['category']->update_count_callback = '_update_generic_term_count';
}
add_action( 'init', 'change_category_arg' );
Explanation:
This is significant in the case of attachments. Because an attachment
is a type of post, the default _update_post_term_count() will be used.
However, this may be undesirable, because this will only count
attachments that are actually attached to another post (like when you
insert an image into a post). This means that attachments that you
simply upload to WordPress using the Media Library, but do not
actually attach to another post will not be counted. If your intention
behind associating a taxonomy with attachments was to leverage the
Media Library as a sort of Document Management solution, you are
probably more interested in the counts of unattached Media items, than
in those attached to posts. In this case, you should force the use of
_update_generic_term_count() by setting '_update_generic_term_count' as the value for update_count_callback.
from Wordpress Codex on register_taxonomy

How to get Wordpress attachments to work for existing images inside the Media Library?

I want to display the images from each Wordpress post on a separate page.
When using get_children (or get_posts) the 'post_type' => 'attachment' only works if I've just uploaded an image (via WP's 'ADD MEDIA > UPLOAD FILES') to that particular post.
It does not work if I add an existing image to a post that's already in my WP MEDIA LIBRARY).
Is there anyway for 'attachment' to work for existing (already uploaded) images?
See my test function:
function echo_first_image($postID){
$args = array(
'post_type' => 'attachment',
'post_parent' => $postID
);
$attachments = get_children( $args );
if($attachments){
echo'YES'; // test answer
}else{
echo'NO'; // test answer
}
}
EDIT: each 'post_type' that is an 'attachment' has a single 'post_parent' - does this mean an attachment can ONLY have a single parent?
Currently as of writing, Wordpress 4.1 does not include multi-parent attachments because the database relationship is one-to-one, not one-to-many. You would have to find a plugin, or write your own to get around that.

Adding postmeta default values after adding post

I am using Wordpress XMLRPC to add posts to my blog.
However, after running this.
$data = array(
'title' => $title,
'description' => $content,
'post_type' => 'post',
'categories' => array($category),
'post_status' => 'publish'
);
$addedPostReturn = $this->_client->query('metaWeblog.newPost',
array(0,$username,$password,$data,1));
This adds the post fine but doesn't add the postmeta information.
If i open the post, click Update, all the default postmeta gets updated. However, I would like to add all the default postmeta information with my php script instead of manually (or else it kinda defeats the purpose).
Is there anyway, either with xmlrpc or regular wordpress functions to create the postmeta custom fields by using their default values? If not, is there a way to have the list of all the custom fields i need to manually add using the custom fields section of the metaWeblog.newPost function? (I don't want to add some but not others. I rather do a complete job)
Thanks in advance... any help is appreciated! :)
EDIT:
$postUpdateContent = array();
$postUpdateContent['ID'] = $newPostId;
$postUpdateContent['post_content'] = $sameContent;
wp_update_post( $postUpdateContent );
didn't seem to do the trick...

Resources