WordPress.org

Ready to get started?Download WordPress

Forums

How do I Add images uploaded in the post to a default custom field (2 posts)

  1. demilio
    Member
    Posted 2 years ago #

    I'm trying to add the URL of the post images in to a default custom field. Here is the code I got so far...

    Here is how I add the default custom field:

    add_action('wp_insert_post', 'mk_set_default_custom_fields');
        function mk_set_default_custom_fields($post_id)
        {
            if ( $_GET['post_type'] != 'post' ) {
                add_post_meta($post_id, 'Image', '', true);
            }
            return true;
        }

    Now I need a function that fills the value with the URL from the image uploaded. If there is no image uploaded the field can be empty.

    Here is the code to fill the value with info, but I need the thumbnail url in the value

    add_post_meta($post_id, 'custom field name', 'custom field value', true);

  2. demilio
    Member
    Posted 2 years ago #

    I've done some research and almost got a solution now, but still haveing problems with the last peace.

    Here is the update question:

    I got a function that automaticly creates a custom field in the post. I have this located in my functions.php.

    Image is the name of the custom field and HERE is the value. How can I put the function w_thumbnail_src as the variable?

    add_action('wp_insert_post', 'mk_set_default_custom_fields');
        function mk_set_default_custom_fields($post_id)
    
        {
            if ( $_GET['post_type'] != 'post' ) {
                add_post_meta($post_id, 'Image','HERE', true);
            }
            return true;
        }

    and let me add that w_thumbnail_src is a function in the same file that looks like this

    function w_thumbnail_src() {
        if (has_post_thumbnail()) {
            $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
           echo $thumb[0]; // thumbnail url
        }
    }

Topic Closed

This topic has been closed to new replies.

About this Topic