I'm building a WordPress custom file uploader in my custom theme and now I need to be able to delete each file when a button is clicked.
I have this so far:
<?php
$attachments = get_posts(
array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
)
);
foreach ( $attachments as $attachment ) {
$file_link = wp_get_attachment_url($attachment->ID, "full");
$file_img = wp_get_attachment_image($attachment->ID);
$file_title = get_the_title($attachment->ID);
$size_media = filesize(get_attached_file($attachment->ID));
$total_size = ($size_media) / 1000;
?>
<div>
<a href="http:<?php echo $file_link; ?>" target="_blank">
<div>
<?php if (strpos($file_link, '.png') !== false || strpos($file_link, '.jpg') !== false || strpos($file_link, '.gif') !== false || strpos($file_link, '.tif') !== false ) {
echo '<span class="material-icons">insert_photo</span>';
} else {
echo '<span class="material-icons">description</span>';
}
?>
</div>
</a>
</div>
<div>
<a href="http:<?php echo wp_get_attachment_url($attachment->ID, "full"); ?>" target="_blank">
<p><?php echo $file_title; ?></p>
</a>
<p>Size: <?php echo $total_size; ?>KB</p>
Delete
</div>
<?php } ?>
}
What is happening is that when the Delete link is pressed ALL the files are deleted. I don't want that. I need only that one file where the link was pressed to be deleted.
You are making a wrong assumption about php-functions included in html. What php does is, it outputs some html. In your case what you are doing is:
Delete
This tells Wordpress to delete the attachment immediately. It then returns nothing, so you html will look like this:
Delete
What you need is a javascript function that does an ajax request to tell the server-side php to delete a specific attachment. You can call this in the html:
Delete
This will result in the following html:
<a onclick="deleteFile(30);">Delete</a>
Now you can go and write a javascript function called deleteFile and takes the attachment id as argument. It should send the id to the server and call wp_delete_attachment( $_GET['id'] ).
Related
I am sure this is answered on this forum but I can't find the answer so here goes:
I have a webpage template.php. It has lot of code but in between there is:
<!-- Hero Content -->
<div class="home-content">
<div class="home-text">
<h1 class="hs-line-8 no-transp font-alt mb-50 mb-xs-30"> DISCOVER </h1>
<h2 class="hs-line-12 font-alt mb-50 mb-xs-30"> <?php echo $saved_data['title']; ?> </h2> <?php echo "<img src='$saved_data['builderimagepath']'>"; ?>
<div class="local-scroll"> Register <span class="hidden-xs"> </span> Learn More </div>
</div>
</div>
<!-- End Hero Content -->
Key lines are:
<?php echo $saved_data['title']; ?>
<?php echo "<img src='$saved_data['builderimagepath']'>"; ?>
Now the second web page read.php wants to read the variable names being used. Hence I want to know how to get "title" and "builderimagepath" in an array in read.php.
( I can rename the variables as a key or as a multi-dimensional array in template.php )
In read.php, this is what I have so far:
<?php
$url = 'index.php';
$content = file_get_contents($url);
$first_step = explode( '<?php echo $saved_data' , $content );
$second_step = explode("']; ?>" , $first_step[1] );
echo $second_step[0]; // Will Add to array to process
?>
We are NOT passing variables through GET / POST from template.php to read.php , but read.php wants to get it in an array. What I want to know:
1) Is there a better approach?
2) What is the best way to name variables in template.php , so its easier to access and process in read.php?
I don't think its relevant but $saved_data is an array coming from a file:
<!DOCTYPE html>
<?php
// Read from file
if (empty($_GET['file'])) {
// Use default file:
$filename = 'mydata.txt';
$saved_raw_data = file_get_contents($filename);
$saved_data = unserialize($saved_raw_data);
} else {
$saved_raw_data = file_get_contents($_GET['file']);
$saved_data = unserialize($saved_raw_data);
}
?>
In the file that you want to store the variables, return the values as an array and you can catch them in another file when including into a variable.
template.php
return [
'var1' => 'value',
'var2' => 'value2
];
read.php
$variable = include('template.php');
print_r($variable);
My website consist of 5 inner pages, and I want to use different banner images for each page. The page is with side bar, but I want a full width banner, so I used a code which I got from wordpress and its working
this is the code..
<div class="banner">
<?php
if( is_page('About') ) $img = 'bannerAbout.jpg';
elseif( is_page('Services') ) $img = 'bannerServices.jpg';
elseif( is_page('Testimonials') ) $img = 'bannerTestimonials.jpg';
elseif( is_page('Testimonials') ) $img = 'bannerTestimonials.jpg';
elseif( is_home() ) $img = 'bannerBlog.jpg';
else $img = 'banner.jpg';?>
<img alt="" src="<?php bloginfo('stylesheet_directory'); ?>/images/<?php echo $img;?>" />
</div>
My question is, How Can I call 'Featured Image' of each page in this code? between $img=""
Or any plug-for this?
If I can call the featured image, then its easy to upload images, otherwise I need to use FTP all the time to change.
Please help me.
Thanks in Advance
Add this code to your theme's functions.php
add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );
this will enable featured image for post and page
Then in your page.php file add this code
before content and sidebar
<?php
global $post;
echo get_the_post_thumbnail($post->ID, 'full'); // visit http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail for more info
?>
Place below code in each condition
global $post
echo get_the_post_thumbnail( $post->ID,'full' );
full will take the full size of image what ever you have uploaded
I've a function that will echo the URL of the image from the content of Wordpress.
I got the function work now no problem
// Get Image Attachments
function sa_get_image($postid=0, $size='thumbnail') { //it can be thumbnail or full
if ($postid<1)
$postid = get_the_ID();
$thumb = get_post_meta($postid, "thumb", TRUE); // Declare the custom field for the image
if ($thumb != null or $thumb != '') {
echo $thumb;
}
elseif ($images = get_children(array( //If you upload an image function gets first image
'post_parent' => $postid,
'post_type' => 'attachment',
'numberposts' => '5',
'post_mime_type' => 'image', )))
foreach($images as $image) {
$thumbnail=wp_get_attachment_image_src($image->ID, $size);
?>
<?php echo $thumbnail[0]; ?>
<?php }
else { //If you don't upload or declare as thumb custom field func. gets custom (default) image
echo get_bloginfo ( 'template_directory' ); //same as wp-content/themes/your-theme/
echo '/images/image-pending.gif'; // Put this image into your themes images folder and set the path here
}
}
The only problem now is that the <?php echo $thumbnail[0]; ?> if there are more than one image it will echo all of them something like this
<img src=" http://applesiam.com/wp-content/uploads/2555-05-02_19h14_34-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h14_11-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_43-150x123.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_20-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_17-150x150.png ">
As you can see it just separated by some spaces.
Now I just want to have the last image if there is more than one image in the $thumbnail
I'm not really expert with PHP as my semester for PHP course will start next week.
Thanks in advance for any suggestion going to be.
Try:
$imgSrc = end((explode(' ', trim($imgSrc)));
Where $imgSrc is the value you put into <img src="!!!==>>here<<==!!!">.
quickly typed, w/o any warranty. Should leave single URLs intact, and if multiple ones separated by space(s) will take the last.
Try this instead.
echo trim($thumbnail[sizeof($thumbnail)-1]);
I'm trying to automatically switch my background image in wordpress depending on the visitors screen size. If he/she has a big screen, he/she gets the big version of the image. If the screen is small, he/she gets the smaller version. (Just for loading time reduction. The image will be resized to fill the screen afterwards, but that's already working.)
What isn't working is the check wether or not the smaller version even exists. If it doesn't exist, the script should fall back to the bigger version and just use that image. I get a background image, but it's only the big one (wordpress field name "BG_value". The url of the small image is stored in "BG_value-medium").
The images DO exist and the paths passed through the wordpress fields are fine, too, so that is NOT the problem. :/
But, without further ado, I present you the code (from my header.php from wordpress).
<body>
<!-- Wordpress Loop -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<script type="text/javascript">
if (($(window).width() < 1340) && (<?php
if(file_exists(bloginfo('template_url').get_post_meta($post->ID, 'BG_value-medium', $single = true))){
echo "true";
}else{
echo "false"; } ?> )){
<?php $bgimg = get_post_meta($post->ID, 'BG_value-medium', $single = true); ?>
} else {
<?php $bgimg = get_post_meta($post->ID, 'BG_value', $single = true); ?>
}
</script>
<div id="bgimage"> <img class="stretch" src="<?php bloginfo('template_url'); ?><?php echo $bgimg; ?>" alt="" /> </div>
<?php endwhile; endif; ?>
I'm sorry if this looks a bit messy, I've been working on this for the last few hours, changing it over and over again.
Any ideas what's going wrong here? Check out the page
You have a big logical error in this. You want to set the bg image with an javascript function, but you never try to set it with javascript, only with an php echo. Take a look at the sourcecode of this javascript snippet in your browser, and you will see what i mean.
You should store the image-pathes in javascript variables inside the then and else, and use them to set the bg image.
Untested:
<script type="text/javascript">
if (($(window).width() < 1340) && (<?php if(file_exists(bloginfo('template_url').get_post_meta($post->ID, 'BG_value-medium', $single = true)))){
echo "true";
}else{
echo "false"; } ?> )){
var bgimage="<?php echo get_post_meta($post->ID, 'BG_value-medium', $single = true); ?>";
} else {
var bgimage="<?php echo get_post_meta($post->ID, 'BG_value', $single = true); ?>";
}
document.getElementById("bgimageimg").src=bgimage;
</script>
<div id="bgimage"><img id="bgimageimg" class="stretch" src="" alt="" /></div>
I've tried to clean up that mess to see what is happening. The following is untested, but if something doesn't work you can now atleast see why it doesn't work.
<?php
if (have_posts()) {
while (have_posts()) {
the_post();
echo '<script type="text/javascript">';
$url = bloginfo('template_url');
$img = get_post_meta($post->ID, 'BG_value-medium', $single = true);
$file_exists = 'false';
if (file_exists($url.$img)) {
$file_exists = 'true';
}
echo 'var bgimg = '.get_post_meta($post->ID, 'BG_value', $single = true).';';
echo 'if ($(window).width() < 1340 && '.$file_exists.') {';
echo ' bgimg = '.get_post_meta($post->ID, 'BG_value-medium', $single = true).';';
echo '}';
echo '$("#bgimage").attr("src", bgimg);';
echo '</script>';
}
}
?>
I have created a basic component for Joomla that allows users to list an item along with some associated images. When the page containing the gallery is viewed a strange folder is created in the root web directory in the format of Resource id #100, the number varies with each item. I have narrowed down the code that is causing this to the following. My question is can some see what I'm doing to cause this and can anyone offer alternatives to the code I'm using to read the files from a particular directory and return the information.
<p id="sl_gallery">
<?php if( is_file( JPATH_ROOT.'/components/com_eg/images/gallery/'.$this->eg->id.'/main.jpg' ) ) : ?>
<img src="<?php echo JURI::root().'/components/com_eg/images/gallery/'.$this->eg->id.'/main.jpg' ?>" alt="myimage">
<?php else: ?>
<img src="<?php echo JURI::root().'/components/com_eg/images/nolistings.gif'; ?>" alt="myimage" />
<?php endif; ?>
<?php
$TrackDir= opendir(JPATH_ROOT.'/components/com_eg/images/gallery/'.$this->eg->id.'/second/');
$count = 0;
if ( !JFolder::exists($TrackDir) ) { JFolder::create($TrackDir); }
while (($file = readdir($TrackDir)) !== false) {
if ($file == "." || $file == "..") { }
else {
?>
<img src="<?php echo JURI::root().'/components/com_eg/images/gallery/'.$this->eg->id.'/second/'.$file; ?>" alt="myimage" />
<?php
}
}
closedir($TrackDir); ?>
</p>
Change this:
$TrackDir= opendir(JPATH_ROOT.'/components/com_eg/images/gallery/'.$this->eg->id.'/second/');
$count = 0;
if ( !JFolder::exists($TrackDir) ) { JFolder::create($TrackDir); }
...to this:
$TrackDirPath = JPATH_ROOT.'/components/com_eg/images/gallery/'.$this->eg->id.'/second/';
if ( !JFolder::exists($TrackDirPath) ) { JFolder::create($TrackDirPath); }
$TrackDir = opendir($TrackDirPath);
$count = 0;
$TrackDir holds the result of a call to opendir() - this means it will either be a resource or FALSE. When you convert a resource to a string, it results in Resource id # - which you did by (effectively) passing it to mkdir().
I have stored the path as a string in a variable $TrackDirPath, and passed that to JFolder::create() instead. I have also re-ordered the statements, to make sure that the directory exists before you try to open it.