Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store Image as ID #1498

Closed
JiveDig opened this issue Aug 9, 2017 · 13 comments
Closed

Store Image as ID #1498

JiveDig opened this issue Aug 9, 2017 · 13 comments

Comments

@JiveDig
Copy link

JiveDig commented Aug 9, 2017

Issue description:

First off, I already read #712

I was a bit surprised when I realized Kirki stores image field values as a URL. I have an existing plugin that i'm converting to use Kirki and this is/was the only hiccup. I was easily able to store the value as an ID using the sanitize_callback, the only issue now is filtering that option (not theme_mods here) and converting the ID to URL when is_customize_preview(). Then my function to retrieve that field also needs to check if the value is a URL, and convert accordingly, though this only happens when using the customizer.

I was hoping apply_filters( 'kirki/values/get_value', $value, $field_id ); would be the easy solution to convert ID to URL only for the field value, but that didn't work.

Is there a better way to do this considering I have a ton of active installs using ID, and I'd prefer to keep it that way?

Lastly, another option is to add a parameter to the image field to give the option to store as URL or ID.

Version used:

3.0.9 from WP repo

Using theme_mods or options?

options

@aristath
Copy link
Contributor

aristath commented Aug 9, 2017

@JiveDig saving as a URL is the default behaviour, but not the only option. 😉
Try adding this to one of your image fields:

'choices' => array(
    'save_as' => 'array'
),

This will save the value as an array containing the url, width, height and ID of the image.

@JiveDig
Copy link
Author

JiveDig commented Aug 9, 2017

Ooh that is interesting, and very helpful. Is there a built in way to only save as ID though?

'save_as' => 'ID' ?

@aristath
Copy link
Contributor

aristath commented Aug 9, 2017

I think it's half-implemented... I started working on it but never tested if it works or not (which is why it's not even documented anywhere).
If it doesn't work let me know and I'll fix it 👍

@JiveDig
Copy link
Author

JiveDig commented Aug 9, 2017

Okay, it definitely stores the image as an ID, which is great (and easy enough to do with the sanitize_callback. But it has the same issue, the image preview is not showing now.

I'm guessing you upon loading of the field you need to check the save_as value and convert accordingly?

aristath added a commit that referenced this issue Aug 9, 2017
@aristath
Copy link
Contributor

aristath commented Aug 9, 2017

I just pushed a commit that should fix this.
Sorry, I'm unable to test right now, but in theory that code should fix the issue and allow using the ID.
Could you please test it now and let me know if that works for you?

@JiveDig
Copy link
Author

JiveDig commented Aug 9, 2017

Hmmm, doesn't seem to work.

I just copied the changes to that file into my current local install. Is there an easier way to test? Is there a featured branch I can use? Pardon my git limitations here ;)

aristath added a commit that referenced this issue Aug 9, 2017
@aristath
Copy link
Contributor

aristath commented Aug 9, 2017

The easiest way is to just download the develop branch from here: https://github.com/aristath/kirki/archive/develop.zip
This always contains the latest commits. 👍
I just pushed another fix, I found some time to test this and it seems to be working now.

@JiveDig
Copy link
Author

JiveDig commented Aug 9, 2017

Yes, this is working. Awesome. I'll lookout for an update before i push my code for my plugin. Thanks.

@JiveDig
Copy link
Author

JiveDig commented Aug 10, 2017

Not to be pushy, but i'm wondering if you have a timeline for the next release, so I can plan my dev/release schedule accordingly.

@aristath
Copy link
Contributor

@JiveDig I'm hoping for sometime next week, though I can't make any promises about that.
version 3.0.10 changed quite a few things - mostly performance improvements - and I need to be thorough with my testing.

@JulienMelissas
Copy link
Contributor

Hey there! Not to revive and old thread, but it looks like there are still some half-baked issues with saving images as IDs and arrays. Right now, I'm trying to save an image as an array and even when I make a change in the customizer it doesn't give me option to save or publish my changes. And also it's not removing the value from the customizer when I click "remove".

@JulienMelissas
Copy link
Contributor

Nevermind! Ugh, silly user error on my part. Array field type is working for me, I think this issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants