Wordpress - Own plugin - get all images of the media gallery - wordpress

i've written a plugin for the Wordpress TinyMCE editor and i want to use images which have been previously uploaded to the media gallery in that plugin. How can i access all uploaded pictures in my own plugin? I can't find it in the wordpress docs.
Thanks.

I am slightly not clear on your question. Are you looking for a method to access the Add Media button?
In attempts to answer your question -> This method allows you to get all attachments that is in your media section.. Currently it displays everything but you can manipulate it the way you want to.
Reference: http://codex.wordpress.org/Template_Tags/get_posts
$args = array( 'post_type' => 'attachment', 'numberposts' => -1);
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
echo apply_filters( 'the_title' , $attachment->post_title );
the_attachment_link( $attachment->ID , false );
}
}

Related

wordpress - to find if a meta_value exists

I'm working on a wordpress site and I have custom field 'mobile' in usermeta table. When a user register, I need to check if mobile number exists.I'm using zm-ajax-login-register plugin which doesnot have a mobile field. I'm successfull in adding field and value to usermeta,but I cant validate.
I tried like this,but metadata_exists is not the right code
if ( $user['mobile']!= ''){
$your_custom_field= metadata_exists( 'user', $user->ID, $user['mobile'] );
if($your_custom_field){
$status =$this->_zm_alr_helpers->status('invalid_mobile');
}
}
Hooray.....Finally I got the answer...
$args = array(
'meta_value' => $user['mobile']
);
$user_mobile_exists=get_users( $args );
if ($user_mobile_exists)
{
$status =$this->_zm_alr_helpers->status('invalid_mobile');
}

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.

WP―button to start media library

I am creating a wordpress plugin and I would like to enable the user to use images from the media library. For example: the »Choose Image« button in the »header« section of Theme configuration does exactly what I am looking for.
Is there any wordpress method I can call to generate such a button?
Greetings philipp
what you try to accomplish is not clear,
using all the images is one thing and give them an option to use wordpress uploader is one thing.
give them the last option is not recommended because they can abuse your system without control.
to see all the images you can use this function, return an array :
function get_all_images(){
$args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
$images = array();
foreach ( $query->posts as $image) {
$images[]= wp_get_attachment_url($image->ID);
}
return $images;
}

How to fetch all WordPress posts with featured image?

In WordPress 3 there is Featured Image functionality. How do i fetch all posts that have a featured image with them? Here is my current custom loop:
$loop = new WP_Query( array( 'posts_per_page' => 15 ) );
This should work:
$loop = new WP_Query( array( 'posts_per_page' => -1, 'meta_key' => '_thumbnail_id' ) );
I haven't tested this, though. Also, this will probably fetch all posts and pages. Use 'post_type' => 'post' to limit it to blog posts.
I don't believe you need any special loops defined for that to work.
Though you do need to add some small snippets into your functions.php
like this one:
<?php add_theme_support ( 'post-thumbnails' ); ?>
after apply the above code to functions.php file, your theme will support Featured Images and you will see a new link # the bottom right of your Post Add / Edit interface.
This guide will help you, if you are looking for more info regarding this : How to Use Wordpress Featured Image Feature

Resources