Is this a good way to achieve this?
Does this input need escaping/sanitizing ?
Could you help with filtering the image caption shortcode to output the credit_txt as an extra paragraph appended to the caption, preceded by something like “Photo supplied by:”.
At least that is how I think it would be done after looking through media.php
For the caption filter, you’ll need something like the following. This is mostly just copied directly from the img_caption_shortcode() function in wp-includes/media.php.
It does not appear to be saving to the database. At least, it is not displaying on the front end. I looked through the postmeta using phpmyadmin, which is where I presume it would save. I’m not experienced in the database, but I cannot see anything in the correct post id except
350 _wp_attached_file 2018/06/hogback-sleepers.jpg
350 _wp_attachment_metadata a:5:{s:5:"width";i:1007;s:6:"height";i:971;s:4:"fi...
350 _wp_attachment_image_alt hogback sleepers in the railway tunnels on the Daw...
There is nothing relating to the credit field when browsing_wp_attachment_metadata which is where I thought it would be, or the other two for that matter.
Curiously it stays in the field in the media library window after logging out in Firefox, log in using Chrome incognito and there it is.
Credit Image by: M McLachlan
Insert image owners name
I’ve been offline most of today. I’ll get this set up on my computer in the morning and see if I can figure it out. It’s a bit much to look over without more testing.
but when I try to get the link to open in a new tab by adding target="_blank" rel="noopener" it redirects to .404, so I am not getting that right, if you could help with that it would be good.
A new day and a little more reading and I find the caption is treated as the excerpt in the posts table. Learning, learning. However, I still can’t find where data from the added fields is stored. I expected it would be with the caption.
Re: That adds sanitization and also a way to delete the text if it’s removed.
Adding a way of deleting the text if it’s removed would be good.
Removing the text from the fields removes it from the front end.
Should the text fields in his code be sanitized?
but when I try to get the link to open in a new tab by adding target=”_blank” rel=”noopener” it redirects to .404, so I am not getting that right, if you could help with that it would be good.
To add your target and rel attributes, you need to change this code:
I also added in some escaping of the data so that it’d be secure on output.
A new day and a little more reading and I find the caption is treated as the excerpt in the posts table. Learning, learning. However, I still can’t find where data from the added fields is stored. I expected it would be with the caption.
Post meta (what this is) is stored in the *_postmeta table in your database.
Adding a way of deleting the text if it’s removed would be good.
Removing the text from the fields removes it from the front end.
Should the text fields in his code be sanitized?
The save_image_source_url() function needs to be fixed for all of these.
It certainly does.
Many Thanks.
This is for another community website with several authors and photo sources – so their thanks as well.
I use Diffmerge to compare the code and to learn.
If your code was a newly planted crop, every seed would germinate, the rows of plants would be straight, the organic fertiliser perfectly balanced and the crop abundant.
I went back through the site to add source and links to various images.
If only one field is used, eg Photo credit without link field being used, it does not display on the front end.
If the link field alone is used it wraps the credit in a link, as expected, but does not open the link in a new tab, rather opens it in the same page.
Since the advent of the new editor, these fields have to be accessed after using the image block to insert the image in the content by editing the image. (click on pencil image to open editing)
I’ve used (slightly modified) info from here to add image credit meta to media attachments
Is this a good way to achieve this?
Does this input need escaping/sanitizing ?
Could you help with filtering the image caption shortcode to output the
credit_txt
as an extra paragraph appended to the caption, preceded by something like “Photo supplied by:”.At least that is how I think it would be done after looking through
media.php
My attempts seem to break things.
Your save function should be something more along these lines (untested):
That adds sanitization and also a way to delete the text if it’s removed.
Let me look into the image caption shortcode.
For the caption filter, you’ll need something like the following. This is mostly just copied directly from the
img_caption_shortcode()
function inwp-includes/media.php
.It does not appear to be saving to the database. At least, it is not displaying on the front end. I looked through the postmeta using phpmyadmin, which is where I presume it would save. I’m not experienced in the database, but I cannot see anything in the correct post id except
There is nothing relating to the credit field when browsing
_wp_attachment_metadata
which is where I thought it would be, or the other two for that matter.Curiously it stays in the field in the media library window after logging out in Firefox, log in using Chrome incognito and there it is.
I’ve been offline most of today. I’ll get this set up on my computer in the morning and see if I can figure it out. It’s a bit much to look over without more testing.
A new day and a new search found this which works and has the added benefit of adding a link.
He references your post from 2011 which I had read a couple of nights ago.
I changed this:
to this
and this:
to this:
but when I try to get the link to open in a new tab by adding
target="_blank" rel="noopener"
it redirects to .404, so I am not getting that right, if you could help with that it would be good.A new day and a little more reading and I find the caption is treated as the excerpt in the posts table. Learning, learning. However, I still can’t find where data from the added fields is stored. I expected it would be with the caption.
Re: That adds sanitization and also a way to delete the text if it’s removed.
Adding a way of deleting the text if it’s removed would be good.
Removing the text from the fields removes it from the front end.
Should the text fields in his code be sanitized?
To add your target and rel attributes, you need to change this code:
To this:
I also added in some escaping of the data so that it’d be secure on output.
Post meta (what this is) is stored in the
*_postmeta
table in your database.The
save_image_source_url()
function needs to be fixed for all of these.the
save_image_source_url()
function has a problem of some kind.Using the old version allows it all to work (with other changes working.)
If you could post a GitHub Gist or share the entirety of your code, I could test it.
there are 2 files in the gist
test.php
works (when added tofunctions.php
)test2.php
fails (when added tofunctions.php
)this is the gist
I cleaned up the code and tested it. I just posted as a comment on your Gist here: https://gist.github.com/Markmcl/2bb6e97cfd03f9fee1e6bd9d76625df7#gistcomment-2657714
It seems to work good for me.
It certainly does.
Many Thanks.
This is for another community website with several authors and photo sources – so their thanks as well.
I use Diffmerge to compare the code and to learn.
If your code was a newly planted crop, every seed would germinate, the rows of plants would be straight, the organic fertiliser perfectly balanced and the crop abundant.
I went back through the site to add source and links to various images.
If only one field is used, eg Photo credit without link field being used, it does not display on the front end.
If the link field alone is used it wraps the credit in a link, as expected, but does not open the link in a new tab, rather opens it in the same page.
I was going to mention the various scenarios (text plus URL, text without URL, URL without text) but figured you had it like you wanted.
I added another comment on the Gist to correct the above two things. https://gist.github.com/Markmcl/2bb6e97cfd03f9fee1e6bd9d76625df7#gistcomment-2658941
Since the advent of the new editor, these fields have to be accessed after using the image block to insert the image in the content by editing the image. (click on pencil image to open editing)
However they do not now show in the caption.
I had searched for “how to save media data to gutenburg image block” and came up with
https://github.com/WordPress/gutenberg/issues/11333
and
https://gist.github.com/joemcgill/dbec1854cd02d63b731c583f4a751e7c
which could be an answer but I do not seem to be able to advance things after messing around with it
I haven’t been able to find anything. I’m not really sure how it’s handled myself.
Just to be clear, you’re wanting to get the “credit” to appear in the new editor, right? It’s already appearing on the front end?
Did the credit line appear in the old editor?