Addiing captions above images in wordpress - wordpress

So I added some code in my css and there are boxes that appear over every image that is attached to a post. I've wanted to number the images and show the image number in the box(1...n). I have this in my functions.php
edit: code was added here http://pastebin.com/gVszwf75
If I run only count_images it will show the correct number of attached images to a post(let's say 15). But for some reason the number that is shown in the boxes over the images is always 1. I've seen this done on several blogs with just php so there has to be a way(even if I have to change my whole code).

The problem with your code is that you're looping through the array each time you call your callback function caption_image_callback() ... it has no memory of how many times it's looped!
The easiest way to fix this is to add a global variable at the beginning of the plug-in, I call it $caption_image_count and set it equal to zero. Then call the variable in caption_image_callback() and increment by 1 each time you call the function. This will keep track of the number of captioned images you have on the page.
If you want, you can also re-set the variable to zero before you return $post_body_content in caption_image(). I've posted the full solution to your pastebin: http://pastebin.com/sFe6dhqL

Related

Ambiguous match, found 2 elements matching css, how to get to the second one?

I am having problems in Capybara with the Ambiguous match problem. And the page provides no 'ids" to identify which one is which.
I am using within function.
within('.tile.tile-animation.animation-left.animation-visible.animated') do
#some code in here
end
I've used the :match option which solved my first problem.
within('.tile.tile-animation.animation-left.animation-visible.animated', :match => :first) do
#some code in here
end
The question is how to get to the SECOND css '.tile.tile-animation.animation-left.animation-visible.animated' ?
It depends on the html -- a simple solutions is
within(all('.tile.tile-animated.animation-left.animation-visible.animated')[1]) do
# some code in here
end
which will scope to the second matching element on the page, but won't be able to auto-reload if the page changes, and won't wait for the elements to appear. If you need it to wait for at least two elements to appear you can do
within(all('.tile.tile-animated.animation-left.animation-visible.animated', minimum: 2)[1]) do
....
which will wait some time for at least the 2 elements to appear on the page, but still won't be able to auto-reload if the page changes. If you need the ability to auto-reload on a dynamically changing page it will need to be possible to write a unique selector for the element (rather than indexing into the results of #all.

Check how many pages are included in a multi-page image file with GraphicsMagick

Is there a way to determine in advance how many pages are included in a multi page tif image with GraphicsMagick ?
I know I can select the first page in this way : image.tif[0], and probably iterating the array until the command fail will return the last image, but I don't think this is the best approach.
You can get a numbered list of the pages with
gm identify image.tif
and look at the last line, or count the lines. The images are numbered 0 thru N -1.
It is not too late to answer.
I think it is not possible to get the page count beforehand, not even with libtiff.
However, you can avoid the trial and error by using Magick::readImages:
// Magick++
std::vector<Magic::Image> images;
Magick::readImages(&images, "multipage.tif");

Create new JPG image thumbnail if original file has changed

I have a simple PHP script using imagecreatefromjpeg to create a thumbnail version of original and save it to a new folder. To speed parsing (all done in the back end prior to upload to static site) I am using file_exist before the creation to check it exists and show - if not create new and show. This works fine but if the original image changes the script does not generate as the thumbnail image exists in it's old form.
I guess I need to use MD5 test but as a n00b not sure how to test if the resulting thumbnail version would be different from the existing as produced by the main image.
Current Logic:
if thumb exist == do nothing,
if thumb does not exist == create it
Additional logic
if thumb will change due to change in original == create it,
if thumb will stay the same == do nothing
The existing PHP is very poor and clobbered together so happy for any pointers from a fresh (expert) view.
Thanks, John
You want to check the time the file was modified.
http://php.net/manual/en/function.filemtime.php
if the original file's timestamp is newer than the thumbnail's timestamp, then make a new thumbnail.
Thanks guys - not sure if that will work if the original image was created previously but only just being used eg stock images that are on file elsewhere and then dragged into the /images/ folder to replace existing image but with the same filename.. does the timestamp show the date dragged or when it was created?
If you move the files, the modified timestamp does not change.
If you copy the files, the copies are created by the copy action.
When you create a file, its Date Modified timestamp is the same as its Date Created timestamp.
So if your drag is copying the files, then the modified timestamp is the timestamp of the moment that the files were copied.
It should be enough to compare the last modification date of the thumb and the original image. if the original image is newer than the thumb, you should recreate it.
In simple terms, say we have an image that is a 200x200 image filled with red and only red (so yeah a red square). We create 80x80 thumbnails. If the original image changes but is still just the same red filled square, the thumbnail will not change.
The thing is that in order to detect this you will need to create the thumbnail or apply advanced image comparison techniques, that you may as well save it.
You can however use MD5 to create a one-on-one map from original to thumbnail, by naming the thumbnails as the MD5 hash of the original. The drawback of this approach is that you will need a way to remove unused thumbnails when their originals are no longer used.
To use this approach use something like:
<?php
$thumbname = md5_file($bigimage_path);
if( !file_exists("images/thumbs/$thumbname.jpg") )
{
create_thumb($bigimage_path, $thumbname);
}
?>
This will accomplish that if you have the exact same big image as different filenames, they will all map to the same thumbnail.

how display more lines in wordpress-the_excerpt()

Hai,
I am designing a blog in wordpress. I am facing problem when I display the topic through the_excerpt().It only display 3 lines and then Continue Link comes.But I want each post will
come with 6-10 line and the continue-> link.how I can increase the line numbers.
You need to add an excerpt_length filter to your theme before you call the_except.
In the filter you sepcify how many words you want in your except (you can only specify words, not lines).
An example:
function my_excerpt_length($length) {
return 120;
}
add_filter('excerpt_length', 'my_excerpt_length');
If you REALLY want to filter on a certain number of lines, you could call get_the_content and extract the number of lines you wanted - but make sure you filter the content to ensure nothing unwanted makes it into your blog.

How to restrict text length of a field while in WordPress editor?

I would like to restrict the fields while creating a new post in WordPress. For the title, it should not exceed 40 characters. For the content, it should not exceed 400 characters. If these maximum values are exceeded, I would like to show an error message and not let the user continue. How do I do that in WordPress?
You should be able to use wordpress filters to modify the code that gets outputted when the editor is called. Essentially you would want to use it to insert some javascript and an extra div tag to display your error, then just read the contents of the "editorcontainer" id and show the error once it reaches a certain character limit.
I don't have the time at the moment to write a case example, but depending on your skill level, the functions you are looking for are:
apply_filters("the_editor", "customfunction_limitarea");
Where customfunction_limit area is your created function to insert the javascript. You can see how the_editor is currently called and how the default filters are applied in "wp-includes\general-template.php" on line 1822. The default looks like this:
$the_editor = apply_filters('the_editor', "<div id='editorcontainer'><textarea rows='$rows'$class cols='40' name='$id' tabindex='$tab_index' id='$id'>%s</textarea></div>\n");
I would try modifying that statement by placing a new filter in a functions.php file located in your themes directory, that way you don't have to worry about it getting over-written during an update. Otherwise, if you absolutely have to edit the wordpress core (generally a no-no), general_template.php would be the place to do it I think.
Essentially just read up on wordpress filters a little bit (be warned there's not a ton of documentation or examples available for it other than the basic stuff), and that should provide everything you need. The input verification end is easy enough to find scripts, just google jquery post limiting. Something like this might be exactly what your looking for:
http://swiki.fromdev.com/2010/02/jquery-maximum-limit-texttextarea-with.html

Resources