Single Image in settings page to be added to a page or post custom field.
Support › General › Single Image in settings page to be added to a page or post custom field.Resolved
- This topic has 11 replies, 2 voices, and was last updated 1 year, 9 months ago by
Peter.
-
AuthorPosts
-
July 26, 2023 at 5:50 PM #42751
[email protected]
ParticipantHi Metabox,
Is it possible to populate an image custom field added to a post or page by uploading an image in a settings page?
I then want to display the image on the frontend using the post/page custom field shortcode (not th.
Many thanks
Mick
July 26, 2023 at 9:51 PM #42756Peter
ModeratorHello,
If you want to update the image on the settings page to the image custom field on a/some posts, you can try to use the action hook
rwmb_{$field_id}_after_save_field
. Follow the documentation https://docs.metabox.io/actions/rwmb-after-save-field/If you just want to display the image on the frontend, you can use the shortcode. For example:
[rwmb_meta id="image_field_id" object_id="settings_page_option" object_type="setting"]
Follow the documentation https://docs.metabox.io/shortcode/
July 26, 2023 at 10:29 PM #42759[email protected]
ParticipantHello Peter,
Thanks for the quick reply!
Yes it seems the first example is the right solution, I did check the doc, but their is no example for updating other fields
Does this look ok to you?
// Hook into rwmb_after_save_field action add_action( 'rwmb_after_save_field', 'update_target_field', 10, 5 ); function update_target_field( $null, $field, $new, $old, $object_id ) { // Check if the saved field is 'image_of_town_1' and the object is a settings page if ( $field['id'] === 'image_of_town_1' && is_admin() && get_post_type( $object_id ) === 'settings_page' ) { // Update the target field (image_of_town) on the post/page with the image URL update_post_meta( 110, 'image_of_town', $new ); } }
Many thanks
Mick
July 27, 2023 at 8:59 PM #42764Peter
ModeratorHello,
I think it's good, except for one point, it could not work because the settings page is not a post type. So you should remove this condition
get_post_type( $object_id ) === 'settings_page'
July 27, 2023 at 10:55 PM #42768[email protected]
ParticipantHello Peter,
Again thank you for the quick reply, unfortunately It did not populate it and still shows the add media button in the page custom fields 🙁 I did remove the condition as you advised so the new code is now:
// Hook into rwmb_after_save_field action add_action( 'rwmb_after_save_field', 'update_target_field', 10, 5 ); function update_target_field( $null, $field, $new, $old, $object_id ) { // Check if the saved field is 'image_of_town_1' and the object is a settings page if ( $field['id'] === 'image_of_town_1' && is_admin() ) { // Update the target field (image_of_town) on the post/page with the image URL update_post_meta( 110, 'image_of_town', $new ); } }
Here is the display code output of the image_of_town_1 in Metabox just so you can see I am using the correct ID's:
This is a single image field in a tab in a settings page:
<?php // Displaying uploaded image: ?> <?php $image = rwmb_meta( 'image_of_town_1', [ 'object_type' => 'setting', 'size' => 'thumbnail' ], 'mb-website-settings' ); ?> <h3>Logo</h3> <img src="<?= $image['url']; ?>"> <?php // or simpler: ?> <h3>Logo</h3> <?php rwmb_the_value( 'image_of_town_1', [ 'object_type' => 'setting', 'size' => 'thumbnail' ], 'mb-website-settings' ) ?>
Here is the single image field in the page custom field:
<?php // Displaying uploaded image: ?> <?php $image = rwmb_meta( 'image_of_town', [ 'size' => 'thumbnail' ] ); ?> <h3>Logo</h3> <img src="<?= $image['url']; ?>"> <?php // or simpler: ?> <h3>Logo</h3> <?php rwmb_the_value( 'image_of_town', [ 'size' => 'thumbnail' ] ) ?>
I also double checked the ID of the page and it is 110.
Many thanks again for your support.
Kindest Regards
Mick
July 28, 2023 at 10:29 PM #42771Peter
ModeratorHello,
The code works fine on my local site, here is the screen record https://drive.google.com/file/d/1mdEJhBQdb5XB6xik_fEsR0ymQP5DPwFa/view?usp=sharing
You can try to add the code to the file functions.php in the theme/child theme folder and deactivate all other plugins except Meta Box, MB plugins to see if it works.
July 29, 2023 at 12:10 AM #42772[email protected]
ParticipantHi Peter,
Still does not work for me for some strange reason. I used 2021 theme with only MB plugins installed and added the code the them function.php file.
Please see video for you to review:
https://komododecks.com/recordings/FJ8dZstUsF8pm8Ghk7V6
Many thanks
Mick
July 30, 2023 at 3:42 PM #42774Peter
ModeratorHello,
Please share your site admin account via this contact form https://metabox.io/contact/
I will take a closer look.July 30, 2023 at 8:40 PM #42775[email protected]
ParticipantThanks Peter,
Just sent them to you now.
Many thanks
July 30, 2023 at 10:39 PM #42776Peter
ModeratorThere are two issues on your site:
1. The code is added inside an
if
statement and the condition is not true so the code is not executed. Screenshot https://imgur.com/bSTMLbH2. The field group Location Details has the field ID prefix
location_details
so the fieldimage_of_town_1
in this field group has the correct IDlocation_detailsimage_of_town_1
. The code should beif ( $field['id'] == 'location_detailsimage_of_town_1' && is_admin() ) {
I fixed those issues on your site and now it works fine.
July 31, 2023 at 1:22 AM #42778[email protected]
ParticipantHello Peter,
Thank you so much!
1. Doh, I must not have scrolled down far enough!
2. Ah ok, I was not to sure if to include it or not/or even the ID of the tab for the settings page haha.
Finally may I ask, is it possible to run that but without the page ID? Example, wherever there is a custom field named
image_of_town
on a post or page and thelocation_detailsimage_of_town_1
is saved it will update them all? Do I jusr remove the110,
from this stringupdate_post_meta( 110, 'image_of_town', $new );
so it will readupdate_post_meta( 'image_of_town', $new );
Again many thanks for your help!
July 31, 2023 at 5:42 PM #42781Peter
ModeratorHello,
Follow the WordPress documentation https://developer.wordpress.org/reference/functions/update_post_meta/
you have to pass the post/page ID to the functionupdate_post_meta()
to update the field value (post meta) for a post/page.If you want to update that value for all posts/pages, please create a custom WP query to get posts/pages and use the function
update_post_meta()
in the loop. You can read more about WP query here https://developer.wordpress.org/reference/classes/wp_query/ -
AuthorPosts
- You must be logged in to reply to this topic.