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

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #42751
    michael@wdsaigon.com[email protected]
    Participant

    Hi 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

    #42756
    PeterPeter
    Moderator

    Hello,

    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/

    #42759
    michael@wdsaigon.com[email protected]
    Participant

    Hello 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

    #42764
    PeterPeter
    Moderator

    Hello,

    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'

    #42768
    michael@wdsaigon.com[email protected]
    Participant

    Hello 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

    #42771
    PeterPeter
    Moderator

    Hello,

    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.

    #42772
    michael@wdsaigon.com[email protected]
    Participant

    Hi 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

    #42774
    PeterPeter
    Moderator

    Hello,

    Please share your site admin account via this contact form https://metabox.io/contact/
    I will take a closer look.

    #42775
    michael@wdsaigon.com[email protected]
    Participant

    Thanks Peter,

    Just sent them to you now.

    Many thanks

    #42776
    PeterPeter
    Moderator

    There 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/bSTMLbH

    2. The field group Location Details has the field ID prefix location_details so the field image_of_town_1 in this field group has the correct ID location_detailsimage_of_town_1. The code should be

        if ( $field['id'] == 'location_detailsimage_of_town_1' && is_admin() ) {
    

    I fixed those issues on your site and now it works fine.

    #42778
    michael@wdsaigon.com[email protected]
    Participant

    Hello 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 the location_detailsimage_of_town_1 is saved it will update them all? Do I jusr remove the 110, from this string update_post_meta( 110, 'image_of_town', $new ); so it will read update_post_meta( 'image_of_town', $new );

    Again many thanks for your help!

    #42781
    PeterPeter
    Moderator

    Hello,

    Follow the WordPress documentation https://developer.wordpress.org/reference/functions/update_post_meta/
    you have to pass the post/page ID to the function update_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/

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.